ElasticSearch底层原理分析

1 ElasticSearch计算文档分值_score底层原理
1.1 boolean model
  根据用户的query条件,先过滤出包含指定term的doc
query "hello world" -->  hello / world / hello & world
bool --> must/must not/should --> 过滤 --> 包含 / 不包含 / 可能包含
doc --> 不打分数 --> 正或反 true or false --> 为了减少后续要计算的doc的数量,提升性能

1.2 relevance score算法

  简单来说,就是计算出,一个索引中的文本,与搜索文本,他们之间的关联匹配程度。
  Elasticsearch使用的是 term frequency/inverse document frequency算法,简称为TF/IDF算法。
  Term frequency:搜索文本中的各个词条在field文本中出现了多少次,出现次数越多,就越相关。
搜索请求:hello world
doc1:hello you, and world is very good
doc2:hello, how are you
  Inverse document frequency:搜索文本中的各个词条在整个索引的所有文档中出现了多少次,出现的次数越多,就越不相关。
搜索请求:hello world
doc1:hello, tuling is very good
doc2:hi world, how are you
  比如说,在index中有1万条document,hello这个单词在所有的document中,一共出现了1000次;world这个单词在所有的document中,一共出现了100次。
  Field-length norm:field长度,field越长,相关度越弱。
  搜索请求:hello world
doc1:{ "title": "hello article", "content": "...... N个单词" }
doc2:{ "title": "my article", "content": "...... N个单词,hi world" }
  hello world在整个index中出现的次数是一样多的。doc1更相关,title field更短。
 
2 分词器工作流程
2.1 切分词语
  给你一段句子,然后将这段句子拆分成一个一个的单个的单词,同时对每个单词进行normalization(时态转换,单复数转换),分词器。recall,召回率:搜索的时候,增加能够搜索到的结果的数量。
character filter:在一段文本进行分词之前,先进行预处理,比如说最常见的就是,过滤html标签(<span>hello<span> --> hello),& --> and(I&you --> I and you)
tokenizer:分词,hello you and me --> hello, you, and, me
token filter:lowercase,stop word,synonymom,liked --> like,Tom --> tom,a/the/an --> 干掉,small --> little

2.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)

stop analyzer:移除停用词,比如a the it等等

测试:
POST _analyze
{
"analyzer":"standard",
"text":"Set the shape to semi-transparent by calling set_trans(5)"
}

2.3 定制分词器

2.3.1 默认分词器  

  standard为默认的分词器。
  standard tokenizer:以单词边界进行切分
  standard token filter:什么都不做
  lowercase token filter:将所有字母转换为小写
  stop token filer(默认被禁用):移除停用词,比如a the it等等
 
2.3.2 修改分词器的设置
  启用english停用词token filter
PUT /my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "es_std": {
          "type": "standard",
          "stopwords": "_english_"
        }
      }
    }
  }
}

GET /my_index/_analyze
{
  "analyzer": "standard", 
  "text": "a dog is in the house"
}

GET /my_index/_analyze
{
  "analyzer": "es_std",
  "text":"a dog is in the house"
}

3、定制化自己的分词器

PUT /my_index
{
"settings": {
"analysis": {
"char_filter": {
"&_to_and": {
"type": "mapping",
"mappings": ["&=> and"]
}
},
"filter": {
"my_stopwords": {
"type": "stop",
"stopwords": ["the", "a"]
}
},
"analyzer": {
"my_analyzer": {
"type": "custom",
"char_filter": ["html_strip", "&_to_and"],
"tokenizer": "standard",
"filter": ["lowercase", "my_stopwords"]
}
}
}
}
}

GET /my_index/_analyze
{
"text": "tom&jerry are a friend in the house, <a>, HAHA!!",
"analyzer": "my_analyzer"
}

PUT /my_index/_mapping/my_type
{
"properties": {
"content": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}

2.3.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,一般,像停用词,会在分词的时候,直接被干掉,不会建立在倒排索引中。
 
2.3.4 IK分词器自定义词库
  自己建立词库:每年都会涌现一些特殊的流行词,网红,蓝瘦香菇,喊麦,鬼畜,一般不会在ik的原生词典里。
  自己补充自己的最新的词语,到ik的词库里面去,IKAnalyzer.cfg.xml:ext_dict,custom/mydict.dic
  补充自己的词语,然后需要重启es,才能生效。
  custom/ext_stopword.dic,已经有了常用的中文停用词,可以补充自己的停用词,然后重启es。
IK分词器源码下载:https://github.com/medcl/elasticsearch-analysis-ik/tree

2.3.5 IK热更新

  每次都是在es的扩展词典中,手动添加新词语,很坑
  (1)每次添加完,都要重启es才能生效,非常麻烦
  (2)es是分布式的,可能有数百个节点,你不能每次都一个一个节点上面去修改
  es不停机,直接我们在外部某个地方添加新的词语,es中立即热加载到这些新词语
  IKAnalyzer.cfg.xml如下:
<properties>
    <comment>IK Analyzer 扩展配置</comment>
    <!--用户可以在这里配置自己的扩展字典 -->
    <entry key="ext_dict">location</entry>
     <!--用户可以在这里配置自己的扩展停止词字典-->
    <entry key="ext_stopwords">location</entry>
    <!--用户可以在这里配置远程扩展字典 -->
    <entry key="remote_ext_dict">words_location</entry> 
    <!--用户可以在这里配置远程扩展停止词字典-->
    <entry key="remote_ext_stopwords">words_location</entry>
</properties>

 

posted @ 2022-07-19 17:05  檀潇兵  阅读(782)  评论(0编辑  收藏  举报