检索(Elasticsearch)基础-SpringBoot
Spring Boot与检索(ElasticSearch)
一、 检索概述ElasticSearch
我们的应用经常需要添加检索功能,开源的 ElasticSearch 是目前全文搜索引擎的首选。他可以快速的存储、搜索和分析海量数据。Spring Boot通过整合Spring Data ElasticSearch为我们提供了非常便捷的检索功能支持;
Elasticsearch是一个分布式搜索服务,提供Restful API
,底层基于Lucene
,采用多shard(分片)的方式保证数据安全,并且提供自动resharding的功能,github等大型的站点也是采用了ElasticSearch作为其搜索服务。
二、ElasticSearch中的概念
- 以
员工文档
的形式存储为例:一个文档
代表一个员工数据。存储数据到 ElasticSearch 的行为叫做索引
,但在索引一个文档之前,需要确定将文档存储在哪里。 - 一个 ElasticSearch 集群可以 包含多个
索引
,相应的每个索引可以包含多个类型
。 这些不同的类型存储着多个文档
,每个文档又有 多个属性
。 - 类似关系mysql:
索引-数据库
类型-表
文档-表中的记录
属性-列
RestAPI操作ES
PUT:添加,修改 /索引/类型/文档id
GET:获取 /索引/类型/文档id
HEAD: 查看是否存在
DELETE:删除 /索引/类型/文档id
注释: 1.特殊用法_search ?lastname=* 含义:查询所有并且name=*
2.还可以利用查询表达式
来定义查询
查询表达式很重要,后续会更新相关语法。
三、SpringBoot整合ElasticSearch案例
1. Docker安装ElasticSearch
注:注意设置堆内存的大小,默认2G
浏览器测试验证:
2. 引入依赖jar包
SpringBoot默认支持两种技术来和ES交互;
1、Jest(默认不生效)
需要导入jest的工具包(io.searchbox.client.JestClient)
2、SpringData ElasticSearch【ES版本有可能不合适】
版本适配说明:https://github.com/spring-projects/spring-data-elasticsearch
如果版本不适配:2.4.6
1)、升级SpringBoot版本
2)、安装对应版本的ES
3、Client 节点信息clusterNodes;clusterName
4、ElasticsearchTemplate 操作es
5、编写一个 ElasticsearchRepository 的子接口来操作ES;
两种用法:https://github.com/spring-projects/spring-data-elasticsearch
6、编写一个 ElasticsearchRepository
<!--SpringBoot默认使用SpringData ElasticSearch模块进行操作-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/io.searchbox/jest -->
<dependency>
<groupId>io.searchbox</groupId>
<artifactId>jest</artifactId>
<version>5.3.3</version>
</dependency>
3. 配置文件
#二选一,上面是jest方式,下面是springDate的方式
spring.elasticsearch.jest.uris=http://118.24.44.169:9200
spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=118.24.44.169:9301
4.SpringBoot整合ElasticSearch案例
1. jest方式
1. 保存索引
public void contextLoads() {
//1、给Es中索引(保存)一个文档;
Article article = new Article();
article.setId(1);
article.setTitle("好消息");
article.setAuthor("zhangsan");
article.setContent("Hello World");
//构建一个索引功能
Index index = new Index.Builder(article).index("atguigu").type("news").build();
try {
//执行
jestClient.execute(index);
} catch (IOException e) {
e.printStackTrace();
}
}
注意:PO类里面要生命主键ID
2.搜索
public void search(){
//查询表达式
String json ="{\n" +
" \"query\" : {\n" +
" \"match\" : {\n" +
" \"content\" : \"hello\"\n" +
" }\n" +
" }\n" +
"}";
//更多操作:https://github.com/searchbox-io/Jest/tree/master/jest
//构建搜索功能
Search search = new Search.Builder(json).addIndex("atguigu").addType("news").build();
//执行
try {
SearchResult result = jestClient.execute(search);
System.out.println(result.getJsonString());
} catch (IOException e) {
e.printStackTrace();
}
}
1. SpringDate BookRepository方式
1.查询
1.先订制repository类
2.再查询
public interface BookRepository extends ElasticsearchRepository<Book,Integer> {
//参照
// https://docs.spring.io/spring-data/elasticsearch/docs/3.0.6.RELEASE/reference/html/
public List<Book> findByBookNameLike(String bookName);
}
public void test02(){
// Book book = new Book();
// book.setId(1);
// book.setBookName("西游记");
// book.setAuthor("吴承恩");
// bookRepository.index(book);
for (Book book : bookRepository.findByBookNameLike("游")) {
System.out.println(book);
}
;
}
注意:用此方式添加的PO类是需要用@Document来注释声明索引和类型值
转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 18846433665@163.com
文章标题:检索(Elasticsearch)基础-SpringBoot
文章字数:982
本文作者:Xu Shengcai
发布时间:2019-09-25, 17:55:42
最后更新:2019-09-27, 03:06:34
原始链接:http://yoursite.com/2019/09/25/SpringBoot与检索-Elasticsearch-基础/版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。