通过match_all来演示下基本的API,先看请求DSL的组织:
返回结果结构 (解析结果———>参考JSON结果,从外到内,逐层解析))
@Test
void testMatchAll() throws IOException {
// ...略
// 4、解析响应结果
SearchHits searchHits = response.getHits();
// 4.1 查询的总条数
TotalHits total = searchHits.getTotalHits();
System.out.println("共搜索到"+total+"条数据");
// 4.2 查询的结果数组
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
// 4.3 得到source
String json = hit.getSourceAsString();
// 4.4 打印
System.err.println(json);
}
}
RestAPI中其中构建DSL是通过HighLevelRestClient中的resource()来实现的,其中包含了查询、排序、分页、高亮等所有功能
RestAPI中其中构建查询条件的核心部分是由一个名为QueryBuilders的工具类提供的,其中包含了各种查询方法:
对文档全部查询的实现:
// match_all查询
@Test
void testMatchAll() throws IOException {
// 1、创建SearchRequest对象
SearchRequest request=new SearchRequest("hotel");
/*2、准备Request.source(),也就是DSL
① QueryBuilders来构建查询条件
② 传入Request.Source()的query()方法*/
request.source().query(QueryBuilders.matchAllQuery());
// 3、发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4、解析响应结果
SearchHits searchHits = response.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
System.err.println(hotelDoc);
}
}
查询成功!!!
全文检索的match和multi_match查询与match_all的API基本一致。差别是查询条件,也就是query的部分,同样是利用QueryBuilders提供的方法。
// match查询
request.source().query(QueryBuilders.matchQuery("all","如家"));
// multi_match查询
request.source().query(QueryBuilders.multiMatchQuery("如家","name","business"));
在查询时整体流程不变,变化的只有查询的方式,因此我们可以将不变的API进行抽取此处演示对结果解析的抽取
选中要抽取的快捷键,使用 Ctrl+Alt+m 进行抽取
// math查询
@Test
void testMatchAll() throws IOException {
// 1、创建SearchRequest对象
SearchRequest request=new SearchRequest("hotel");
//2、准备DSL
request.source().query(QueryBuilders.matchQuery("all","如家"));
// 3、发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4、解析响应结果
handleResponse(response);
}
private void handleResponse(SearchResponse response) {
SearchHits searchHits = response.getHits();
TotalHits total = searchHits.getTotalHits();
System.out.println("共搜索到"+total+"条数据");
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String json = hit.getSourceAsString();
HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
System.err.println(hotelDoc);
}
}
精确查询常见的有term查询和range查询,同样利用QueryBuilders实现:
request.source().query(QueryBuilders.termQuery("city","江苏"));
request.source().query(QueryBuilders.rangeQuery("price").gte(100).lte(150));
复合查询-boolean query,同样利用QueryBuilders实现:
// 创建布尔查询
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 添加must条件
boolQuery.must(QueryBuilders.termQuery("city","杭州"));
// 添加filter条件
boolQuery.filter(QueryBuilders.rangeQuery("price").lte(300));
@Test
void testBool() throws IOException {
// 1、创建SearchRequest对象
SearchRequest request=new SearchRequest("hotel");
//2、准备DSL
// 创建布尔查询
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 添加must条件
boolQuery.must(QueryBuilders.termQuery("city","杭州"));
// 添加filter条件
boolQuery.filter(QueryBuilders.rangeQuery("price").lte(300));
request.source().query(boolQuery);
// 3、发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4、解析响应结果
handleResponse(response);
}
要构建查询条件,只需记住一个类:QueryBuilders
搜索结果的排序和分页是与query同级的参数,对应的API如下:
request.source().query(QueryBuilders.matchAllQuery());
// 分页
request.source().from(0).size(10);
// 价格排序
request.source().sort("price", SortOrder.ASC);
@Test
void testBool() throws IOException {
// 页码,每页大小
int page=1,size=5;
// 1、创建SearchRequest对象
SearchRequest request=new SearchRequest("hotel");
//2、准备DSL
request.source().query(QueryBuilders.matchAllQuery());
// 分页 form、size
request.source().from((page-1)*size).size(5);
// 价格排序 sort
request.source().sort("price", SortOrder.ASC);
// 3、发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4、解析响应结果
handleResponse(response);
}
高亮API包括请求DSL构建和结果解析两部分。请求的DSL构建:
request.source().highlighter(new HighlightBuilder()
.field("name")
// 是否要与查询字段匹配
.requireFieldMatch(false)
) ;
@Test
void testHighlight() throws IOException {
// 1、创建SearchRequest对象
SearchRequest request=new SearchRequest("hotel");
//2、准备DS
request.source().query(QueryBuilders.matchQuery("all","如家"));
// 高亮
request.source().highlighter(new HighlightBuilder()
.field("name")
// 是否要与查询字段匹配
.requireFieldMatch(false)
) ;
// 3、发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4、解析响应结果
handleResponse(response);
}
高亮的结果处理相对较麻烦高亮结果解析是参考JSON结果,逐层解析
private void handleResponse(SearchResponse response) {
// 4、解析响应结果
SearchHits searchHits = response.getHits();
TotalHits total = searchHits.getTotalHits();
System.out.println("共搜索到"+total+"条数据");
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
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);
}
}
}