SpringBoot与检索-ElasticSearch

  • ElasticSearch
  • ElasticSearch使用
  • 整合ElasticSearch

打开网易新闻 查看精彩图片

ElasticSearch

  • ElasticSearch是开源的全文搜索引擎,可以快速地存储,搜索,分析海量数据.SpringBoot通过整合Spring Data ElasticSearch提供检索功能支持
  • ElasticSearch是分布式搜索服务,提供RESTful API,底层基于Lucene,采用多shard(分片) 的方式保证数据安全,并且提供 自动resharding的功能

ElasticSearch使用

  • 参照ElasticSearch使用文档
  • 示例

对于员工目录,我们将做如下操作:1.每个员工索引一个文档,文档包含该员工的所有信息。2.每个文档都将是 employee 类型 。3.该类型位于 索引 megacorp 内。4.该索引保存在我们的 Elasticsearch 集群中。

PUT /megacorp/employee/1{ "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ]}

  • 路径/megacorp/employee/1 包含了三部分的信息:megacorp:索引名称employee:类型名称1:特定雇员的ID

打开网易新闻 查看精彩图片

整合ElasticSearch

  • 引入spring-boot-starter-data-elasticsearch

org.springframework.bootgroupId> spring-boot-starter-data-elasticsearchartifactId> dependency>

  • 安装Spring Data对应版本的ElasticSearch
  • application.yml配置
  • SpringBoot自动配置的ElasticsearchRepository,ElasticsearchTemplate,Client
  • SpringBoot默认支持两种技术和ElasticSearch进行交互:
  • Jest:默认不生效使用时需要导入Jest工具包:import io.searchbox.client.JestClient;

1.给Elasticsearch索引一个文档

2.构建索引功能 index.Builder(article).index("indexName").type("news").build();

3.搜索 search.Builder(json).addIndex("indexName").addType("news").build(); .执行 jetClient.excute()

打开网易新闻 查看精彩图片