ElasticSearch分布式搜索引擎从入门到实战应用(实战篇-仿京东首页搜索商品高亮显示)
Posted 一宿君
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ElasticSearch分布式搜索引擎从入门到实战应用(实战篇-仿京东首页搜索商品高亮显示)相关的知识,希望对你有一定的参考价值。
ElasticSearch分布式搜索引擎从入门到实战应用(实战篇-仿京东首页搜索商品高亮显示)
1、熟悉SpringBoot集成ElasticSearch
1.1、官方指导文档
elasticsearch官方指导文档:https://www.elastic.co/guide/index.html
推荐使用REST风格操作es,可以直接根据REST Client客户端官方指导文档即可:
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/index.html
1.2、创建集成项目配置
1、引入springboot集成es客户端依赖
<!--springboot集成es-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
2、统一版本
<!--此处version最好改为2.2.5.RELEASE,不要太高,也不要太低-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--版本统一-->
<properties>
<java.version>1.8</java.version>
<elasticsearch.version>7.6.1</elasticsearch.version>
</properties>
3、导入后续会用到的关键依赖
<!--lombok插件依赖,需要先安装lombok插件引入依赖才不build报错-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--fastjson-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.70</version>
</dependency>
4、创建并编写配置类
@Configuration
public class ElasticSearchRestClientConfig
// 向spring容器中注入Rest高级客户端
//方法名最好和返回类型保持一直,后续自动匹配装载时方便
@Bean
public RestHighLevelClient restHighLevelClient()
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("127.0.0.1",9200,"http"))
);
return client;
5、创建并编写测试实体类
@Data //生成setter和getter方法
@NoArgsConstructor //生成无参构造函数
@AllArgsConstructor //生成带参构造函数
public class User implements Serializable
private String name;
private Integer age;
1.3、测试索引-增删查
- 首先启动
elasticsearch
和es-head
服务和插件 - 然后要启动项目的主启动类
SpringbootElasticsearchApiApplication
,因为要把RestHighLevelClient
注入到spring容器中,在测试前一定一定要做这一步,后续的测试才不会报错,血的教训!!! - 测试建议写在test包下的
SpringbootElasticsearchApplicationTests
类中
6.1、创建索引
@SpringBootTest
class SpringbootElasticsearchApplicationTests
@Autowired
RestHighLevelClient restHighLevelClient;
@Test
public void testPUTCreateIndex() throws IOException
//创建索引请求对象,同时可初始化索引名
CreateIndexRequest request = new CreateIndexRequest("yxj_index");
//创建索引响应对应,默认类型
CreateIndexResponse reponse = restHighLevelClient.indices().create(request,RequestOptions.DEFAULT);
System.out.println(reponse.isAcknowledged());//根据响应状态,索引是够创建成功
System.out.println(reponse);//查询响应对象信息
restHighLevelClient.close();//用完一定要关闭客户端
控制台结果:
true
org.elasticsearch.client.indices.CreateIndexResponse@5565235d
6.2、获取索引,并判断其是否存在
@Test
public void testGETIndexAndIsExists() throws IOException
//创建获取索引请求对象
GetIndexRequest request = new GetIndexRequest("yxj_index");
//创建获取索引响应对象
GetIndexResponse response = restHighLevelClient.indices().get(request, RequestOptions.DEFAULT);
//判断索引是否存在
boolean exits = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
System.out.println(response.getIndices());//输出索引信息(暂时还没数据)
System.out.println(exits);//判断是否存在
restHighLevelClient.close();//用完一定要关闭客户端
控制台结果:
[Ljava.lang.String;@36790bec
true
6.3、删除索引
@Test
public void testDeleteIndex() throws IOException
//创建删除索引的请求对象
DeleteIndexRequest request = new DeleteIndexRequest("yxj_index");
//创建删除索引的响应对象
AcknowledgedResponse response = restHighLevelClient.indices().delete(request,RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());//判断删除是否成功
restHighLevelClient.close();
控制台结果:
true
1.4、测试文档-增删改查
1、添加文档
/**
* 向索引中添加文档信息
*/
@Test
void testAddDocument() throws IOException
//创建对象
User user = new User("一宿君",21);
//创建请求,链接索引库
IndexRequest request = new IndexRequest("yxj_index");
//规则 PUT /yxj_index/_doc/1
request.id("1");
request.timeout("1s");//设置超时时间为1s
request.timeout(TimeValue.timeValueMinutes(1));//这两种方式应该都可以
//将数据放入request请求中(json格式)
request.source(JSON.toJSONString(user), XContentType.JSON);
//客户端发送请求,获取响应的结果信息
IndexResponse response = restHighLevelClient.index(request,RequestOptions.DEFAULT);
System.out.println(response.status());//获取操作文档的状态
System.out.println(response);//获取文档操作相应信息
restHighLevelClient.close();
控制台结果:
CREATED
IndexResponse[index=yxj_index,type=_doc,id=1,version=1,result=created,seqNo=0,primaryTerm=1,shards="total":2,"successful":1,"failed":0]
2、获取文档信息
/**
* 获取文档信息
*/
@Test
void testGetDocumntAndIsExits() throws IOException
//创建获取文档请求,指定索引名和文档id
GetRequest request = new GetRequest("yxj_index","1");
//过滤掉_source文档上下文,我们只需要判断文档是否存在,不需要获取内容,可以提高效率
//request.fetchSourceContext(new FetchSourceContext(false));
//不获取任何字段
//request.storedFields("_none_");
//获取值钱,先判断该文档是否存在(提高效率)
boolean exists = restHighLevelClient.exists(request, RequestOptions.DEFAULT);
if(exists)
System.out.println("文档存在。。。");
//发送请求获取响应对象(此处发送请求,如果使用上述的request过滤掉上下文,是获取不到内容的,可以把上述过滤注释掉)
GetResponse response = restHighLevelClient.get(request,RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());//获取文档全部内容,转换为字符串
System.out.println(response);//获取全部相应信息(和Kibana的命令操作是一致的)
else
System.out.println("文档不存在!!!");
restHighLevelClient.close();//关闭客户端
控制台结果:
文档存在。。。
"age":21,"name":"一宿君"
"_index":"yxj_index","_type":"_doc","_id":"1","_version":1,"_seq_no":0,"_primary_term":1,"found":true,"_source":"age":21,"name":"一宿君"
3、文档更新
/**
* 文档的更新
*/
@Test
void testUpdateDocument() throws IOException
//创建更新请求
UpdateRequest request = new UpdateRequest("yxj_index","1");
//创建更新数据
User user = new User("一宿君Java",19);
//将数据放入请求中,转换为JSON格式
request.doc(JSON.toJSONString(user),XContentType.JSON);
//发送请求
UpdateResponse response = restHighLevelClient.update(request, RequestOptions.DEFAULT);
System.out.println(response.status());//查询更新状态是否成功
restHighLevelClient.close();//关闭客户端
控制台结果:
OK
4、文档的删除
/**
* 文档的删除
* @throws IOException
*/
@Test
void testDeleteDocument() throws IOException
//创建删除请求
DeleteRequest request = new DeleteRequest("yxj_index", "1");
//发送请求
DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
System.out.println(response.status());//查询更新状态是否成功
restHighLevelClient.close();//关闭客户端
控制台结果:
OK
5、批量插入文档数据
/**
* 批处理插入文档数据
* @throws IOException
*/
@Test
void testBulkInsertDocument() throws IOException
//创建批量出入请求对象
BulkRequest request = new BulkRequest();
request.timeout("1s");
//创建集合文档数据
List<User> userList = new ArrayList<>();
userList.add(new User("一宿君1", 1));
userList.add(new User("一宿君2", 2));
userList.add(new User("一宿君3", 3));
userList.add(new User("一宿君4", 4));
userList.add(new User("一宿君5", 5));
userList.add(new User("一宿君6", 6));
//批量请求处理
for(int i=0;i<userList.size();i++)
request.add(
//此处是新建索引,或者指定已存在索引
new IndexRequest("bulk_index")
//id不指定的话会随机生成
.id(""+(i+1))
.source(JSON.toJSONString(userList.get(i)),XContentType.JSON)
);
//提交请求
BulkResponse response = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.status());//判断批处理插入处理的状态
restHighLevelClient.close();//关闭客户端
控制台结果:
OK
6、文档带条件查询
/**
* 查询
* SearchRequest 搜索请求
* SearchSourceBuilder 条件构造
* HighlightBuilder 高亮
* TermQueryBuilder 精确查询
* MatchAllQueryBuilder 匹配所有条件
* MatchQueryBuilder 匹配指定条件
* xxxQueryBuilder ...
* source : 添加构建条件
* indices :指定查询索引
* @throws IOException
*/
@Test
void testHasConditionSearch() throws IOException
//创建查询条件请求对象
SearchRequest request = new SearchRequest();
//构建查询条件对象
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
/**
* 查询可分为两种:
* match匹配查询
* matchAll:匹配查询所有 MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
* term精确查询
*/
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name","一宿君");
//TermQueryBuilder queryBuilder = QueryBuilders.termQuery("name","一宿君");
//将查询条件对象放入 请求构建查询条件对象中
searchSourceBuilder.query(matchQueryBuilder);
//设置高亮
searchSourceBuilder.highlighter(new HighlightBuilder());
//设置分页(当前第0页,每页显示3条数据)
searchSourceBuilder.from(0);
searchSourceBuilder.size(3);
//将构建查询条件对象放入到请求查询条件对象中
request.source(searchSourceBuilder);
//此处是指定索引,如果不指定会遍历所有的索引
request.indices("bulk_index");
//客户单发送请求
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
System.out.println(response.status());//查看查询的状态
System.out.println(response);//打印全部响应信息
//获取查询结果集,并遍历
SearchHits hits = response.getHits();//此处获取到的是整个hits标签,包含全部信息
System.out.println(JSON.toJSONString(hits));//将结果集转换为JSON格式
System.out.println("============================================================");
//此处的hits内部才是包含数据
for(SearchHit documentFields:hits.getHits())
System.out.println(documentFields.getSourceAsString());//这个是获取字符串格式
//System.out.println(documentFields.getSourceAsMap());//这个是获取map集合对格式
restHighLevelClient.close();//关闭客户端
控制台结果:
OK
"took":19,"timed_out":false,"_shards"以上是关于ElasticSearch分布式搜索引擎从入门到实战应用(实战篇-仿京东首页搜索商品高亮显示)的主要内容,如果未能解决你的问题,请参考以下文章
550Elasticsearch详细入门教程系列 -分布式全文搜索引擎 Elasticsearch 2023.03.31