分词器工作流程和Ik分词器详解

news2025/5/13 12:36:48
切分词语,normalization
给你一段句子,然后将这段句子拆分成一个一个的单个的单词,同时对每个单词进行
normalization(时态转换,单复数转换),分词器
recall,召回率:搜索的时候,增加能够搜索到的结果的数量
1 character filter:在一段文本进行分词之前,先进行预处理,比如说最常见的就是,过滤html
标签(<span>hello<span> ‐‐> hello),& ‐‐> and(I&you ‐‐> I and you)
2
3 tokenizer:分词,hello you and me ‐‐> hello, you, and, me
4
5 token filter:lowercase,stop word,synonymom,liked ‐‐> like,Tom ‐‐> tom,a/th
e/an ‐‐> 干掉,small ‐‐> little
6
一个分词器,很重要,将一段文本进行各种处理,最后处理好的结果才会拿去建立倒
排索引
2、内置分词器的介绍
1 Set the shape to semi‐transparent by calling set_trans(5)
2
3 standard analyzer:set, the, shape, to, semi, transparent, by, calling, set_tra
ns, 5(默认的是standard)
4
5 simple analyzer:set, the, shape, to, semi, transparent, by, calling, set, tran
s
6
7 whitespace analyzer:Set, the, shape, to, semi‐transparent, by, calling, set_tr
ans(5)
8
9 stop analyzer:移除停用词,比如a the it等等
10
11 测试:
12 POST _analyze
13 {
14 "analyzer":"standard",
15 "text":"Set the shape to semi‐transparent by calling set_trans(5)"
16 }
3、定制分词器
1)默认的分词器
standard
standard tokenizer:以单词边界进行切分
standard token filter:什么都不做
lowercase token filter:将所有字母转换为小写
stop token filer(默认被禁用):移除停用词,比如a the it等等
2)修改分词器的设置
启用english停用词token filter
1 PUT /my_index
2 {
3 "settings": {
4 "analysis": {
5 "analyzer": {
6 "es_std": {
7 "type": "standard",
8 "stopwords": "_english_"
9 }
10 }
11 }
12 }
13 }
14
15 GET /my_index/_analyze
16 {
17 "analyzer": "standard",
18 "text": "a dog is in the house"
19 }
20
21 GET /my_index/_analyze
22 {
23 "analyzer": "es_std",
24 "text":"a dog is in the house"
25 }
26
27 3、定制化自己的分词器
28
29 PUT /my_index
30 {
31 "settings": {
32 "analysis": {
33 "char_filter": {
34 "&_to_and": {
35 "type": "mapping",
36 "mappings": ["&=> and"]
37 }
38 },
39 "filter": {
40 "my_stopwords": {
41 "type": "stop",
42 "stopwords": ["the", "a"]
43 }
44 },
45 "analyzer": {
46 "my_analyzer": {
47 "type": "custom",
48 "char_filter": ["html_strip", "&_to_and"],
49 "tokenizer": "standard",
50 "filter": ["lowercase", "my_stopwords"]
51 }
52 }
53 }
54 }
55 }
56
57 GET /my_index/_analyze
58 {
59 "text": "tom&jerry are a friend in the house, <a>, HAHA!!",
60 "analyzer": "my_analyzer"
61 }
62
63 PUT /my_index/_mapping/my_type
64 {
65 "properties": {
66 "content": {
67 "type": "text",
68 "analyzer": "my_analyzer"
69 }
70 }
71 }

ik分词器详解

