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
代码成果:
相关文章推荐
- Spring Data Elasticsearch加Elasticsearch服务实现全文搜索
- Lucene/Solr/ElasticSearch搜索问题案例分析
- 搜索学习--Elasticsearch全文搜索服务器的基本使用
- 20_ElasticSearch rescoring机制优化近似匹配搜索的性能
- ElasticSearch+Springboot实际应用:索引同步建设,搜索过程
- 分布式搜索Elasticsearch——概述
- 分布式搜索Elasticsearch——创建客户端(二)
- 13_ElasticSearch multi_match+most fiels策略进行multi-field搜索
- Elasticsearch Delete/UpdateByQuery案例分享
- 分布式搜索Elasticsearch——安装
- 实时搜索 elasticsearch vs solr
- 分布式搜索Elasticsearch——集成paoding-maping
- 分布式搜索Elasticsearch——MultiSearch
- 分布式搜索Elasticsearch——创建客户端(五)
- 实战:Nodejs+Mongodb+Elasticsearch 实现简单的搜索
- Linq实践系列(1):一行代码实现DataTable全文搜索(Full Text Search)
- [搜索]ElasticSearch Java Api(一) -创建索引
- Zend Search Lucene实现全文搜索
- ElasticSearch+Solr几个案例笔记
- 分布式搜索Elasticsearch——删除指定索引