【Elasticsearch】06-JavaRestClient查询
1. MatchAll查询与结果解析
public class ElasticQueryTest {
private RestHighLevelClient client;
@BeforeEach
public void init() {
client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://nuaamvp.cn:9200")
));
}
@AfterEach
void tearDown() {
if (client!=null) {
try {
client.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
@Test
void testMatchAll() throws IOException {
// 1. 创建 Request
SearchRequest request = new SearchRequest("items");
// 2. 组织请求参数
request.source().query(
QueryBuilders.matchAllQuery()
);
// 3. 发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4. 解析响应
handleResponse(response);
}
public void handleResponse(SearchResponse response) {
SearchHits searchHits = response.getHits();
// 1. 获取总条数
long total = searchHits.getTotalHits().value;
System.out.println("共搜索到" + total + "条记录");
// 2. 遍历结果数组
SearchHit[] hits = searchHits.getHits();
for(SearchHit hit:hits) {
// 3. 得到原始 json 文档 _source
String source = hit.getSourceAsString();
// 4. 反序列化打印
ItemDoc itemDoc = JSONUtil.toBean(source, ItemDoc.class);
System.out.println(itemDoc);
}
}
}
2. 复合查询
分页、排序。
@Test
void testMatch() throws IOException {
// 1. 创建 Request
SearchRequest request = new SearchRequest("items");
// 2. 组织请求参数
request.source().query(
// QueryBuilders.multiMatchQuery("牛奶", "name", "category")
// QueryBuilders.matchQuery("name", "智能手机")
QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("name", "脱脂牛奶"))
.filter(QueryBuilders.termQuery("brand", "德"))
.filter(QueryBuilders.rangeQuery("price").lt(30000))
);
// 3. 发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4. 解析响应
handleResponse(response);
}
3. 高亮
@Test
void testMatch() throws IOException {
// 0. 前端参数
int pageNo = 1, pageSize = 5;
// 1. 创建 Request
SearchRequest request = new SearchRequest("items");
// 2. 组织请求参数
request.source().query(
// QueryBuilders.multiMatchQuery("牛奶", "name", "category")
// QueryBuilders.matchQuery("name", "智能手机")
QueryBuilders.boolQuery()
.must(QueryBuilders.matchQuery("name", "脱脂牛奶"))
.filter(QueryBuilders.termQuery("brand", "德"))
.filter(QueryBuilders.rangeQuery("price").lt(30000))
);
// 2.1 分页
request.source().from((pageNo-1)*pageSize).size(pageSize);
// 2.2 排序
request.source()
.sort("sold", SortOrder.ASC)
.sort("price", SortOrder.DESC);
// 2.3 高亮
request.source().highlighter(
SearchSourceBuilder.highlight()
.field("name")
.preTags("<em>")
.postTags("</em>")
);
// 3. 发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4. 解析响应
handleResponse(response);
}
将高亮结果解析出来。
public void handleResponse(SearchResponse response) {
SearchHits searchHits = response.getHits();
// 1. 获取总条数
long total = searchHits.getTotalHits().value;
System.out.println("共搜索到" + total + "条记录");
// 2. 遍历结果数组
SearchHit[] hits = searchHits.getHits();
for(SearchHit hit:hits) {
// 3. 得到原始 json 文档 _source
String source = hit.getSourceAsString();
// 4. 反序列化打印
ItemDoc itemDoc = JSONUtil.toBean(source, ItemDoc.class);
// 5. 获取高亮结果
Map<String, HighlightField> hfs = hit.getHighlightFields();
if (CollUtils.isNotEmpty(hfs)) {
// 5.1 获取name的高亮结果
HighlightField hf = hfs.get("name");
if (hf!=null) {
String hfName = hf.getFragments()[0].string();
itemDoc.setName(hfName);
}
}
System.out.println(itemDoc);
}
}