41 "type": "stop",
42 "stopwords": ["the", "a"]
43 }
44 },
45 "analyzer": {
46 "my_analyzer": {
47 "type": "custom",
48 "char_filter": ["html_strip", "&_to_and"],
49 "tokenizer": "standard",
50 "filter": ["lowercase", "my_stopwords"]
51 }
52 }
53 }
54 }
55 }
56
57 GET /my_index/_analyze
58 {
59 "text": "tom&jerry are a friend in the house, <a>, HAHA!!",
60 "analyzer": "my_analyzer"
61 }
62
63 PUT /my_index/_mapping/my_type
64 {
65 "properties": {
66 "content": {
67 "type": "text",
68 "analyzer": "my_analyzer"
69 }
70 }
71 }
3)ik分词器详解
ik配置文件地址:es/plugins/ik/config目录
IKAnalyzer.cfg.xml:用来配置自定义词库
main.dic:ik原生内置的中文词库,总共有27万多条,只要是这些单词,都会被分在
一起
quantifier.dic:放了一些单位相关的词
suffix.dic:放了一些后缀
surname.dic:中国的姓氏
stopword.dic:英文停用词
ik原生最重要的两个配置文件
main.dic:包含了原生的中文词语,会按照这个里面的词语去分词
stopword.dic:包含了英文的停用词
停用词,stopword
a the and at but
一般,像停用词,会在分词的时候,直接被干掉,不会建立在倒排索引中
4)IK分词器自定义词库
(1)自己建立词库:每年都会涌现一些特殊的流行词,网红,蓝瘦香菇,喊麦,鬼
畜,一般不会在ik的原生词典里
自己补充自己的最新的词语,到ik的词库里面去
IKAnalyzer.cfg.xml:ext_dict,custom/mydict.dic
补充自己的词语,然后需要重启es,才能生效
(2)自己建立停用词库:比如了,的,啥,么,我们可能并不想去建立索引,让人家
搜索
custom/ext_stopword.dic,已经有了常用的中文停用词,可以补充自己的停用词,然
后重启es
1 IK分词器源码下载:https://github.com/medcl/elasticsearch‐analysis‐ik/tree
5)IK热更新
每次都是在es的扩展词典中,手动添加新词语,很坑
(1)每次添加完,都要重启es才能生效,非常麻烦
(2)es是分布式的,可能有数百个节点,你不能每次都一个一个节点上面去修改
es不停机,直接我们在外部某个地方添加新的词语,es中立即热加载到这些新词语
IKAnalyzer.cfg.xml

<properties>
2 <comment>IK Analyzer 扩展配置</comment>
3 <!‐‐用户可以在这里配置自己的扩展字典 ‐‐>
4 <entry key="ext_dict">location</entry>
5 <!‐‐用户可以在这里配置自己的扩展停止词字典‐‐>
6 <entry key="ext_stopwords">location</entry>
7 <!‐‐用户可以在这里配置远程扩展字典 ‐‐>
8 <entry key="remote_ext_dict">words_location</entry>
9 <!‐‐用户可以在这里配置远程扩展停止词字典‐‐>
10 <entry key="remote_ext_stopwords">words_location</entry>
11 </properties>
12

高亮显示

在搜索中,经常需要对搜索关键字做高亮显示,高亮显示也有其常用的参数,在这个案例中
做一些常用参数的介绍。
现在搜索cars索引中remark字段中包含“大众”的document。并对“XX关键字”做高亮显
示,高亮效果使用html标签<span>,并设定字体为红色。如果remark数据过长,则只显示前20个
字符。

