DSL实现聚合
DSLGET /hotel/_search { "size": 0, "aggs":{ "brandAgg":{ "terms":{ "field": "brand", "size": 20 } } } }
自定义排序规则
DSLGET /hotel/_search { "size": 0, "aggs":{ "brandAgg":{ "terms":{ "field": "brand", "size": 20 , "order": { "_count": "asc" } } } } }
限定搜索范围
DSLGET /hotel/_search { "query": { "range": { "price": { "lte": 200 } } }, "size": 0, "aggs":{ "brandAgg":{ "terms":{ "field": "brand", "size": 20 , "order": { "_count": "asc" } } } } }
嵌套聚合
DSLGET /hotel/_search { "size": 0, "aggs": { "brandAgg": { "terms": { "field": "brand", "size": 20 }, "aggs": { "scoreAgg": { "stats": { "field": "score" } } } } } }
嵌套聚合排序
DSLGET /hotel/_search { "size": 0, "aggs": { "brandAgg": { "terms": { "field": "brand", "size": 20, "order": { "scoreAgg.avg": "desc" } }, "aggs": { "scoreAgg": { "stats": { "field": "score" } } } } } }
RestClient实现聚合
java @Test
public void testClientJuHe() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().size(0);
request.source().aggregation(
AggregationBuilders.terms("brand_agg")
.field("brand")
.size(20)
);
SearchResponse response = client.search(request,RequestOptions.DEFAULT);
System.out.println(response);
}
自定义分词器
DSL// 自定义拼音分词器 PUT /test { "settings": { "analysis": { "analyzer": { "my_analyzer": { "tokenizer": "ik_max_word", "filter": "py" } }, "filter": { "py": { "type": "pinyin", "keep_full_pinyin": false, "keep_joined_full_pinyin": true, "keep_original": true, "limit_first_letter_length": 16, "remove_duplicated_term": true, "none_chinese_pinyin_tokenize": false } } } }, "mappings": { "properties": { "name":{ "type":"text", "analyzer":"my_analyzer" } } } }
settings中配置的分词器只对当前索引库生效,除非指明索引库
DSLPOST /test/_analyze { "text": ["如家酒店真不错"], "analyzer": "my_analyzer" }
自动补全查询
自动补全测试
DSL// 自动补全的索引库 PUT test { "mappings": { "properties": { "title":{ "type": "completion" } } } } // 示例数据 POST test/_doc { "title": ["Sony", "WH-1000XM3"] } POST test/_doc { "title": ["SK-II", "PITERA"] } POST test/_doc { "title": ["Nintendo", "switch"] } // 自动补全查询 POST /test/_search { "suggest": { "title_suggest": { "text": "s", // 关键字 "completion": { "field": "title", // 补全字段 "skip_duplicates": true, // 跳过重复的 "size": 10 // 获取前10条结果 } } } }
实现酒店项目自动补全功能
DSL// 酒店数据索引库 PUT /hotel { "settings": { "analysis": { "analyzer": { "text_anlyzer": { "tokenizer": "ik_max_word", "filter": "py" }, "completion_analyzer": { "tokenizer": "keyword", "filter": "py" } }, "filter": { "py": { "type": "pinyin", "keep_full_pinyin": false, "keep_joined_full_pinyin": true, "keep_original": true, "limit_first_letter_length": 16, "remove_duplicated_term": true, "none_chinese_pinyin_tokenize": false } } } }, "mappings": { "properties": { "id":{ "type": "keyword" }, "name":{ "type": "text", "analyzer": "text_anlyzer", "search_analyzer": "ik_smart", "copy_to": "all" }, "address":{ "type": "keyword", "index": false }, "price":{ "type": "integer" }, "score":{ "type": "integer" }, "brand":{ "type": "keyword", "copy_to": "all" }, "city":{ "type": "keyword" }, "starName":{ "type": "keyword" }, "business":{ "type": "keyword", "copy_to": "all" }, "location":{ "type": "geo_point" }, "pic":{ "type": "keyword", "index": false }, "all":{ "type": "text", "analyzer": "text_anlyzer", "search_analyzer": "ik_smart" }, "suggestion":{ "type": "completion", "analyzer": "completion_analyzer" } } } }
自动补全的restAPI
解析结果
java @Test
public void testSuggestions() throws IOException {
SearchRequest request = new SearchRequest("hotel");
request.source().suggest(
new SuggestBuilder().addSuggestion(
"suggestions",
SuggestBuilders.completionSuggestion("suggestion")
.prefix("hp")
.skipDuplicates(true)
.size(10)
)
);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
Suggest suggest = response.getSuggest();
CompletionSuggestion completionSuggestion= suggest.getSuggestion("suggestions");
List<CompletionSuggestion.Entry.Option> options = completionSuggestion.getOptions();
for (CompletionSuggestion.Entry.Option option : options) {
String s = option.getText().toString();
System.out.println(s);
}
}
实现酒店的自动补全
java@Override
public List<String> suggestion(String key) {
SearchRequest request = new SearchRequest("hotel");
request.source().suggest(
new SuggestBuilder().addSuggestion(
"suggestions",
SuggestBuilders.completionSuggestion("suggestion")
.prefix(key)
.skipDuplicates(true)
.size(10)
)
);
try {
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
Suggest suggest = response.getSuggest();
CompletionSuggestion completionSuggestion= suggest.getSuggestion("suggestions");
List<CompletionSuggestion.Entry.Option> options = completionSuggestion.getOptions();
List<String> result = new ArrayList<>();
for (CompletionSuggestion.Entry.Option option : options) {
String s = option.getText().toString();
result.add(s);
}
return result;
} catch (IOException e) {
throw new RuntimeException(e);
}
}
总结
本文作者:钱小杰
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!