ES - RestClient 查询文档
快速入门
@SpringBootTest
class SearchfDocTest {
private RestHighLevelClient client;
//查询所有文档
@Test
public void testMatchAll() throws IOException {
//1. 准备请求对象
SearchRequest searchRequest = new SearchRequest("hotel");
//2. 准备DSL
searchRequest.source().query(QueryBuilders.matchAllQuery());
//3. 发送请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//4. 解析响应解锁
SearchHits hits = response.getHits();
//4.1 获取搜索到的总条数
long value = hits.getTotalHits().value;
System.out.println("共搜索到" + value + "条数据");
SearchHit[] hitsArr = hits.getHits();
for (SearchHit hit : hits) {
//获取文档soure
String sourceAsString = hit.getSourceAsString();
//反序列化
HotelDoc hotel = JSON.parseObject(sourceAsString, HotelDoc.class);
System.out.println(hotel);
}
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.184.152:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}
match 查询
@Test //match 查询
public void testMatch() throws IOException {
//1. 准备请求对象
SearchRequest searchRequest = new SearchRequest("hotel");
//2. 准备DSL
searchRequest.source().query(QueryBuilders.matchQuery("brand","如家"));
//3. 发送请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
handResponse(response);
}
boolean 查询
@Test //booleanm 查询
public void testBooleanQuery() throws IOException {
//1. 准备请求对象
SearchRequest searchRequest = new SearchRequest("hotel");
//2. 准备DSL
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termQuery("name","如家"));
boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").lte(250));
searchRequest.source().query(boolQueryBuilder);
//3. 发送请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
handResponse(response);
}
排序和分页 查询
@Test //排序和分页 查询
public void testPageAndSort() throws IOException {
//1. 准备请求对象
SearchRequest searchRequest = new SearchRequest("hotel");
//2. 准备DSL
searchRequest.source().query(QueryBuilders.matchAllQuery());
//排序
searchRequest.source().sort("price", SortOrder.ASC);
//分页
searchRequest.source().from(0).size(10);
//3. 发送请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
handResponse(response);
}
高亮处理
@Test //高亮处理
public void testHighLightQuery() throws IOException {
//1. 准备请求对象
SearchRequest searchRequest = new SearchRequest("hotel");
//2. 准备DSL
searchRequest.source().query(QueryBuilders.matchQuery("all","如家"));
// 处理高亮, 将name中包含 如家的 高亮显示
searchRequest.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
//3. 发送请求
SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
handResponse(response);
}
private void handResponse(SearchResponse response) {
//4. 解析响应结果
SearchHits hits = response.getHits();
//4.1 获取搜索到的总条数
long value = hits.getTotalHits().value;
System.out.println("共搜索到" + value + "条数据");
SearchHit[] hitsArr = hits.getHits();
for (SearchHit hit : hits) {
//获取文档soure
String sourceAsString = hit.getSourceAsString();
//反序列化
HotelDoc hotel = JSON.parseObject(sourceAsString, HotelDoc.class);
Map<String, HighlightField> highlightFields = hit.getHighlightFields();;
String s = Optional.ofNullable(highlightFields)
.map(x -> x.get("name"))
.map(y -> y.getFragments()[0])
.map(z -> z.toString())
.orElse("存在null");
hotel.setName(s);
System.out.println(hotel);
}
}
结果:
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/17738170.html