拼音搜索在中文搜索环境中是经常使用的一种功能,用户只需要输入关键词的拼音全拼或者拼音首字母,搜索引擎就可以搜索出相关结果。在国内,中文输入法基本上都是基于汉语拼音的,这种在符合用户输入习惯的条件下缩短用户输入时间的功能是非常受欢迎的;

一、安装拼音搜索插件

下载对应版本的elasticsearch-analysis-pinyin插件;

https://github.com/medcl/elasticsearch-analysis-pinyin/releases/download/v7.9.2/elasticsearch-analysis-pinyin-7.9.2.zip

在elasticsearch安装目录下的的plugin目录新建analysis-pinyin目录,并解压下载的安装包;

重启elasticsearch,可以看到已经正常加载拼音插件

[2022-01-13T20:37:25,368][INFO ][o.e.p.PluginsService     ] [mango] loaded plugin [analysis-pinyin]

二、使用拼音插件

试一下分词效果,可以看到除了每个词的全频,还有每个字的首字母缩写;

POST _analyze
{
"analyzer": "pinyin",
"text": "我爱你,中国"
} {
"tokens" : [
{
"token" : "wo",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 0
},
{
"token" : "wanzg",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 0
},
{
"token" : "ai",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 1
},
{
"token" : "ni",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 2
},
{
"token" : "zhong",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 3
},
{
"token" : "guo",
"start_offset" : 0,
"end_offset" : 0,
"type" : "word",
"position" : 4
}
]
}

自定义pinyin filter,并创建mapping;

PUT /milk
{
"settings": {
"analysis": {
"filter": {
"pinyin_filter":{
"type":"pinyin",
"keep_separate_first_letter" : false,
"keep_full_pinyin" : true,
"keep_original" : true,
"limit_first_letter_length" : 16,
"lowercase" : true,
"remove_duplicated_term" : true
}
},
"analyzer": {
"ik_pinyin_analyzer":{
"tokenizer":"ik_max_word",
"filter":["pinyin_filter"]
}
}
}
},
"mappings": {
"properties": {
"brand":{
"type": "text",
"analyzer": "ik_pinyin_analyzer"
},
"series":{
"type": "text",
"analyzer": "ik_pinyin_analyzer"
},
"price":{
"type": "float"
}
}
}
}

批量索引文档;

POST _bulk
{"index":{"_index":"milk", "_id":1}}}
{"brand":"蒙牛", "series":"特仑苏", "price":60}
{"index":{"_index":"milk", "_id":2}}}
{"brand":"蒙牛", "series":"真果粒", "price":40}
{"index":{"_index":"milk", "_id":3}}}
{"brand":"华山牧", "series":"华山牧", "price":49.90}
{"index":{"_index":"milk", "_id":4}}}
{"brand":"伊利", "series":"安慕希", "price":49.90}
{"index":{"_index":"milk", "_id":5}}}
{"brand":"伊利", "series":"金典", "price":49.90}

搜索tls,可以看到已经匹配到对应的记录;

POST milk/_search
{ "query": {
"match_phrase_prefix": {
"series": "tl"
}
}
} {
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 6.691126,
"hits" : [
{
"_index" : "milk",
"_type" : "_doc",
"_id" : "1",
"_score" : 6.691126,
"_source" : {
"brand" : "蒙牛",
"series" : "特仑苏",
"price" : 60
}
}
]
}
}

可以看到查询直接使用MultiPhraseQuery来实现对两个位置字符的定位;

POST milk/_search
{ "query": {
"match_phrase_prefix": {
"series": "tl"
}
},
"highlight": {
"fields": {
"series": {}
}
}
} {
"profile" : {
"shards" : [
{
"id" : "[OoNXoregTmKQAFotUgOeaA][milk][0]",
"searches" : [
{
"query" : [
{
"type" : "MultiPhraseQuery",
"description" : "series:\"(t tl) (li l lun)\"",
"time_in_nanos" : 177400,
"breakdown" : {
"set_min_competitive_score_count" : 0,
"match_count" : 1,
"shallow_advance_count" : 0,
"set_min_competitive_score" : 0,
"next_doc" : 5400,
"match" : 12800,
"next_doc_count" : 1,
"score_count" : 1,
"compute_max_score_count" : 0,
"compute_max_score" : 0,
"advance" : 9200,
"advance_count" : 1,
"score" : 3900,
"build_scorer_count" : 2,
"create_weight" : 40800,
"shallow_advance" : 0,
"create_weight_count" : 1,
"build_scorer" : 105300
}
}
],
"rewrite_time" : 45700,
"collector" : [
{
"name" : "SimpleTopScoreDocCollector",
"reason" : "search_top_hits",
"time_in_nanos" : 14500
}
]
}
],
"aggregations" : [ ]
}
]
}
}

查询也可以返回高亮信息

POST milk/_search
{ "query": {
"match_phrase_prefix": {
"series": "tl"
}
},
"highlight": {
"fields": {
"series": {}
}
}
} {
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 6.691126,
"hits" : [
{
"_index" : "milk",
"_type" : "_doc",
"_id" : "1",
"_score" : 6.691126,
"_source" : {
"brand" : "蒙牛",
"series" : "特仑苏",
"price" : 60
},
"highlight" : {
"series" : [
"<em>特</em><em>仑</em>苏"
]
}
}
]
}
}

