个人名片:
博主:酒徒ᝰ.
个人简介:沉醉在酒中,借着一股酒劲,去拼搏一个未来。
本篇励志:三人行,必有我师焉。
本项目基于B站黑马程序员Java《SpringCloud微服务技术栈》,SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式
【SpringCloud+RabbitMQ+Docker+Redis+搜索+分布式,系统详解springcloud微服务技术栈课程|黑马程序员Java微服务】 点击观看
Elasticsearch中的RestClient是用于发送HTTP请求的客户端。
RestClient可用于创建索引、获取索引、删除索引、插入/更新文档、删除文档、搜索文档等操作。
RestClient的使用效果:
- 可以在Java应用程序中实现Elasticsearch的各种功能,例如创建索引、获取索引、删除索引、插入/更新文档、删除文档、搜索文档等。
- 可以实现高可用性、高扩展性的应用程序,通过RestClient可以方便地实现集群管理和负载均衡。
- 可以实现更快速的数据检索和查询,提高查询效率,减少查询时间。
- 可以实现更安全的数据检索和查询,通过身份验证和权限控制等机制,保护数据的机密性和完整性。
- 可以实现更灵活的数据检索和查询,支持多种查询方式和过滤器,可以根据需求定制化查询结果。
初始化:
@SpringBootTest
public class DSLSearchTest {
@Autowired
private RestHighLevelClient client;
@BeforeEach
void setup(){
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.179.128:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
查询结果解析:
private void handleResponse(SearchResponse response) {
SearchHits hits = response.getHits();
long total = hits.getTotalHits().value;
System.out.println("数据总条数:" + total + "条");
SearchHit[] hitsHits = hits.getHits();
for (SearchHit hit : hitsHits) {
String json = hit.getSourceAsString();
HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
if (!CollectionUtils.isEmpty(highlightFields)) {
HighlightField highlightField = highlightFields.get("name");
if (highlightField != null) {
String name = highlightField.getFragments()[0].string();
hotelDoc.setName(name);
}
}
System.out.println(hotelDoc);
}
}
@Test
void testMathAll() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchAllQuery());
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
@Test
void testMath() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchQuery("all", "如家"));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
@Test
void testMultiMath() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.multiMatchQuery("如家", "brand", "name", "business"));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
@Test
void testTerm() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.termQuery("city", "上海"));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
@Test
void testRange() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.rangeQuery("price").gte(100).lte(150));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
@Test
void testBool() throws IOException {
SearchRequest request = new SearchRequest("hotel");
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("city", "上海"))
.should(QueryBuilders.termQuery("brand", "皇冠假日"))
.should(QueryBuilders.termQuery("brand", "华美达"))
.mustNot(QueryBuilders.rangeQuery("price").lte(500))
.filter(QueryBuilders.rangeQuery("score").gte(45));
request.source().query(boolQuery);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
@Test
void testPageAndSort() throws IOException {
int page = 1, size = 5;
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchAllQuery())
.from((page - 1) * size).size(size)
.sort("price", SortOrder.DESC);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}
@Test
void testHighLight() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchQuery("all", "如家"))
.highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
handleResponse(response);
}