SpringCloud分布式搜索引擎、数据聚合、ES和MQ的结合使用、ES集群的问题

目录

数据聚合

聚合的分类

​编辑 DSL实现Bucket聚合

​编辑

 DSL实现Metrics聚合​编辑

RestAPI实现聚合

 对接前端接口​编辑

 自定义分词器​编辑

Completion suggester查询

Completion suggester查询

酒店数据自动补全

实现酒店搜索框界面输入框的自动补全

 数据同步问题分析​编辑

同步问题分析

数据同步问题分析

数据问题分析

利用 MQ实现mysql与elasticearch数据同步

ES集群机构

ES集群的节点角色

ES集群的脑裂

ES集群的分布式存储

ES集群的分布式存储

ES集群的分布式查询

ES集群的故障转移


数据聚合

 

聚合的分类

 DSL实现Bucket聚合

#聚合功能
GET /hotel/_search
{"size": 0,"aggs": {"brandAgg": {"terms": {"field": "brand","size": 20}}}
}#聚合功能,自定义排序规则
GET /hotel/_search
{"size": 0,"aggs": {"brandAgg": {"terms": {"field": "brand","size": 20,"order": {"_count": "asc"}}}}
}#聚合功能,限定聚合范围
GET /hotel/_search
{"query": {"range": {"price": {"lte": 200}}}, "size": 0,"aggs": {"brandAgg": {"terms": {"field": "brand","size": 20}}}
}

 DSL实现Metrics聚合

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

RestAPI实现聚合

@Overridepublic Map<String, List<String>> filters() {try {//1.准备RequestSearchRequest request = new SearchRequest("hotel");//2.准备DSL//2.1设置sizerequest.source().size(0);//2.2 聚合buildAggregation(request);//3.发出请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);//4.解析结果Map<String, List<String>> result = new HashMap<>();Aggregations aggregations = response.getAggregations();//TODO 根据品牌名称,获取品牌结果List<String> brandList = getAggByName(aggregations,"brandAgg");result.put("brand",brandList);//TODO 根据品牌名称,获取品牌结果List<String> cityList = getAggByName(aggregations,"cityAgg");result.put("city",cityList);//TODO 根据品牌名称,获取品牌结果List<String> starList = getAggByName(aggregations,"starAgg");result.put("starName",starList);return result;}catch (IOException e) {throw new RuntimeException(e);}}private List<String> getAggByName(Aggregations aggregations,String aggName) {//4.1根据聚合名称获取聚合结果Terms brandTerms = aggregations.get(aggName);//4.2获取bucketsList<? extends Terms.Bucket> buckets = brandTerms.getBuckets();//4.3遍历List<String> brandList = new ArrayList<>();for ( Terms.Bucket bucket : buckets ) {String key = bucket.getKeyAsString();
//            System.out.println(key);brandList.add(key);}return brandList;}

 对接前端接口

 

POST /_analyze
{"text": ["如家酒店还不错"],"analyzer": "pinyin"}

 自定义分词器

POST /test2/_analyze
{"text": ["如家酒店还不错"],"analyzer": "my_analyzer"}// 自定义拼音分词器
PUT /test2
{"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"}}}
}

Completion suggester查询

Completion suggester查询

 

// 自动补全的索引库
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条结果}}}
}

酒店数据自动补全


#酒店数据索引库
GET /hotel/_mapping
DELETE /hotel
#酒店数据索引库
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"}}}
}GET /hotel/_search
{"query": {"match_all": {}}
}GET /hotel/_search
{"suggest": {"suggestions": {"text":"sd","completion":{"field":"suggestion","skip_duplicates": true, "size": 10 }}}
}

RestAPI实现自动补全

 

 

@Testvoid testSuggest() throws IOException {//1、准备RequestSearchRequest request = new SearchRequest("hotel");//2.准备DSLrequest.source().suggest(new SuggestBuilder().addSuggestion("suggestions",SuggestBuilders.completionSuggestion("suggestion").prefix("hz").skipDuplicates(true).size(10)));//3、发起请求SearchResponse response = client.search(request, RequestOptions.DEFAULT);//4、解析结果Suggest suggest = response.getSuggest();//4.1根据补全查询名称,获取补全结果CompletionSuggestion suggestions = suggest.getSuggestion("suggestions");///4.2获取optionsList<CompletionSuggestion.Entry.Option> options = suggestions.getOptions();//4.3遍历for ( CompletionSuggestion.Entry.Option option : options ) {String text = option.getText().toString();System.out.println("text = " + text);}}

实现酒店搜索框界面输入框的自动补全

 数据同步问题分析

同步问题分析

数据同步问题分析

数据问题分析

利用 MQ实现mysql与elasticearch数据同步

 


@Configuration
public class MqConfig {@Beanpublic TopicExchange topicExchange(){return new TopicExchange(MqConstants.HOTEL_EXCHANGE,true,false);}@Beanpublic Queue insertQueue(){return new Queue(MqConstants.HOTEL_INSERT_QUEUE,true);}@Beanpublic Queue deleteQueue(){return new Queue(MqConstants.HOTEL_DELETE_QUEUE,true);}@Beanpublic Binding insertQueueBinding(){return BindingBuilder.bind(insertQueue()).to(topicExchange()).with(MqConstants.HOTEL_INSERT_KEY);}@Beanpublic Binding deleteQueueBinding(){return BindingBuilder.bind(deleteQueue()).to(topicExchange()).with(MqConstants.HOTEL_DELETE_KEY);}}

@Component
public class HotelListener {@Autowiredprivate IHotelService hotelService;/*** 监听酒店新增或修改的业务* @param id 酒店id*/@RabbitListener(queues = MqConstants.HOTEL_INSERT_QUEUE)public void listenHotelInsertOrUpdate(Long id){hotelService.insertById(id);}/*** 监听酒店删除业务* @param id 酒店id*/@RabbitListener(queues = MqConstants.HOTEL_DELETE_QUEUE)public void listenHotelDelete(Long id){hotelService.deleteById(id);}
}
   @PutMapping()public void updateById(@RequestBody Hotel hotel){if (hotel.getId() == null) {throw new InvalidParameterException("id不能为空");}hotelService.updateById(hotel);rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_INSERT_KEY,hotel.getId());}@DeleteMapping("/{id}")public void deleteById(@PathVariable("id") Long id) {hotelService.removeById(id);rabbitTemplate.convertAndSend(MqConstants.HOTEL_EXCHANGE,MqConstants.HOTEL_DELETE_KEY,id);}