1 PUT /news_website
2 {
3 "mappings": {
4
5 "properties": {
6 "title": {
7 "type": "text",
8 "analyzer": "ik_max_word"
9 },
10 "content": {
11 "type": "text",
12 "analyzer": "ik_max_word"
13 }
14 }
15 }
16
17 }
18
19
20 PUT /news_website
21 {
22 "settings" : {
23 "index" : {
24 "analysis.analyzer.default.type": "ik_max_word"
25 }
26 }
27 }
28
29
30
31
32 PUT /news_website/_doc/1
33 {
34 "title": "这是我写的第一篇文章",
35 "content": "大家好,这是我写的第一篇文章,特别喜欢这个文章门户网站!!!"
36 }
37
38 GET /news_website/_doc/_search
39 {
40 "query": {
41 "match": {
42 "title": "文章"
43 }
44 },
45 "highlight": {
46 "fields": {
47 "title": {}
48 }
49 }
50 }
51
52 {
53 "took" : 458,
54 "timed_out" : false,
55 "_shards" : {
56 "total" : 1,
57 "successful" : 1,
58 "skipped" : 0,
59 "failed" : 0
60 },
61 "hits" : {
62 "total" : {
63 "value" : 1,
64 "relation" : "eq"
65 },
66 "max_score" : 0.2876821,
67 "hits" : [
68 {
69 "_index" : "news_website",
70 "_type" : "_doc",
71 "_id" : "1",
72 "_score" : 0.2876821,
73 "_source" : {
74 "title" : "我的第一篇文章",
75 "content" : "大家好,这是我写的第一篇文章,特别喜欢这个文章门户网站!!!"
76 },
77 "highlight" : {
78 "title" : [
79 "我的第一篇<em>文章</em>"
80 ]
81 }
82 }
83 ]
84 }
85 }
86
87 <em></em>表现,会变成红色,所以说你的指定的field中,如果包含了那个搜索词的话,就会在
那个field的文本中,对搜索词进行红色的高亮显示
88
89 GET /news_website/_doc/_search
90 {
91 "query": {
92 "bool": {
93 "should": [
94 {
95 "match": {
96 "title": "文章"
97 }
98 },
99 {
100 "match": {
101 "content": "文章"
102 }
103 }
104 ]
105 }
106 },
107 "highlight": {
108 "fields": {
109 "title": {},
110 "content": {}
111 }
112 }
113 }
114
115 highlight中的field,必须跟query中的field一一对齐的
116
117 2、常用的highlight介绍
118
119 plain highlight,lucene highlight,默认
120
121 posting highlight,index_options=offsets
122
123 (1)性能比plain highlight要高,因为不需要重新对高亮文本进行分词
124 (2)对磁盘的消耗更少
125
126
127 DELETE news_website
128 PUT /news_website
129 {
130 "mappings": {
131 "properties": {
132 "title": {
133 "type": "text",
134 "analyzer": "ik_max_word"
135 },
136 "content": {
137 "type": "text",
138 "analyzer": "ik_max_word",
139 "index_options": "offsets"
140 }
141 }
142 }
143 }
144
145 PUT /news_website/_doc/1
146 {
147 "title": "我的第一篇文章",
148 "content": "大家好,这是我写的第一篇文章,特别喜欢这个文章门户网站!!!"
149 }
150
151 GET /news_website/_doc/_search
152 {
153 "query": {
154 "match": {
155 "content": "文章"
156 }
157 },
158 "highlight": {
159 "fields": {
160 "content": {}
161 }
162 }
163 }
164
165 fast vector highlight
166
167 index‐time term vector设置在mapping中,就会用fast verctor highlight
168
169 (1)对大field而言(大于1mb),性能更高
170
171 delete /news_website
172
173 PUT /news_website
174 {
175 "mappings": {
176 "properties": {
177 "title": {
178 "type": "text",
179 "analyzer": "ik_max_word"
180 },
181 "content": {
182 "type": "text",
183 "analyzer": "ik_max_word",
184 "term_vector" : "with_positions_offsets"
185 }
186 }
187 }
188 }
189
190 强制使用某种highlighter,比如对于开启了term vector的field而言,可以强制使用plain h
ighlight
191
192 GET /news_website/_doc/_search
193 {
194 "query": {
195 "match": {
196 "content": "文章"
197 }
198 },
199 "highlight": {
200 "fields": {
201 "content": {
202 "type": "plain"
203 }
204 }
205 }
206 }
207
208 总结一下,其实可以根据你的实际情况去考虑,一般情况下,用plain highlight也就足够了,
不需要做其他额外的设置
209 如果对高亮的性能要求很高,可以尝试启用posting highlight
210 如果field的值特别大,超过了1M,那么可以用fast vector highlight
211
212 3、设置高亮html标签,默认是<em>标签
213
214 GET /news_website/_doc/_search
215 {
216 "query": {
217 "match": {
218 "content": "文章"
219 }
220 },
221 "highlight": {
222 "pre_tags": ["<span color='red'>"],
223 "post_tags": ["</span>"],
224 "fields": {
225 "content": {
226 "type": "plain"
227 }
228 }
229 }
230 }
231
232 4、高亮片段fragment的设置
233
234 GET /_search
235 {
236 "query" : {
237 "match": { "content": "文章" }
238 },
239 "highlight" : {
240 "fields" : {
241 "content" : {"fragment_size" : 150, "number_of_fragments" : 3 }
242 }
243 }
244 }
245
246 fragment_size: 你一个Field的值,比如有长度是1万,但是你不可能在页面上显示这么长
啊。。。设置要显示出来的fragment文本判断的长度,默认是100
247 number_of_fragments:你可能你的高亮的fragment文本片段有多个片段,你可以指定就显示几
个片段
248

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

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

