编辑
2025-10-05
java
00

目录

1.数据聚合
2.自动补全分词
3.数据同步
(1)同步调用
(2)异步通知
(3)监听binlog

1.数据聚合

image.png

DSL实现聚合

image.png

DSL
GET /hotel/_search { "size": 0, "aggs":{ "brandAgg":{ "terms":{ "field": "brand", "size": 20 } } } }

自定义排序规则

image.png

DSL
GET /hotel/_search { "size": 0, "aggs":{ "brandAgg":{ "terms":{ "field": "brand", "size": 20 , "order": { "_count": "asc" } } } } }

限定搜索范围

image.png

DSL
GET /hotel/_search { "query": { "range": { "price": { "lte": 200 } } }, "size": 0, "aggs":{ "brandAgg":{ "terms":{ "field": "brand", "size": 20 , "order": { "_count": "asc" } } } } }

嵌套聚合

image.png

DSL
GET /hotel/_search { "size": 0, "aggs": { "brandAgg": { "terms": { "field": "brand", "size": 20 }, "aggs": { "scoreAgg": { "stats": { "field": "score" } } } } } }

嵌套聚合排序

DSL
GET /hotel/_search { "size": 0, "aggs": { "brandAgg": { "terms": { "field": "brand", "size": 20, "order": { "scoreAgg.avg": "desc" } }, "aggs": { "scoreAgg": { "stats": { "field": "score" } } } } } }

RestClient实现聚合

image.png

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); }

2.自动补全分词

image.png

自定义分词器

image.png

image.png

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中配置的分词器只对当前索引库生效,除非指明索引库

DSL
POST /test/_analyze { "text": ["如家酒店真不错"], "analyzer": "my_analyzer" }

自动补全查询

image.png

自动补全测试

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

image.png

解析结果

image.png

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); } }

3.数据同步

image.png

(1)同步调用

image.png

(2)异步通知

image.png

(3)监听binlog

image.png

总结

image.png

本文作者:钱小杰

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!