Elasticsearch的高阶玩法--拼音、同义词、自动补全、多字段按权重综合排序

介绍

基于 Elasticsearch 自定义扩展功能。我们将在其基础上构建拼音支持、同义词和自动更正。

安装扩展插件

cd elasticsearch-7.17.9/bin

./elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-pinyin/7.17.9

创建索引

使用curl命令操作elasticsearch 建立索引

curl -X PUT "http://localhost:9200/test_index" -H 'Content-Type: application/json' -d '
{"settings":{"analysis":{"analyzer":{"pinyin_analyzer":{"tokenizer":"my_pinyin"},"ik_max_syno":{"tokenizer":"ik_max_word","filter":"my_synonym"},"ik_smart_syno":{"tokenizer":"ik_smart","filter":"my_synonym"}},"tokenizer":{"my_pinyin":{"type":"pinyin","keep_first_letter":true,"keep_separate_first_letter":false,"keep_full_pinyin":true,"keep_original":false,"limit_first_letter_length":16,"lowercase":true,"remove_duplicated_term":true}},"filter":{"my_synonym":{"type":"synonym_graph","synonyms_path":"analysis/synonyms.txt"// ,"updateable": true}}}},"mappings":{"properties":{"name":{"type":"text","analyzer":"standard","fields":{"keyword":{"type":"keyword","ignore_above":256,"doc_values":false},"pinyin":{"type":"text","analyzer":"pinyin_analyzer"},"ik":{"type":"text","analyzer":"ik_max_syno","search_analyzer":"ik_smart_syno"},"suggest":{"type":"completion","analyzer":"ik_max_syno","search_analyzer":"ik_smart_syno","preserve_separators": "false","preserve_position_increments": "true","max_input_length": 50}}},"remark":{"type":"text","analyzer":"standard","fields":{"keyword":{"type":"keyword","ignore_above":256,"doc_values":false},"pinyin":{"type":"text","analyzer":"pinyin_analyzer"},"ik":{"type":"text","analyzer":"ik_max_syno","search_analyzer":"ik_smart_syno"},"suggest":{"type":"completion","analyzer":"ik_max_syno","search_analyzer":"ik_smart_syno","preserve_separators": "false","preserve_position_increments": "true","max_input_length": 50}}},"category":{"type":"keyword","doc_values":false},"tag":{"type":"keyword"},"rating":{"type":"scaled_float","scaling_factor": 10},"times":{"type":"integer"}}}
}'

插入示例数据

curl -X POST "http://localhost:9200/test_index/_bulk" -H 'Content-Type: application/json' -d'
{ "index": { "_id": 1 } }
{ "name": "番茄炒蛋", "remark": "番茄, 鸡蛋", "category": "家常菜", "tag": ["简单", "营养"] , "rating": 2.0,"times":100}
{ "index": { "_id": 2 } }
{ "name": "宫保鸡丁", "remark": "鸡胸肉, 干辣椒, 花生", "category": "川菜", "tag": ["辣", "下饭"], "rating": 3.4,"times":80 }
{ "index": { "_id": 3 } }
{ "name": "蒸蛋羹", "remark": "水,盐,蛋,麻油,生抽", "category": "川菜", "tag": ["辣", "下饭"] , "rating": 4.8,"times":20}
{ "index": { "_id": 4 } }
{ "name": "水煮蛋", "remark": "水,蛋", "category": "川菜", "tag": ["下饭"] , "rating": 4.9,"times":30}
{ "index": { "_id": 5 } }
{ "name": "酒酿蛋花年糕", "remark": "水,糖,蛋白,酒酿", "category": "家常菜", "tag": [ "简单","营养", "快手菜"] , "rating": 5.0,"times":50}
'

拼音搜索示例

curl -X GET "http://localhost:9200/test_index/_search" -H 'Content-Type: application/json' -d'
{"query": {"multi_match": {"query": "fanqie","fields": ["name.pinyin^4","name.ik^5","remark.pinyin^2","remark.ik^3"],"operator": "or"}},"size": 15, "sort": [{"_score": {"order": "desc"},"times": {"order": "desc"}}],"highlight": {"fields": {"name": {}},"highlight_query": {"match_phrase": {"name": {"query": "fanqie"}}}}
}'匹配结果:{"took": 3,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": null,"hits": [{"_index": "test_index","_type": "_doc","_id": "1","_score": 13.421699,"_source": {"name": "番茄炒蛋","remark": "番茄, 鸡蛋","category": "家常菜","tag": ["简单","营养"],"rating": 3.0,"times": 100},"sort": [13.421699,100]}]}
}

同义词搜索示例

curl -X GET "http://localhost:9200/test_index/_search" -H 'Content-Type: application/json' -d'
{"query": {"multi_match": {"query": "西红柿","fields": ["name.pinyin^4","name.ik^5","remark.pinyin^2","remark.ik^3"],"operator": "or"}},"size": 15, "sort": [{"_score": {"order": "desc"},"times": {"order": "desc"}}],"highlight": {"fields": {"name": {}},"highlight_query": {"match_phrase": {"name": {"query": "西红柿"}}}}
}'匹配结果:
{"took": 3,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 1,"relation": "eq"},"max_score": null,"hits": [{"_index": "test_index","_type": "_doc","_id": "1","_score": 8.388562,"_source": {"name": "番茄炒蛋","remark": "番茄, 鸡蛋","category": "家常菜","tag": ["简单","营养"],"rating": 3.0,"times": 100},"sort": [8.388562,100]}]}
}

自动补全搜索示例

根据输入的关键字给出关联的建议词

如下是根据输入的关键字匹配两个字段进行自动给出建议词汇

curl -X GET "http://localhost:9200/test_index/_search" -H 'Content-Type: application/json' -d'
{"suggest": {"nameSuggest": {"text": "鸡","completion": {"field": "name.suggest"}},"remarkSuggest": {"text": "鸡","completion": {"field": "remark.suggest"}}}
}'
匹配的结果:
{"took": 2,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 0,"relation": "eq"},"max_score": null,"hits": []},"suggest": {"remarkSuggest": [{"text": "鸡","offset": 0,"length": 1,"options": [{"text": "鸡胸肉, 干辣椒, 花生","_index": "test_index","_type": "_doc","_id": "2","_score": 1.0,"_source": {"name": "宫保鸡丁","remark": "鸡胸肉, 干辣椒, 花生","category": "川菜","tag": ["辣","下饭"],"rating": 3.4,"times": 80}}]}],"nameSuggest": [{"text": "鸡","offset": 0,"length": 1,"options": []}]}
}

