您的位置:首页 > 其它

ElasticSearch全文搜索小案例

2018-12-19 17:49 225 查看

ElasticSearch实战应用

ElasticSearch介绍

ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。
实际上它就是一个非关系型的数据库

实现demo

我用的框架是springboot+JPA,首先去ElasticSearch官网下载ElasticSearch,点击bin/elasticsearch.bat启动.

项目的pom依赖

<!-- elasticsearch -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

因为elasticsearch 是一个非关系型数据库,所以要实现全文搜索,第一步就是把数据导入elasticsearch 中;
数据库的实体类如下:

@Entity
@Table(name="poetry")
public class Poetry implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String title;
private String author;
private String kind;
private String intro;
private String content;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id=id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title=title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author=author;
}

public String getKind() {
return kind;
}

public void setKind(String kind) {
this.kind=kind;
}

public String getIntro() {
return intro;
}

public void setIntro(String intro) {
this.intro=intro;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content=content;
}
}

再新建一个ElasticSearch的实体类

@Document(indexName = "knowledge", type = "poetry")
public class PoetryModel implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String title;
private String author;
private String kind;
private String intro;
private String content;

@Id
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getAuthor() {
return author;
}

public void setAuthor(String author) {
this.author = author;
}

public String getKind() {
return kind;
}

public void setKind(String kind) {
this.kind = kind;
}

public String getIntro() {
return intro;
}

public void setIntro(String intro) {
this.intro = intro;
}

public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

}

一定要加上这个注解
import org.springframework.data.elasticsearch.annotations.Document;
@Document(indexName = “knowledge”, type = “poetry”);
indexName表示库名;poetry表示表名.

执行导入方法,把数据库的内容写到ElasticSearch中;

public void init() {
List<Poetry> list = poetryRepository.findAll();
for (Poetry p : list) {
PoetryModel pModel = new PoetryModel();
BeanUtils.copyProperties(p, pModel);
poetryElasticRepository.save(pModel);
System.out.println("导入-" + pModel.getId() + "-" + pModel.getTitle());
}
}

Controller层代码

@Controller
@RequestMapping("/poetry")
public class PoetryController extends BaseController {
@Autowired
private PoetryService poetryService;

@RequestMapping("/init")
public String init() {
poetryService.init();
return "demo/success";
}

@RequestMapping("/findByContent")
@ResponseBody
public List<PoetryModel> findByContent(String content) {

return poetryService.findByContent(content);
}

@RequestMapping("/findByAuthor")
@ResponseBody
public List<PoetryModel> findByAuthor(String author) {
return poetryService.findByAuthor(author);
}
}

前段代码用的thymeleaf
代码成果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: