match匹配具體及分詞內(nèi)容
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供綿陽(yáng)企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站建設(shè)、網(wǎng)站制作、H5建站、小程序制作等業(yè)務(wù)。10年已為綿陽(yáng)眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站建設(shè)公司優(yōu)惠進(jìn)行中。
@Test
void testMatch() throws IOException {SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchQuery("all","如家"));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//4、解析響應(yīng)
handleResponse(response);
}
term查詢term具體匹配,不分詞
@Test
void testTerm() throws IOException {SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.termQuery("city","上海"));
SearchResponse response = client.search(request,RequestOptions.DEFAULT);
handleResponse(response);
}
range查詢range范圍查詢
@Test
void testRange() throws IOException {SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.rangeQuery("price").gte(100).lte(2000));
SearchResponse response = client.search(request,RequestOptions.DEFAULT);
handleResponse(response);
}
bool查詢多條件查詢
@Test
void testBoolQuery() throws IOException {SearchRequest request = new SearchRequest("hotel");
//組裝bool條件
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.termQuery("city","上海"));
boolQueryBuilder.must(QueryBuilders.termQuery("brand","如家"));
boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(250));
request.source().query(boolQueryBuilder);
SearchResponse response = client.search(request,RequestOptions.DEFAULT);
handleResponse(response);
page分頁(yè)分頁(yè)和排序
還記得我們mysql的分頁(yè)嗎?
@Test
void pageAndSort() throws IOException {int page = 1,size = 5;
SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchAllQuery());
//排序
request.source().sort("price", SortOrder.ASC);
//分頁(yè)
request.source().from((page-1)*size).size(size);
SearchResponse response = client.search(request,RequestOptions.DEFAULT);
handleResponse(response);
}
高亮顯示被標(biāo)簽包裹會(huì)高亮顯示
我們這里后端處理,當(dāng)然前端也可以
@Test
void testHighlight() throws IOException {SearchRequest request = new SearchRequest("hotel");
request.source().query(QueryBuilders.matchQuery("name","如家"));
request.source().highlighter(new HighlightBuilder()
.field("name").requireFieldMatch(false));
SearchResponse response = client.search(request,RequestOptions.DEFAULT);
handleResponse(response);
}
封裝的response解析方法代碼冗長(zhǎng),將解析單獨(dú)封裝
我的案例解析皆為此方法
private void handleResponse(SearchResponse response) {SearchHits searchHits = response.getHits();
//4.1、獲取總條數(shù)
long total = searchHits.getTotalHits().value;
System.out.println("共搜索到" + total + "條");
//4.2、文檔數(shù)組
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {String json = hit.getSourceAsString();
HotelDoc hotelDoc = JSON.parseObject(json, HotelDoc.class);
//獲取高亮結(jié)果
MaphighlightFields = hit.getHighlightFields();
if(!CollectionUtils.isEmpty(highlightFields)){//根據(jù)字段獲取高亮結(jié)果
HighlightField highlightField = highlightFields.get("name");
//獲取高亮值
String name = highlightField.getFragments()[0].string();
//覆蓋非高亮值
if(name!=null){//判空處理
hotelDoc.setName(name);
}
}
System.out.println("hotelDoc=>" + hotelDoc);
}
}
聚合數(shù)據(jù)
單個(gè)屬性聚合@Test
void testAggregation() throws IOException {//準(zhǔn)備request
SearchRequest request = new SearchRequest("hotel");
//準(zhǔn)備DSL
//設(shè)置size、清除文檔數(shù)據(jù)
request.source().size(0);
//聚合
request.source().aggregation(AggregationBuilders
.terms("brand")
.size(20)
.field("brand")) ;
//發(fā)出請(qǐng)求
SearchResponse response = client.search(request,RequestOptions.DEFAULT);
//解析結(jié)果
Aggregations aggregations = response.getAggregations();
Terms brands = aggregations.get("brand");
List extends Terms.Bucket>buckets = brands.getBuckets();
for (Terms.Bucket bucket: buckets){String key = bucket.getKeyAsString();
System.out.println(key);
}
}
多個(gè)屬性聚合@Test
void testAggregations() throws IOException {//準(zhǔn)備request
SearchRequest request = new SearchRequest("hotel");
//準(zhǔn)備DSL、限定查詢范圍
//設(shè)置size、清除文檔數(shù)據(jù)
request.source().size(0);
//聚合
buildAggregation(request);
//發(fā)出請(qǐng)求
SearchResponse response = client.search(request,RequestOptions.DEFAULT);
//解析結(jié)果
Map>map = new HashMap<>();
Aggregations aggregations = response.getAggregations();
//根據(jù)名稱放入品牌結(jié)果
ListbrandList = getAggByName(aggregations,"brand");
ListcityList = getAggByName(aggregations,"city");
map.put("品牌",brandList);
map.put("city",cityList);
System.out.println(map);
}
private void buildAggregation(SearchRequest request) {request.source().aggregation(AggregationBuilders
.terms("brand")
.size(200)
.field("brand")) ;
request.source().aggregation(AggregationBuilders
.terms("city")
.size(200)
.field("city")) ;
}
private ListgetAggByName(Aggregations aggregations,String aggName) {Terms brands = aggregations.get(aggName);
List extends Terms.Bucket>buckets = brands.getBuckets();
ListbrandList = new ArrayList<>();
for (Terms.Bucket bucket: buckets){String key = bucket.getKeyAsString();
brandList.add(key);
}
return brandList;
}
自動(dòng)補(bǔ)全自動(dòng)補(bǔ)全需要pinyin分詞器,需要在github上自行下載
@Test
void testSuggest() throws IOException {//準(zhǔn)備request
SearchRequest request = new SearchRequest("hotel");
//準(zhǔn)備DSL
request.source().suggest(new SuggestBuilder().addSuggestion(
"suggestions",
//補(bǔ)全的字段
SuggestBuilders.completionSuggestion("suggestion")
.prefix("sh")
.skipDuplicates(true)
.size(10)
));
//發(fā)起請(qǐng)求
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
//解析結(jié)果
Suggest suggest = response.getSuggest();
CompletionSuggestion suggestions = suggest.getSuggestion("suggestions");
Listoptions = suggestions.getOptions();
for(CompletionSuggestion.Entry.Option option :options){String text = option.getText().toString();
System.out.println(text);
}
}
總結(jié)你是否還在尋找穩(wěn)定的海外服務(wù)器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機(jī)房具備T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確流量調(diào)度確保服務(wù)器高可用性,企業(yè)級(jí)服務(wù)器適合批量采購(gòu),新人活動(dòng)首月15元起,快前往官網(wǎng)查看詳情吧