多字段按照权重综合排序

curl -X GET "http://localhost:9200/test_index/_search" -H 'Content-Type: application/json' -d'
{"query": {"function_score": {"functions": [{"field_value_factor": {"field": "rating","factor": 5,  // name 字段的权重最大"modifier": "none","missing": 1  // 如果字段为空,则使用默认值 1}},{"field_value_factor": {"field": "times","factor": 3,  // title 字段的权重次之"modifier": "none","missing": 1}}],"score_mode": "sum",  // 将所有字段的得分加总"boost_mode": "multiply"  // 将查询得分与函数得分相乘}},"size": 10, "sort": [{"_score": {"order": "desc"}}]
}'匹配结果:
{"took": 2,"timed_out": false,"_shards": {"total": 1,"successful": 1,"skipped": 0,"failed": 0},"hits": {"total": {"value": 6,"relation": "eq"},"max_score": null,"hits": [{"_index": "test_index","_type": "_doc","_id": "1","_score": 315.0,"_source": {"name": "番茄炒蛋","remark": "番茄, 鸡蛋","category": "家常菜","tag": ["简单","营养"],"rating": 3.0,"times": 100},"sort": [315.0]},{"_index": "test_index","_type": "_doc","_id": "2","_score": 257.0,"_source": {"name": "宫保鸡丁","remark": "鸡胸肉, 干辣椒, 花生","category": "川菜","tag": ["辣","下饭"],"rating": 3.4,"times": 80},"sort": [257.0]},{"_index": "test_index","_type": "_doc","_id": "5","_score": 174.0,"_source": {"name": "酒酿蛋花年糕","remark": "水,糖,蛋白,酒酿","category": "家常菜","tag": ["简单","营养","快手菜"],"rating": 4.8,"times": 50},"sort": [174.0]},{"_index": "test_index","_type": "_doc","_id": "4","_score": 114.5,"_source": {"name": "水煮蛋","remark": "水,蛋","category": "家常菜","tag": ["简单","营养","快手菜"],"rating": 4.9,"times": 30},"sort": [114.5]},{"_index": "test_index","_type": "_doc","_id": "3","_score": 84.0,"_source": {"name": "蒸蛋羹","remark": "水,盐,蛋,麻油,生抽","category": "川菜","tag": ["辣","下饭"],"rating": 4.8,"times": 20},"sort": [84.0]},{"_index": "test_index","_type": "_doc","_id": "6","_score": 83.0,"_source": {"name": "牛奶麵包","remark": "奶油,牛奶,糖,3颗蛋","category": "家常菜","tag": ["简单","营养","快手菜"],"rating": 4.6,"times": 20},"sort": [83.0]}]}
}

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

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

相关文章

界面控件DevExpress v24.2.3全新发布——正式支持.NET 9

DevExpress拥有.NET开发需要的所有平台控件,包含600多个UI控件、报表平台、DevExpress Dashboard eXpressApp 框架、适用于 Visual Studio的CodeRush等一系列辅助工具。 屡获大奖的软件开发平台DevExpress 近期重要版本v24.2已正式发布,该版本拥有众多新…

Three.js资源-模型下载网站

在使用 Three.js 进行 3D 开发时,拥有丰富的模型资源库可以大大提升开发效率和作品质量。以下是一些推荐的 Three.js 模型下载网站,它们提供了各种类型的 3D 模型,适合不同项目需求。无论你是需要逼真的建筑模型,还是简单的几何体…

(三)PyQT5+QGIS+python使用经验——解决各版本不兼容问题

一、问题描述 基础环境:Windows10(64) PyCharm2024 QGIS 3.22。 目的:解决之前python版本多,pyqt5以及QT Designer交互使用存在环境变量冲突矛盾,以及QGIS安装时自带python、pyqt5等问题。 尤其是在QT …

C++ webrtc开发(非原生开发,linux上使用libdatachannel库)

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录 前言一、libdatachannel库的下载和build二、开始使用 1.2.引入库3.开始使用 总结 前言 使用c开发webrtc在互联网上留下的资料甚少,经过我一段时间的探…

SpringBoot 手动实现动态切换数据源 DynamicSource (中)

大家好,我是此林。 SpringBoot 手动实现动态切换数据源 DynamicSource (上)-CSDN博客 在上一篇博客中,我带大家手动实现了一个简易版的数据源切换实现,方便大家理解数据源切换的原理。今天我们来介绍一个开源的数据源…

上海艾一公司-运维工程师知识点备战

1.AD域控(ActionDirectory活动目录) ad域的作用:批量管理主机和用户(所以数量要多用这个才合适) 前置1:VM安装Windows镜像 2.IT资产管理 3.会议室管理

构建一个rust生产应用读书笔记四(实战2)

此门课程学习采用actix-web框架完成一个生产级别的rust应用,在 actix-web 中,Extractors 是一个非常重要的概念,它们用于从传入的 HTTP 请求中提取特定的信息片段。actix-web 提供了多种内置的提取器,以满足常见的使用场景。说白了…

前端学习笔记-Vue篇-04

4 Vue中的ajax 4.1 解决开发环境Ajax跨域问题 vue脚手架配置代理 配置参考 | Vue CLI方法一:在vue.config.js中添加如下配置: module.exports {devServer: {proxy: http://localhost:4000} } 说明: 1.优点:配置简单,请求资源时直接发给前端(8080)即…

InnoDB事务系统(二):事务的实现

事务隔离性由锁来实现。原子性、一致性、持久性通过数据库的 redo log 和 undo log 来完成。 redo log 称为重做日志,用来保证事务的原子性和持久性。undo log 用来保证事务的一致性。 有的 DBA 或许会认为 undo 是 redo 的逆过程,其实不然。redo 和 u…

c++理解(三)

本文主要探讨c相关知识。 模板是对类型参数化 函数模板特化不是模板函数重载 allocator(空间配置器):内存开辟释放,对象构造析构 优先调用对象成员方法实现的运算符重载函数,其次全局作用域找 迭代器遍历访问元素,调用erase,insert方法后,当前位置到容器…

实训项目11基于51单片机的门禁监测系统设计

00 要求 基于51单片机和RFID模块实现门禁的设计。使之具有以下功能: 能够正常的读卡信息;在正常刷卡通过后,可以控制电子锁动作;在刷卡失败后,可以产生报警信号; 01 功能分析 读卡后会RFID会自动通过TXD(串口&…

opencv——识别图片颜色并绘制轮廓

图像边缘检测 本实验要用到Canny算法,Canny边缘检测方法常被誉为边缘检测的最优方法。 首先,Canny算法的输入端应为图像的二值化结果,接收到二值化图像后,需要按照如下步骤进行: 高斯滤波。计算图像的梯度和方向。非极…

源码安装PHP-7.2.19

源码安装PHP-7.2.19 1.解压 tar -xjvf php-7.2.19.tar.bz2.编译 -prefix安装路径 cd php-7.2.19 ./configure --prefix/home/work/study 成功输出 3.make(构建) makemake testmake installlinux对php操作的一些命令 # 进入到php [rootvdb1 study]# cd php/ [rootvdb1 st…

数据库管理-第271期 Oracle 23ai:用MongoDB的方式来操作JSON二元性(20241214)

数据库管理271期 2024-12-14 数据库管理-第271期 Oracle 23ai:用MongoDB的方式来操作JSON二元性(20241214)1 初始化数据1.1 创建用户1.2 导入数据1.3 创建JSON关系二元性视图 2 创建ORDS服务2.1 下载JDK172.2 安装ORDS2.3 启用MongoDB API2.4…

2024 年的科技趋势

2024 年在科技领域有着诸多重大进展与突破。从人工智能、量子计算到基因组医学、可再生能源以及新兴技术重塑了众多行业。随着元宇宙等趋势的兴起以及太空探索取得的进步,未来在接下来的岁月里有望继续取得进展与突破。让我们来探讨一下定义 2024 年的一些关键趋势&…

WPF+MVVM案例实战与特效(三十八)- 封装一个自定义的数字滚动显示控件

文章目录 1、运行效果2、案例实现1、功能设计2、页面布局3、控件使用4、运行效果3、拓展:多数字自定义控件1、控件应用4、总结1、运行效果 在Windows Presentation Foundation (WPF)应用程序中,自定义控件允许开发者创建具有特定功能和外观的独特UI元素。本博客将介绍一个名…

ElasticSearch的自动补全功能(拼音分词器、自定义分词器、DSL实现自动补全查询、RestAPI实现自动补全查询)

文章目录 1. 什么是自动补全2. 拼音分词器2.1 初识拼音分词器2.2 下载拼音分词器2.3 安装拼音分词器2.4 测试拼音分词器 3. 自定义分词器3.1 拼音分词器存在的问题3.2 分词器(analyzer)的组成3.3 如何自定义分词器3.4 拼音分词器的可选参数3.5 配置自定义…

八股—Java基础(二)

目录 一. 面向对象 1. 面向对象和面向过程的区别? 2. 面向对象三大特性 3. Java语言是如何实现多态的? 4. 重载(Overload)和重写(Override)的区别是什么? 5. 重载的方法能否根据返回值类…

Java-08

类的抽象是将类的实现和使用分离, 而类的封装是将实现的细节封装起来并且对用户隐藏,用户只需会用就行。 类的合约指的是从类外可以访问的方法和数据域的集合以及与其这些成员如何行为的描述 isAlive()方法的返回值类型为布尔型(Boolean)。这个方法用于…

【MATLAB第109期】基于MATLAB的带置信区间的RSA区域敏感性分析方法,无目标函数

【MATLAB第108期】基于MATLAB的带置信区间的RSA区域敏感性分析方法,无目标函数 参考第64期文章【MATLAB第64期】【保姆级教程】基于MATLAB的SOBOL全局敏感性分析模型运用(含无目标函数,考虑代理模型) 创新点: 1、采…