一、MatchALL查询
@Test
void testMatchAll() throws IOException {
// 准备Request对象
SearchRequest request = new SearchRequest("hotel");
// 准备DSL
request.source().query(QueryBuilders.matchAllQuery());
// 发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 解析响应
SearchHits searchHits = response.getHits();
// 获取总条数
long value = searchHits.getTotalHits().value;
System.out.println("共搜索到"+value+"条数据!");
// 获取文档数组
SearchHit[] hits = searchHits.getHits();
// 遍历
for (SearchHit hit : hits) {
// 获取文档source
String json = hit.getSourceAsString();
// 反序列化
HotelDoc object = JSON.parseObject(json, HotelDoc.class);
System.out.println("object = " + object);
}
}
二、Match查询
@Test
void testMatch() throws IOException {
// 准备Request对象
SearchRequest request = new SearchRequest("hotel");
// 准备DSL
request.source().query(QueryBuilders.matchQuery("name","连锁"));
// 发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 解析响应
SearchHits searchHits = response.getHits();
// 获取总条数
long value = searchHits.getTotalHits().value;
System.out.println("共搜索到"+value+"条数据!");
// 获取文档数组
SearchHit[] hits = searchHits.getHits();
// 遍历
for (SearchHit hit : hits) {
// 获取文档source
String json = hit.getSourceAsString();
// 反序列化
HotelDoc object = JSON.parseObject(json, HotelDoc.class);
System.out.println("object = " + object);
}
}
三、bool查询
@Test
void testBool() throws IOException {
// 准备Request对象
SearchRequest request = new SearchRequest("hotel");
// 准备DSL
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("city","杭州"));
boolQuery.filter(QueryBuilders.rangeQuery("price").lte(250));
request.source().query(boolQuery);
// 发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
extracted(response);
}
四、排序和分页
@Test
void testMatchAll() throws IOException {
// 准备Request对象
SearchRequest request = new SearchRequest("hotel");
// 准备DSL
request.source().query(QueryBuilders.matchAllQuery());
// 分页
request.source().from(0).size(5);
// 排序
request.source().sort("price", SortOrder.ASC);
// 发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 解析响应
extracted(response);
}
五、高亮
@Test
void testHighLight() throws IOException {
// 准备Request对象
SearchRequest request = new SearchRequest("hotel");
// 准备DSL
request.source().query(QueryBuilders.matchQuery("name", "连锁"));
request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
// 发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
extracted(response);
}
private void extracted(SearchResponse response) {
// 解析响应
SearchHits searchHits = response.getHits();
// 获取总条数
long value = searchHits.getTotalHits().value;
System.out.println("共搜索到" + value + "条数据!");
// 获取文档数组
SearchHit[] hits = searchHits.getHits();
// 遍历
for (SearchHit hit : hits) {
// 获取文档source
String json = hit.getSourceAsString();
// 反序列化
HotelDoc object = JSON.parseObject(json, HotelDoc.class);
// 获取高亮结果
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
if (!CollectionUtils.isEmpty(highlightFields)){
// 根据字段名获取高亮结果
HighlightField highlightField = highlightFields.get("name");
if (highlightFields != null){
// 获取高亮值
String name = highlightField.getFragments()[0].string();
// 覆盖非高亮结果
object.setName(name);
}
}
System.out.println("object = " + object);
}
}
原文地址:https://blog.csdn.net/icbbm/article/details/134628779
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.7code.cn/show_10385.html
如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。