ES集群机构

ES集群的节点角色

ES集群的分布式查询

ES集群的脑裂

ES集群的分布式存储

ES集群的分布式存储

ES集群的分布式查询

ES集群的故障转移

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/23193.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

ASO优化之在海外如何进行竞品分析

进行竞争对手研究&#xff0c;需要分析当前的市场形势&#xff0c;确定竞争对手表现的模式&#xff0c;并将其应用到我们应用营销策略中。不同的应用可以在直观的用户界面上进行简单的比较&#xff0c;很容易看到长期趋势。 在App Store和Google Play上获取竞争对手评论区的信…

怎么用PDF派工具将Word转成PDF

Word是我们最常用的一种格式文件&#xff0c;它易于编辑&#xff0c;但是安全性和稳定性较差&#xff0c;有时候我们发送给别人的Word文件&#xff0c;接收到打开内容已经乱码。遇到这种情况&#xff0c;我们可以优先将Word文件转换成稳定性好的PDF文件。那么如何进行文件格式转…

opencv 基础学习08-图像通道操作

opencv 基础学习08-图像通道操作 什么是图像通道&#xff1f;通道操作&#xff1a;**1 通过索引拆分**2 通过opencv 函数拆分通道合并 什么是图像通道&#xff1f; OpenCV的通道拆分功能可用于将多通道图像拆分成单独的通道&#xff0c;这在图像处理和计算机视觉任务中具有许多…

7月6日华为云盘古气象大模型登上《Nature》杂志:相比传统数值预报快10000倍

7月6日&#xff0c;国际顶级学术期刊《自然》&#xff08;Nature&#xff09;杂志正刊发表了华为云盘古大模型研发团队的最新研究成果——《三维神经网络用于精准中期全球天气预报》&#xff08;《Accurate medium-range global weather forecasting with 3D neural networks》…

解析!1V1直播源码开发搭建技术实时语音识别翻译功能的应用

语言是我们人类交流的工具&#xff0c;它的种类繁多&#xff0c;比如世界语言&#xff0c;像是中国的汉语、英国的英语、法国的法语等&#xff1b;又或是我们中国的方言&#xff0c;像是山东话、北京话、上海话等。可谓是五花八门&#xff0c;争奇斗艳&#xff0c;每一种世界语…

thinkphp 上传图片

public function upload_img(){// 读取图片资源// 存储路径$path "uploads/avatar";$file request()->file(background_img);// 存储图片$info $file->rule(uniqid)->move($path);// 存储成功if ($info) {//获取到上传图片的路径名称$name_img $path . …

利用ChatGPT场景化学习英语听说读写

大家好&#xff0c;我是可夫小子&#xff0c;关注AIGC、读书和自媒体。解锁更多ChatGPT、AI绘画玩法。加我&#xff0c;备注&#xff1a;chatgpt&#xff0c;拉你进群。 我们从初中就开始学习英语&#xff0c;到大学也有小十年&#xff0c;在这个过程中&#xff0c;我们投入了很…

卷积神经网络(CNN)原理详解

近些年人工智能发展迅速&#xff0c;在图像识别、语音识别、物体识别等各种场景上深度学习取得了巨大的成功&#xff0c;例如AlphaGo击败世界围棋冠军&#xff0c;iPhone X内置了人脸识别解锁功能等等&#xff0c;很多AI产品在世界上引起了很大的轰动。 而其中 卷积神经网络&am…

线性代数 4 every one(线性代数学习资源分享)

Linear Algebra 4 Every One 版权说明&#xff0c;以下我分享的都是一个名叫Kenji Hiranabe的日本学者&#xff0c;在github上分享的&#xff0c;关于Gilbert Strang教授所撰写的《Linear Algebra for Everyone》一书的总结&#xff0c;更像是一个非常精美的线性代数手册&#…

Verilog 学习之路

循环 7-10 代码段 generategenvar i;for (i0; i<8; i i1) begin: my_block_nameassign out[i] in[8-i-1];end endgenerate解释 该代码使用了 S y s t e m V e r i l o g SystemVerilog SystemVerilog 中的 g e n e r a t e generate generate 构造&#xff0c;它允许在…

FreeRTOS实时操作系统(十五)Tickless低功耗模式

系列文章目录 文章目录 系列文章目录低功耗模式Tickless低功耗模式宏定义配置 实验测试 低功耗模式 在之前的逻辑开发中学习过STM32的低功耗模式&#xff0c;在FreeRTOS中的低功耗模式&#xff0c;方便操作系统进入低功耗模式。 stm32中的低功耗模式&#xff1a; Tickless低…

不外传秘诀| docker 快速搭建常用的服务环境

本文主要给大家介绍如何使用 docker 搭建常用的服务环境&#xff0c; 包括mysql,reedis,nginx,jenkins 等常用的环境&#xff0c;下面直接进入主题。 1、MySQL 部署 ①搜索 MySQL 镜像 docker search mysql ②拉取 MySQL 镜像 docker pull mysql:5.7 ③创建容器&#xf…