ElasticSearch7.3|ElasticSearch7.3 学习之倒排索引揭秘及初识分词器(Analyzer)
一、倒排索引
1. 构建倒排索引
例如说有下面两个句子doc1,doc2
doc1:I really liked my small dogs, and I think my mom also liked them.
doc2:He never liked any dogs, so I hope that my mom will not expect me to liked him.
首先进行英文分词,这个阶段就是初步的倒排索引的建立
term | doc1 | doc2 |
---|---|---|
I | * | * |
really | * | |
liked | * | * |
my | * | * |
small | * | |
dogs | * | |
and | * | |
think | * | |
mom | * | * |
also | * | |
them | * | |
He | * | |
never | * | |
any | * | |
so | * | |
hope | * | |
that | * | |
will | * | |
not | * | |
expect | * | |
me | * | |
to | * | |
him | * |
2. 重建倒排索引
normalization正规化,建立倒排索引的时候,会执行一个操作,也就是说对拆分出的各个单词进行相应的处理,以提升后面搜索的时候能够搜索到相关联的文档的概率。比如说时态的转换,单复数的转换,同义词的转换,大小写的转换等
mom ―> mother
liked ―> like
small ―> little
dogs ―> dog
重新建立倒排索引,加入normalization,重建后的倒排索引如下
word | doc1 | doc2 | normalization |
---|---|---|---|
I | * | * | |
really | * | ||
like | * | * | liked ―> like |
my | * | * | |
little | * | small ―> little | |
dog | * | dogs ―> dog | |
and | * | ||
think | * | ||
mother | * | * | mom ―> mother |
also | * | ||
them | * | ||
He | * | ||
never | * | ||
any | * | ||
so | * | ||
hope | * | ||
that | * | ||
will | * | ||
not | * | ||
expect | * | ||
me | * | ||
to | * | ||
him | * |
再次用mother liked little dog搜索,就可以搜索到了。对搜索条件经行分词 normalization
mother -》mom
liked -》like
little -》small
dog -》dogs
这样的话doc1和doc2都会搜索出来
二、分词器 analyzer 1. 什么是分词器 analyzer
作用:简单来说就是切分词语。给你一段句子,然后将这段句子拆分成一个一个的单个的单词,同时对每个单词进行normalization(时态转换,单复数转换)
normalization的好处就是提升召回率(recall)
recall:搜索的时候,增加能够搜索到的结果的数量
analyzer 组成部分:
- character filter:在一段文本进行分词之前,先进行预处理,比如说最常见的就是,过滤html标签(hello --> hello),& --> and(I&you --> I and you)
- tokenizer:分词,hello you and me --> hello, you, and, me
- token filter:lowercase(小写转换),stop word(去除停用词),synonym(同义词处理),例如:dogs --> dog,liked --> like,Tom --> tom,a/the/an --> 干掉,mother --> mom,small --> little
2. 内置分词器的介绍
例句: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
官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.4/analysis-analyzers.html
文章图片
三、测试分词器
GET /_analyze
{
"analyzer": "standard",
"text": "Text to analyze 80"
}
返回值:
{
"tokens" : [
{
"token" : "text",
"start_offset" : 0,
"end_offset" : 4,
"type" : "",
"position" : 0
},
{
"token" : "to",
"start_offset" : 5,
"end_offset" : 7,
"type" : "",
"position" : 1
},
{
"token" : "analyze",
"start_offset" : 8,
"end_offset" : 15,
"type" : "",
"position" : 2
},
{
"token" : "80",
"start_offset" : 16,
"end_offset" : 18,
"type" : "",
"position" : 3
}
]
}
token:实际存储的term 关键字
position:在此词条在原文本中的位置
start_offset/end_offset:字符在原始字符串中的位置
【ElasticSearch7.3|ElasticSearch7.3 学习之倒排索引揭秘及初识分词器(Analyzer)】
推荐阅读
- 软件开发通识之二(如何从零开始学编程)
- 学习笔记(带你十天轻松搞定|学习笔记:带你十天轻松搞定 Go 微服务系列大结局(十)- 分布式事务)
- rodert教你学FFmpeg实战这一篇就够了
- 学习Spring源码篇之环境搭建
- 小学期第一套题总结与反思
- vue|基于spring cloud + nacos + gateway + ssm+的学生管理系统
- 机器学习|河南郑州二手房房价预测
- big|big data 入门
- Python全栈之学习CSS(2)
- 安全学习笔记-web安全之XSS攻击