ElasticSearch 分词器

文章目录

      • 一、安装中文分词插件
        • Linux安装7.14.1版本:
          • 测试1:ik_smart
          • 测试2:ik_max_word
      • 二、es内置的分词器:
      • 三、拼音插件安装以及(IK+pinyin使用)
        • 配置 IK + pinyin 分词配置

一、安装中文分词插件

IK Analysis for Elasticsearch是开源社区比较流行的中文分词插件
官网:https://github.com/medcl/elasticsearch-analysis-ik

  本来想用这两种方法安装:

.\bin\elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.4.2/elasticsearch-analysis-ik-5.4.2.zip
bin\elasticsearch-plugin install file:///C:\Users\jk\Desktop\elasticsearch-analysis-ik-5.4.2.zip

  但是却总是报错:

ERROR: `elasticsearch` directory is missing in the plugin zip

  后来只能用了这种方法:在Elasticsearch安装目录下的文件夹plugins中新建文件夹ik,将elasticsearch-analysis-ik-5.4.2.zip解压到这里即可,其实官网里已经说明了低于5.5.1版本的用解压的方式安装了:
在这里插入图片描述
参考:Elasticsearch5.x安装IK分词器以及使用

Linux安装7.14.1版本:

下载地址:https://github.com/medcl/elasticsearch-analysis-ik/releases

[hadoop@node01 ~]$ elasticsearch-7.14.1/bin/elasticsearch-plugin install file:///mnt/elasticsearch-analysis-ik-7.14.1.zip

在这里插入图片描述
ik_smart:会做最粗粒度的拆分
ik_max_word:会将文本做最细粒度的拆分。

测试1:ik_smart
GET /_analyze
{"analyzer": "ik_smart","text":"中华人民共和国"
}

结果:

{"tokens": [{"token": "中华人民共和国","start_offset": 0,"end_offset": 7,"type": "CN_WORD","position": 0}]
}
测试2:ik_max_word
GET /_analyze
{"analyzer": "ik_max_word","text":"中华人民共和国"
}

结果:

{"tokens": [{"token": "中华人民共和国","start_offset": 0,"end_offset": 7,"type": "CN_WORD","position": 0},{"token": "中华人民","start_offset": 0,"end_offset": 4,"type": "CN_WORD","position": 1},{"token": "中华","start_offset": 0,"end_offset": 2,"type": "CN_WORD","position": 2},{"token": "华人","start_offset": 1,"end_offset": 3,"type": "CN_WORD","position": 3},{"token": "人民共和国","start_offset": 2,"end_offset": 7,"type": "CN_WORD","position": 4},{"token": "人民","start_offset": 2,"end_offset": 4,"type": "CN_WORD","position": 5},{"token": "共和国","start_offset": 4,"end_offset": 7,"type": "CN_WORD","position": 6},{"token": "共和","start_offset": 4,"end_offset": 6,"type": "CN_WORD","position": 7},{"token": "国","start_offset": 6,"end_offset": 7,"type": "CN_CHAR","position": 8}]
}
GET /_analyze
{"analyzer": "ik_max_word","text":"I love you"
}
结果:
{"tokens" : [{"token" : "i","start_offset" : 0,"end_offset" : 1,"type" : "ENGLISH","position" : 0},{"token" : "love","start_offset" : 2,"end_offset" : 6,"type" : "ENGLISH","position" : 1},{"token" : "you","start_offset" : 7,"end_offset" : 10,"type" : "ENGLISH","position" : 2}]
}

参考:https://blog.csdn.net/wenxindiaolong061/article/details/82562450

二、es内置的分词器:

  • standard analyzer
  • simple analyzer
  • whitespace analyzer
  • language analyzer(特定的语言的分词器)

例句:Set the shape to semi-transparent by calling set_trans(5)
不同分词器的分词结果:

  • standard analyzer:set, the, shape, to, semi, transparent, by, calling, set_trans, 5(默认的是standard)
  • simple analyzer:set, the, shape, to, semi, transparent, by, calling, set, trans
  • whitespace analyzer:Set, the, shape, to, semi-transparent, by, calling, set_trans(5)
  • language analyzer(特定的语言的分词器,比如说,english,英语分词器):set, shape, semi, transpar, call, set_tran, 5