相关文章

QT6 源(93)篇三:阅读与注释共用体类 QVariant 及其源代码,本类支持比较运算符 ==、!=。

&#xff08;9&#xff09; 本类支持比较运算符 、! &#xff1a; 可见&#xff0c; QString 类型里可存储多个 unicode 字符&#xff0c;即使只存储一个 unicode 字符也不等于 QChar。 &#xff08;10&#xff09;本源代码来自于头文件 qvariant . h &#xff1a; #ifndef Q…

Maven私服搭建与登录全攻略

目录 1.背景2.简介3.安装4.启动总结参考文献 1.背景 回顾下maven的构建流程&#xff0c;如果没有私服&#xff0c;我们所需的所有jar包都需要通过maven的中央仓库或者第三方的maven仓库下载到本地&#xff0c;当一个公司或者一个团队所有人都重复的从maven仓库下载jar包&#…

力扣210(拓扑排序)

210. 课程表 II - 力扣&#xff08;LeetCode&#xff09; 这是一道拓扑排序的模板题。简单来说&#xff0c;给出一个有向图&#xff0c;把这个有向图转成线性的排序就叫拓扑排序。如果有向图中有环就没有办法进行拓扑排序了。因此&#xff0c;拓扑排序也是图论中判断有向无环图…

C++ asio网络编程(5)简单异步echo服务器

上一篇文章:C asio网络编程(4)异步读写操作及注意事项 文章目录 前言一、Session类1.代码2.代码详解3.实现Session类1.构造函数2.handle_read3.介绍一下boost的封装函数和api4.handle_write 二、Server类1.代码2.代码思路详解 三、客户端四、运行截图与流程图 前言 提示&…

【机器人】复现 UniGoal 具身导航 | 通用零样本目标导航 CVPR 2025

UniGoal的提出了一个通用的零样本目标导航框架&#xff0c;能够统一处理多种类型的导航任务。 支持 对象类别导航、实例图像目标导航和文本目标导航&#xff0c;而无需针对特定任务进行训练或微调。 本文分享UniGoal复现和模型推理的过程&#xff5e; 查找沙发&#xff0c;模…

spring中的@PropertySource注解详解

一、核心功能与作用 PropertySource是Spring框架中用于加载外部配置文件的核心注解&#xff0c;主要作用是将属性文件&#xff08;如.properties、.yml&#xff09;的键值对加载到Spring的Environment环境中&#xff0c;实现配置与代码的解耦。其核心价值包括&#xff1a; 外部…

二极管钳位电路——Multisim电路仿真

目录 二极管钳位电路 2.1 二极管正向钳位电路 二极管压降测试 2.1.1 二极管正向钳位电路图 2.1.2 二极管正向钳位工作原理 2.2 二极管负向钳位电路 2.2.1 二极管负向钳位电路图 2.2.2 二极管负向钳位工作原理 二极管正向反向钳位仿真电路实验结果 2.3 二极管顶部钳位…

suricata增加单元测试编译失败

一、环境 $ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 22.04.5 LTS Release: 22.04 Codename: jammysuricata: suricata7.0.5 IDE: vscode 二、背景 在suricata中开发了某个功能后&#xff0c;增加unittest时&#xff0c;…

高并发场景下的BI架构设计:衡石分布式查询引擎与缓存分级策略

在电商大促、金融交易时段或IoT实时监控场景中&#xff0c;企业BI系统常面临瞬时万级并发查询的冲击——运营团队需要实时追踪GMV波动&#xff0c;风控部门需秒级响应欺诈检测&#xff0c;产线监控需毫秒级反馈设备状态。传统单体架构的BI系统在此类场景下极易崩溃&#xff0c;…

鱼眼摄像头(一)多平面格式 单缓冲读取图像并显示

鱼眼摄像头&#xff08;一&#xff09;多平面格式 单缓冲读取图像并显示 1.摄像头格式 1. 单平面格式&#xff08;Single Plane&#xff09;&#xff1a;各通道数据保存在同一个平面&#xff08;缓冲&#xff09;&#xff0c;图像数据按行连续存储a. mjpeg&#xff0c;yuyv等…

