RestClient查询文档
- 快速入门
- match查询
- 精确查询
- 复合查询
- 排序、分页、高亮
一、快速入门
Java代码:
@SpringBootTest
class HotelISearchTest {
private RestHighLevelClient client;
@BeforeEach
public void contextLoads() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.30.125:9200")));
}
/**
* 使用matchAllQuery(): 查询 hotel 索引库里的 全部数据
* @throws IOException
*/
@Test
public void testMatchAll() throws IOException {
// 1.准备Request
SearchRequest request = new SearchRequest("hotel");
// 2.准备DSL
request.source().query(QueryBuilders.matchAllQuery());
// 3.发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4.解析结果
SearchHits searchHits = response.getHits();
// 4.1 查询总条数
long total = searchHits.getTotalHits().value;
System.out.println("共搜索到" + total + "数据。");
// 4.2 查询结果数组
SearchHit[] hits = searchHits.getHits();
// 遍历结果数组
for (SearchHit hit : hits) {
// 4.3 读取文档source
String json = hit.getSourceAsString();
// 反序列化
HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
// 4.4 打印
System.out.println(hotelDoc);
}
}
@AfterEach
public void tearDown() throws IOException {
this.client.close();
}
}
二、match查询
全文检索的match和multi_match查询与match_all的API基本一致。差别是查询条件,也就是query的部分。
同样是利用QueryBuilders提供的方法:
三、精确查询
四、复合查询 -boolean query
总结
要构建查询条件,只要记住一个类:QueryBuilders
五、排序、分页、高亮
1.排序、分页
2.高亮
2.1 高亮API包括请求DSL构建和结果解析两部分。我们先看请求的DSL构建:
2.2 结果解析
Java代码演示:
@SpringBootTest
class HotelISearchTest {
private RestHighLevelClient client;
@BeforeEach
public void contextLoads() {
this.client = new RestHighLevelClient(RestClient.builder(HttpHost.create("http://192.168.30.125:9200")));
}
/**
* 一、快速入门
* 使用matchAllQuery(): 查询 hotel 索引库里的 全部数据
* @throws IOException
*/
@Test
public void testMatchAll() throws IOException {
// 1.准备Request
SearchRequest request = new SearchRequest("hotel");
// 2.准备DSL
request.source().query(QueryBuilders.matchAllQuery());
// 3.发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4.解析结果
handleResponse(response);
}
/**
* 二、Match 查询
* 全文检索查询:
* Match 查询 copy 字段all 中所有字段中带如家的数据
*/
@Test
public void testMatch() throws IOException {
// 1.准备Request
SearchRequest request = new SearchRequest("hotel");
// 2.准备DSL
request.source().query(QueryBuilders.matchQuery("all", "如家"));
// 3.发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4.解析结果
handleResponse(response);
}
/**
* 三、精确查询(term、range)
* 四、复合查询 - boolean query
* 精确查询常见的有term查询和range查询,同样利用QueryBuilders实现:
*/
@Test
public void testBool() throws IOException {
// 1.准备Request
SearchRequest request = new SearchRequest("hotel");
// 2.准备DSL
// 2.1 准备BooleanQuery
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
// 2.2 添加term
boolQuery.must(QueryBuilders.termQuery("city", "北京"));
// 2.3 添加range
boolQuery.filter(QueryBuilders.rangeQuery("price").lte(250));
request.source().query(boolQuery);
// 3.发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4.解析结果
handleResponse(response);
}
/**
* 五、排序、分页
* 全文检索查询:
* Match 查询 copy 字段all 中所有字段中带如家的数据
*/
@Test
public void testPageAndSort() throws IOException {
int page = 2, size = 5;
// 1.准备Request
SearchRequest request = new SearchRequest("hotel");
// 2.准备DSL
request.source().query(QueryBuilders.matchQuery("all", "如家"));
// 2.1 排序 sort
request.source().sort("price", SortOrder.ASC);
// 2.2 分页 from、size
request.source().from((page - 1) * size).size(5);
// 3.发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4.解析结果
handleResponse(response);
}
/**
* 五、高亮
* 全文检索查询:
* Match 查询 copy 字段all 中所有字段中带如家的数据 并 将 name字段 中 如家 两个字高亮显示
*/
@Test
public void testHighlight() throws IOException {
// 1.准备Request
SearchRequest request = new SearchRequest("hotel");
// 2.准备DSL
request.source().query(QueryBuilders.matchQuery("all", "如家"));
// 2.1 高亮
request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
// 3.发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
// 4.解析结果
handleResponse(response);
}
private void handleResponse(SearchResponse response) {
SearchHits searchHits = response.getHits();
// 4.1 查询总条数
long total = searchHits.getTotalHits().value;
System.out.println("共搜索到" + total + "数据。");
// 4.2 查询结果数组
SearchHit[] hits = searchHits.getHits();
// 遍历结果数组
for (SearchHit hit : hits) {
// 4.3 读取文档source
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);
}
}
// 4.4 打印
System.out.println(hotelDoc);
}
}
@AfterEach
public void tearDown() throws IOException {
this.client.close();
}
}