分词器测试:

GET /_analyze
{"analyzer": "standard","text":"The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone."
}

结果:

  "tokens" : [{"token" : "the","start_offset" : 0,"end_offset" : 3,"type" : "<ALPHANUM>","position" : 0},{"token" : "2","start_offset" : 4,"end_offset" : 5,"type" : "<NUM>","position" : 1},{"token" : "quick","start_offset" : 6,"end_offset" : 11,"type" : "<ALPHANUM>","position" : 2},{"token" : "brown","start_offset" : 12,"end_offset" : 17,"type" : "<ALPHANUM>","position" : 3},{"token" : "foxes","start_offset" : 18,"end_offset" : 23,"type" : "<ALPHANUM>","position" : 4},{"token" : "jumped","start_offset" : 24,"end_offset" : 30,"type" : "<ALPHANUM>","position" : 5},{"token" : "over","start_offset" : 31,"end_offset" : 35,"type" : "<ALPHANUM>","position" : 6},{"token" : "the","start_offset" : 36,"end_offset" : 39,"type" : "<ALPHANUM>","position" : 7},{"token" : "lazy","start_offset" : 40,"end_offset" : 44,"type" : "<ALPHANUM>","position" : 8},{"token" : "dog’s","start_offset" : 45,"end_offset" : 50,"type" : "<ALPHANUM>","position" : 9},{"token" : "bone","start_offset" : 51,"end_offset" : 55,"type" : "<ALPHANUM>","position" : 10}]
}

  可以看出是按照空格、非字母的方式对输入的文本进行了转换,比如对 Java 做了转小写,对一些停用词也没有去掉,比如 in,其中 token 为分词结果;start_offset 为起始偏移;end_offset 为结束偏移;position 为分词位置。可配置项

选项描述
max_token_length最大令牌长度。如果看到令牌超过此长度,则将其max_token_length间隔分割。默认为255。
stopwords预定义的停用词列表,例如english或包含停用词列表的数组。默认为none。
stopwords_path包含停用词的文件的路径。
COPY{"settings": {"analysis": {"analyzer": {"my_english_analyzer": {"type": "standard","max_token_length": 5,"stopwords": "_english_"}}}}
}

  不同的 Analyzer 会有不同的分词结果,内置的分词器有以下几种,基本上内置的 Analyzer 包括 Language Analyzers 在内,对中文的分词都不够友好,中文分词需要安装其它 Analyzer