elasticsearch之拼音搜索的更多相关文章

  1. elasticsearch实战 中文+拼音搜索

    需求 雪花啤酒  需要搜索雪花.啤酒 .雪花啤酒.xh.pj.xh啤酒.雪花pj ik导入 参考https://www.cnblogs.com/LQBlog/p/10443862.html,不需要修改 ...

  2. 从零搭建 ES 搜索服务(四)拼音搜索

    一.前言 上篇介绍了 ES 的同义词搜索,使我们的搜索更强大了,然而这还远远不够,在实际使用中还可能希望搜索「fanqie」能将包含「番茄」的结果也罗列出来,这就涉及到拼音搜索了,本篇将介绍如何具体实 ...

  3. ElasticSearch安装拼音插件 elasticsearch-analysis-pinyin

    elasticsearch-analysis-pinyin 是 ElasticSearch的拼音插件.强大的功能支持拼音等的搜索 1.下载源代码 源码地址https://github.com/medc ...

  4. 【Solr】 solr对拼音搜索和拼音首字母搜索的支持

    问:对于拼音和拼音首字母的支持,当你在搜商品的时候,如果想输入拼音和拼音首字母就给出商品的信息,怎么办呢? 实现方式有2种,但是他们其实是对应的.  用lucene实现 1.建索引, 多建一个索引字段 ...

  5. elasticsearch的rest搜索--- 查询

    目录: 一.针对这次装B 的解释 二.下载,安装插件elasticsearch-1.7.0   三.索引的mapping 四. 查询 五.对于相关度的大牛的文档 四. 查询 1. 查询的官网的文档   ...

  6. 用MFC(C++)实现拼音搜索

    2015年4月1日更新: 我在github开源了Objective-C版的拼音搜索项目,感兴趣的可以去看看: OC版拼音搜索 最近项目需要实现按照拼音搜索资源.在网上找了一下,这方面的东西太少了. J ...

  7. elasticsearch实现网站搜索

    使用elasticsearch 实现网站搜索,可以支持商品搜索,筛选项过滤搜索 ,价格排序, 打分 筛选项聚合,还有其他综合排序 后续推出搜索人工干预排序,根据销量,好评率,售卖率 进行全方位的搜索实 ...

  8. php根据汉字获取拼音(php基于拼音搜索实现原理)

    php根据汉字获取拼音(php基于拼音搜索实现原理) 代码一:获取字符串汉字首字母,兼容GBK和UTF-8 <?php function getfirstchar($s0){   //获取单个汉 ...

  9. Python 和 Elasticsearch 构建简易搜索

    Python 和 Elasticsearch 构建简易搜索 作者:白宁超 2019年5月24日17:22:41 导读:件开发最大的麻烦事之一就是环境配置,操作系统设置,各种库和组件的安装.只有它们都正 ...

随机推荐

  1. 使用.NET 6开发TodoList应用(1)——系列背景

    前言 想到要写这样一个系列博客,初衷有两个:一是希望通过一个实践项目,将.NET 6 WebAPI开发的基础知识串联起来,帮助那些想要入门.NET 6服务端开发的朋友们快速上手,对使用.NET 6开发 ...

  2. uni-app + Cloudbase——uni-app 项目中如何使用腾讯云开发后端服务

    1 基本介绍 uni-app 是一个基于 Vue.js 的跨端开发框架,一套代码可以发布到 App.小程序.Web 等不同平台 腾讯云开发平台 Cloudbase 提供的 @cloudbase/js- ...

  3. CF70B Text Messaging 题解

    Content 有一个短信软件最多只能够上传长度为 \(n\) 的消息.现在你有一段话,但不一定能够一次发出.这段话由若干句话组成,以 ..? 或者 ! 为结尾.你不能够将一句话拆开来发,但是如果容量 ...

  4. Hystrix 监控可视化页面——Dashboard 流监控

    1.什么是Dashboard Hystrix-dashboard 是一款针对 Hystrix 进行实时监控的工具页面,通过 Hystrix Dashboard 我们可以在直观地看到各 Hystrix ...

  5. AcWing422. 校门外的树

    题目: 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米. 我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0,1,2,--,L,都种 ...

  6. 【LeetCode】717. 1-bit and 2-bit Characters 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcod ...

  7. 【LeetCode】454. 4Sum II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 字典 日期 题目地址:https://leetcod ...

  8. C. Watching Fireworks is Fun(Codeforces 372C)

    C. Watching Fireworks is Fun time limit per test 4 seconds memory limit per test 256 megabytes input ...

  9. Centos/Docker/Nginx/Node/Jenkins 操作

    Centos Centos 是一个基于 Linux 的开源免费操作系统 # 本地拷贝文件到远程服务器scp output.txt root@47.93.242.155:/data/ output.tx ...

  10. PL2586旺玖|USB 2.0HUB 工业级芯片|PROLIFIC PL2586

    工业级  USB 2.0 HUB 高速4端口集线器控制器 PL2586 1.PL2586说明   PL2586是USB 2.0高速4端口集线器控制器的高性能解决方案,完全符合通用串行总线规范2.0.控 ...