Elastic Search提供的Java语言客户端RestClient基本语法
- 全文检索
- Boolean
- 分页和排序
- 高亮
RestClient常见的几种命令
package cn.itcast.hotel;
import cn.itcast.hotel.pojo.HotelDoc;
import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.util.CollectionUtils;
import java.io.IOException;
import java.util.Map;
public class HotelSearchTest {
private RestHighLevelClient client;
// 处理发送请求的方法
private void extracted(SearchResponse response) {
// 4 结果的解析
SearchHits searchHits = response.getHits();
// 获取命中的结果数
TotalHits totalHits = searchHits.getTotalHits();
System.out.println("共命中" + totalHits + "条数据");
// 获取命中的结果
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);
}
}
System.out.println("hotelDoc >>> " + hotelDoc);
}
}
// 全文匹配
@Test
void testMatchAll() throws IOException {
// 1 准备请求
SearchRequest request = new SearchRequest("hotel");
// 2 准备查询参数
request.source().query(QueryBuilders.matchAllQuery());
// 3 发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
extracted(response);
}
// Match匹配
@Test
void testMatch() throws IOException {
// 1 准备请求
SearchRequest request = new SearchRequest("hotel2");
// 2 准备查询参数
request.source().query(QueryBuilders.matchQuery("all", "如家"));
// 3 发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
extracted(response);
}
// Bool匹配must filter
@Test
void testBool() throws IOException {
// 1 准备请求
SearchRequest request = new SearchRequest("hotel2");
// 2 准备查询参数
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
boolQuery.must(QueryBuilders.termQuery("city", "北京"));
boolQuery.filter(QueryBuilders.rangeQuery("price").lte(500));
request.source().query(boolQuery);
// 3 发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
extracted(response);
}
// 分页和排序
@Test
void testPageAndSort() throws IOException {
int page = 2;
int size = 5;
// 1 准备请求
SearchRequest request = new SearchRequest("hotel2");
// 2 准备查询参数
// 2.1 query
request.source().query(QueryBuilders.matchAllQuery());
// 2.2 sort
request.source().sort("price", SortOrder.ASC);
// 2.3 分页
request.source().from((page - 1) * size).size(5);
// 3 发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
extracted(response);
}
// 高亮处理
@Test
void testHighLight() throws IOException {
// 1 准备请求
SearchRequest request = new SearchRequest("hotel2");
// 2 准备查询参数
// 2.1 query
request.source().query(QueryBuilders.matchQuery("all", "如家"));
// 2.2 highLight
request.source().highlighter(new HighlightBuilder().field("name").requireFieldMatch(false));
// 3 发送请求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
extracted(response);
}
@BeforeEach
void setUp() {
this.client = new RestHighLevelClient(RestClient.builder(
HttpHost.create("http://192.168.17.136:9200")
));
}
@AfterEach
void tearDown() throws IOException {
this.client.close();
}
}