机器学习笔记——特征工程

大家好&#xff0c;这里是好评笔记&#xff0c;公主号&#xff1a;Goodnote&#xff0c;专栏文章私信限时Free。本笔记介绍机器学习中常见的特征工程方法、正则化方法和简要介绍强化学习。 文章目录 特征工程&#xff08;Fzeature Engineering&#xff09;1. 特征提取&#xff…

A Survey of Learning from Rewards:从训练到应用的全面剖析

A Survey of Learning from Rewards&#xff1a;从训练到应用的全面剖析 你知道大语言模型&#xff08;LLMs&#xff09;如何通过奖励学习变得更智能吗&#xff1f;这篇论文将带你深入探索。从克服预训练局限的新范式&#xff0c;到训练、推理各阶段的策略&#xff0c;再到广泛…

Python爬虫第20节-使用 Selenium 爬取小米商城空调商品

目录 前言 一、 本文目标 二、环境准备 2.1 安装依赖 2.2 配置 ChromeDriver 三、小米商城页面结构分析 3.1 商品列表结构 3.2 分页结构 四、Selenium 自动化爬虫实现 4.1 脚本整体结构 4.2 代码实现 五、关键技术详解 5.1 Selenium 启动与配置 5.2 页面等待与异…

Aware和InitializingBean接口以及@Autowired注解失效分析

Aware 接口用于注入一些与容器相关信息&#xff0c;例如&#xff1a; ​ a. BeanNameAware 注入 Bean 的名字 ​ b. BeanFactoryAware 注入 BeanFactory 容器 ​ c. ApplicationContextAware 注入 ApplicationContext 容器 ​ d. EmbeddedValueResolverAware 注入 解析器&a…

Unity3D仿星露谷物语开发41之创建池管理器

1、目标 在PersistentScene中创建池管理器&#xff08;Pool Manager&#xff09;。这将允许一个预制对象池被创建和重用。 在游戏中当鼠标点击地面时&#xff0c;便会启用某一个对象。比如点击地面&#xff0c;就创建了一棵树&#xff0c;而这棵树是从预制体对象池中获取的&a…

Modbus协议介绍

Modbus是一种串行通信协议&#xff0c;由Modicon公司&#xff08;现为施耐德电气&#xff09;在1979年为可编程逻辑控制器&#xff08;PLC&#xff09;通信而开发。它是工业自动化领域最常用的通信协议之一&#xff0c;具有开放性、简单性和跨平台兼容性&#xff0c;广泛应用于…

I/O多路复用(select/poll/epoll)

通过一个进程来维护多个Socket&#xff0c;也就是I/O多路复用&#xff0c;是一种常见的并发编程技术&#xff0c;它允许单个线程或进程同时监视多个输入/输出&#xff08;I/O&#xff09;流&#xff08;例如网络连接、文件描述符&#xff09;。当任何一个I/O流准备好进行读写操…

Westlake-Omni 情感端音频生成式输出模型

简述 github地址在 GitHub - xinchen-ai/Westlake-OmniContribute to xinchen-ai/Westlake-Omni development by creating an account on GitHub.https://github.com/xinchen-ai/Westlake-Omni Westlake-Omni 是由西湖心辰&#xff08;xinchen-ai&#xff09;开发的一个开源…

随手记录5

一些顶级思维&#xff1a; ​ 顶级思维 1、永远不要自卑。 也永远不要感觉自己比别人差&#xff0c;这个人有没有钱&#xff0c;有多少钱&#xff0c;其实跟你都没有关系。有很多人就是那个奴性太强&#xff0c;看到比自己优秀的人&#xff0c;甚至一些装逼的人&#xff0c;这…

Linux驱动:驱动编译流程了解

要求 1、开发板中的linux的zImage必须是自己编译的 2、内核源码树,其实就是一个经过了配置编译之后的内核源码。 3、nfs挂载的rootfs,主机ubuntu中必须搭建一个nfs服务器。 内核源码树 解压 tar -jxvf x210kernel.tar.bz2 编译 make x210ii_qt_defconfigmakeCan’t use ‘…