分析器描述分词对象结果
standard标准分析器是默认的分析器,如果没有指定,则使用该分析器。它提供了基于文法的标记化(基于 Unicode 文本分割算法,如 Unicode 标准附件 # 29所规定) ,并且对大多数语言都有效。The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.[ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog’s, bone ]
simple简单分析器将文本分解为任何非字母字符的标记,如数字、空格、连字符和撇号、放弃非字母字符,并将大写字母更改为小写字母。The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.[ the, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]
whitespace空格分析器在遇到空白字符时将文本分解为术语The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.[ The, 2, QUICK, Brown-Foxes, jumped, over, the, lazy, dog’s, bone. ]
stop停止分析器与简单分析器相同,但增加了删除停止字的支持。默认使用的是 english 停止词。The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.[ quick, brown, foxes, jumped, over, lazy, dog, s, bone ]
keyword不分词,把整个字段当做一个整体返回The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.[The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.]
pattern模式分析器使用正则表达式将文本拆分为术语。正则表达式应该匹配令牌分隔符,而不是令牌本身。正则表达式默认为 w+ (或所有非单词字符)。The 2 QUICK Brown-Foxes jumped over the lazy dog’s bone.[ the, 2, quick, brown, foxes, jumped, over, the, lazy, dog, s, bone ]
多种西语系 arabic, armenian, basque, bengali, brazilian, bulgarian, catalan, cjk, czech, danish, dutch, english等等一组旨在分析特定语言文本的分析程序。

  中文分词器最简单的是ik分词器,还有jieba分词,哈工大分词器等

分析器描述分词对象结果
ik_smartik分词器中的简单分词器,支持自定义字典,远程字典学如逆水行舟,不进则退[学如逆水行舟,不进则退]
ik_max_wordik_分词器的全量分词器,支持自定义字典,远程字典学如逆水行舟,不进则退[学如逆水行舟,学如逆水,逆水行舟,逆水,行舟,不进则退,不进,则,退]

参考:
【9种】ElasticSearch分词器详解,一文get!!!| 博学谷狂野架构师
[转]中英文停止词表(stopword)

三、拼音插件安装以及(IK+pinyin使用)

  有时在淘宝搜索商品的时候,会发现使用汉字、拼音、或者拼音混合汉字都会出来想要的搜索结果,其实是通过拼音搜索插件实现的。

  地址:https://github.com/infinilabs/analysis-pinyin/releases

  选择对应的版本,版本与 ES 版本一致,建议直接下载编译后的 zip 包;若是下载源码包,则需要自己编码打包 mvn clean package 生成 zip 包。联网安装:elasticsearch-7.14.1/bin/elasticsearch-plugin install https://get.infini.cloud/elasticsearch/analysis-pinyin/7.14.1

在这里插入图片描述
  使用自定义拼音分词器创建索引:

PUT /medcl/ 
{"settings" : {"analysis" : {"analyzer" : {"pinyin_analyzer" : {"tokenizer" : "my_pinyin"}},"tokenizer" : {"my_pinyin" : {"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}}}}
}

  测试:

POST medcl/_analyze
{"analyzer": "pinyin_analyzer","text": "刘德华"
}

  结果:

{"tokens" : [{"token" : "liu","start_offset" : 0,"end_offset" : 0,"type" : "word","position" : 0},{"token" : "刘德华","start_offset" : 0,"end_offset" : 0,"type" : "word","position" : 0},{"token" : "ldh","start_offset" : 0,"end_offset" : 0,"type" : "word","position" : 0},{"token" : "de","start_offset" : 0,"end_offset" : 0,"type" : "word","position" : 1},{"token" : "hua","start_offset" : 0,"end_offset" : 0,"type" : "word","position" : 2}]
}
配置 IK + pinyin 分词配置

  settings 设置:

curl -XPUT "http://localhost:9200/medcl/" -d'
{"index": {"analysis": {"analyzer": {"default": {"tokenizer": "ik_max_word"},"pinyin_analyzer": {"tokenizer": "shopmall_pinyin"}},"tokenizer": {"shopmall_pinyin": {"keep_joined_full_pinyin": "true","keep_first_letter": "true","keep_separate_first_letter": "false","lowercase": "true","type": "pinyin","limit_first_letter_length": "16","keep_original": "true","keep_full_pinyin": "true"}}}}
}'

  创建 mapping:

curl -XPOST http://localhost:9200/medcl/folks/_mapping -d'
{"folks": {"properties": {"name": {"type": "text","analyzer": "ik_max_word","include_in_all": true,"fields": {"pinyin": {"type": "text","analyzer": "pinyin_analyzer"}}}}}
}'

  添加测试文档:

curl -XPOST http://localhost:9200/medcl/folks/ -d'{"name":"刘德华"}'curl -XPOST http://localhost:9200/medcl/folks/ -d'{"name":"中华人民共和国国歌"}'

  拼音分词效果:

curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:liu"curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:de"curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:hua"curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:ldh"

  中文分词测试:

curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name:刘"curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name:刘德"

注意:用户输入搜索内容,根据正则匹配分成中文、拼音、中文+拼音、中文+拼音+数字+特殊符号等情况进行搜索,如下:

  1. 若是汉字搜索,没有搜索结果,转化为拼音再搜索一次,按拼音搜索还是无结果,则按模糊搜索再搜一次,若是还无结果,可考虑推荐
  2. 若是拼音搜索,没有搜索结果,则按模糊搜索再搜一次
  3. 若是汉字+拼音搜索,暂且按拼音处理
  4. 拼音、数字、特殊字符,暂且按拼音处理

参考:elasticsearch拼音插件安装以及(IK+pinyin使用)

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

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

相关文章

arm64位FFmpeg与X264库

参考链接&#xff1a; https://blog.csdn.net/gitblog_09700/article/details/142945092

机器学习与深度学习4:数据集处理Dataset,DataLoader,batch_size

深度学习中&#xff0c;我们能看到别人的代码中都有一个继承Dataset类的数据集处理过程&#xff0c;这也是深度学习处理数据集的的基础&#xff0c;下面介绍这个数据集的定义和使用&#xff1a; 1、数据集加载 1.1 通用的定义 Bach&#xff1a;表示每次喂给模型的数据 Epoc…

MySQL数据库和表的操作之SQL语句

&#x1f3af; 本文专栏&#xff1a;MySQL深入浅出 &#x1f680; 作者主页&#xff1a;小度爱学习 MySQL数据库和表的操作 关系型数据库&#xff0c;都是遵循SQL语法进行数据查询和管理的。 SQL语句 什么是sql SQL&#xff1a;结构化查询语言(Structured Query Language)&…

ubuntu开发mcu环境

# 编辑 vim或者vscode # 编译 arm-none-eabi # 烧写 openocd 若是默认安装&#xff0c;会在/usr/share/openocd/scripts/{interface,target} 有配置接口和目标版配置 示例&#xff1a; openocd -f interface/stlink-v2.cfg -f target/stm32f1x.cfg 启动后&#xff0c;会…

Windows模仿Mac大小写切换, 中英文切换

CapsLock 功能优化脚本部署指南 部署步骤 第一步&#xff1a;安装 AutoHotkey v2 访问 AutoHotkey v2 官网下载并安装最新版本安装时勾选 "Add Compile Script to context menus" 第二步&#xff1a;部署脚本 直接运行 (调试推荐) 新建文本文件&#xff0c;粘贴…

Selenium Web自动化如何快速又准确的定位元素路径,强调一遍是元素路径

如果文章对你有用&#xff0c;请给个赞&#xff01; 匹配的ChromeDriver和浏览器版本是更好完成自动化的基础&#xff0c;可以从这里去下载驱动程序&#xff1a; 最全ChromeDriver下载含win linux mac 最新版本134.0.6998.165 持续更新..._chromedriver 134-CSDN博客 如果你问…

CSRF vs SSRF详解

一、CSRF&#xff08;跨站请求伪造&#xff09;攻击全解 攻击原理示意图 受害者浏览器 ├── 已登录银行网站&#xff08;Cookie存活&#xff09; └── 访问恶意网站执行&#xff1a;<img src"http://bank.com/transfer?tohacker&amount1000000">核心…

Python PDF解析利器:pdfplumber | AI应用开发

Python PDF解析利器&#xff1a;pdfplumber全面指南 1. 简介与安装 1.1 pdfplumber概述 pdfplumber是一个Python库&#xff0c;专门用于从PDF文件中提取文本、表格和其他信息。相比其他PDF处理库&#xff0c;pdfplumber提供了更直观的API和更精确的文本定位能力。 主要特点…

niuhe 插件教程 - 配置 MCP让AI更聪明

niuhe 插件官方教程已经上线, 请访问: http://niuhe.zuxing.net niuhe 连接 MCP 介绍 API 文档的未来&#xff1a;MCP&#xff0c;让协作像聊天一样简单. MCP 是 Model Context Protocol(模型上下文协议)的缩写&#xff0c;是 2024 年 11 月 Claude 的公司 Anthropic 推出并开…

26考研——排序_插入排序(8)

408答疑 文章目录 二、插入排序基本概念插入排序方法直接插入排序算法描述示例性能分析 折半插入排序改进点算法步骤性能分析 希尔排序相关概念示例分析希尔排序的效率效率分析空间复杂度时间复杂度 九、参考资料鲍鱼科技课件26王道考研书 二、插入排序 基本概念 定义&#x…

精华贴分享|从不同的交易理论来理解头肩形态,殊途同归

本文来源于量化小论坛策略分享会板块精华帖&#xff0c;作者为孙小迪&#xff0c;发布于2025年2月17日。 以下为精华帖正文&#xff1a; 01 前言 学习了一段时间交易后&#xff0c;我发现在几百年的历史中&#xff0c;不同门派的交易理论对同一种市场特征的称呼不一样&#x…

leetcode437.路径总和|||

对于根结点来说&#xff0c;可以选择当前结点为路径也可以不选择&#xff0c;但是一旦选择当前结点为路径那么后续都必须要选择结点作为路径&#xff0c;不然路径不连续是不合法的&#xff0c;所以这里分开出来两个方法进行递归 由于力扣最后一个用例解答错误&#xff0c;分析…

北斗导航 | 改进奇偶矢量法的接收机自主完好性监测算法原理,公式,应用,RAIM算法研究综述,matlab代码

改进奇偶矢量法的接收机自主完好性监测算法研究 摘要 接收机自主完好性监测(RAIM)是保障全球导航卫星系统(GNSS)安全性的核心技术。针对传统奇偶矢量法在噪声敏感性、多故障隔离能力上的缺陷,本文提出一种基于加权奇偶空间与动态阈值的改进算法。通过引入观测值权重矩阵重…

深度神经网络全解析:原理、结构与方法对比

深度神经网络全解析&#xff1a;原理、结构与方法对比 1. 引言 随着人工智能的发展&#xff0c;深度神经网络&#xff08;Deep Neural Network&#xff0c;DNN&#xff09;已经成为图像识别、自然语言处理、语音识别、自动驾驶等领域的核心技术。相比传统机器学习方法&#x…

经典论文解读系列:MapReduce 论文精读总结:简化大规模集群上的数据处理

&#x1f9e0; MapReduce 论文解读总结&#xff1a;简化大规模集群上的数据处理 原文标题&#xff1a;MapReduce: Simplified Data Processing on Large Clusters 作者&#xff1a;Jeffrey Dean & Sanjay Ghemawat 发表时间&#xff1a;2004 年 发表机构&#xff1a;Google…

通过Appium理解MCP架构

MCP即Model Context Protocol&#xff08;模型上下文协议&#xff09;&#xff0c;是由Anthropic公司于2024年11月26日推出的开放标准框架&#xff0c;旨在为大型语言模型与外部数据源、工具及系统建立标准化交互协议&#xff0c;以打破AI与数据之间的连接壁垒。 MCP架构与Appi…

网页版五子棋项目的问题处理

文章目录 config.WebSocketConfig将键值对加⼊OnlineUserManager中线程安全、锁ObjectMapper来处理json针对多开情况的判定处理连接关闭、异常&#xff08;玩家中途退出&#xff09;后的不合理操作游戏大厅数据更新 config.WebSocketConfig 把MatchAPI注册进去 • 在addHandle…

【初探数据结构】归并排序与计数排序的序曲

&#x1f4ac; 欢迎讨论&#xff1a;在阅读过程中有任何疑问&#xff0c;欢迎在评论区留言&#xff0c;我们一起交流学习&#xff01; &#x1f44d; 点赞、收藏与分享&#xff1a;如果你觉得这篇文章对你有帮助&#xff0c;记得点赞、收藏&#xff0c;并分享给更多对数据结构感…

算法刷题记录——LeetCode篇(8.7) [第761~770题](持续更新)

更新时间&#xff1a;2025-03-30 算法题解目录汇总&#xff1a;算法刷题记录——题解目录汇总技术博客总目录&#xff1a;计算机技术系列博客——目录页 优先整理热门100及面试150&#xff0c;不定期持续更新&#xff0c;欢迎关注&#xff01; 763. 划分字母区间 给你一个字…

Pod 网络与 CNI 的作用

在 Kubernetes 中&#xff0c;Pod 网络 是实现容器间通信的核心机制&#xff0c;每个 Pod 拥有独立的 IP 地址&#xff0c;可直接跨节点通信。CNI&#xff08;Container Network Interface&#xff09; 是 Kubernetes 的网络插件标准&#xff0c;负责为 Pod 分配 IP、配置网络规…