1、查询
查询的基本步骤是:
1.创建SearchRequest对象
2.准备Request.source(),也就是DSL。
QueryBuilders来构建查询条件
传入Request.source()的 query()方法
3.发送请求,得到结果
4.解析结果(参考JSON结果,从外到内,逐层解析)
java和dsl对应关系 response和dsl查询结果对应关系
java
@Test
void testSearchMatchall() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchAllQuery());
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
long value = hits.getTotalHits().value;
System.out.println(value);
SearchHit[] hits1 = hits.getHits();
for (SearchHit hit : hits1) {
String string = hit.getSourceAsString();
//反序列化
HotelDoc hotelDoc = JSON.parseObject(string, HotelDoc.class);
System.out.println(hotelDoc);
}
}
bool查询示例:
java
@Test
void testSearchBoolQuery() throws IOException {
//准备request
SearchRequest request = new SearchRequest("hotel");
//准备boolQuery
BoolQueryBuilder bool = QueryBuilders.boolQuery();
//准备boolQuery中的条件
bool.must(QueryBuilders.termQuery("city","杭州"));
bool.filter(QueryBuilders.rangeQuery("price").lte(250));
request.source().query(bool);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
extracted(response);
}
private static void extracted(SearchResponse response) {
SearchHits hits = response.getHits();
long value = hits.getTotalHits().value;
System.out.println(value);
SearchHit[] hits1 = hits.getHits();
for (SearchHit hit : hits1) {
String string = hit.getSourceAsString();
//反序列化
HotelDoc hotelDoc = JSON.parseObject(string, HotelDoc.class);
System.out.println(hotelDoc);
}
}
要构建查询条件,只要记住一个类:QueryBuilders
2、排序和分页
java
@Test
void testSearchSort() throws IOException {
//准备request
SearchRequest request = new SearchRequest("hotel");
//准备query,所有数据
request.source().query(QueryBuilders.matchAllQuery());
//设置分页,从0开始,5条数据
request.source().from(0).size(5);
//设置排序,升序
request.source().sort("price", SortOrder.ASC);
//发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//解析结果
extracted(response);
}
ps:一如dsl语句中一般,query字段和from、size、sort字段平级,因此都是在source()之后设置
3、高亮
java
@Test
void testSearchHightlight() throws IOException {
//准备request
SearchRequest request = new SearchRequest("hotel");
//准备query,所有数据
request.source().query(QueryBuilders.matchQuery("all","如家"));
//准备hightlight字段
request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
//发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//解析结果
highlightextracted(response);
}
private static void highlightextracted(SearchResponse response) {
SearchHits hits = response.getHits();
long value = hits.getTotalHits().value;
System.out.println(value);
SearchHit[] hits1 = hits.getHits();
for (SearchHit hit : hits1) {
String string = hit.getSourceAsString();
//反序列化
HotelDoc hotelDoc = JSON.parseObject(string, 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);
}
}
所有搜索DSL的构建,记住一个API:
SearchRequest的source()方法。
高亮结果解析是参考JSON结果,逐层解析