ElasticSearch 底层原理与分组查询(上)

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: ElasticSearch 底层原理与分组查询

一、ElasticSearch 文档分值 _score 计算底层原理

1)boolean model


根据用户的query条件,先过滤出包含指定 term(关键字) 的 doc(文档)


  • query "hello world" ‐‐> hello / world / hello & world


  • bool ‐‐> must/must not/should ‐‐> 过滤 ‐‐> 包含 / 不包含 / 可能包含


  • doc ‐‐> 不打分数 ‐‐> 正或反 true or false ‐‐> 为了减少后续要计算的doc的数量,提升性能

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、分析一个document上的_score是如何被计算出来的


GET /es_db/_doc/1/_explain 
{ 
"query": { 
"match": { 
"remark": "java developer" 
} 
} 
} 

二、分词器工作流程

1、切分词语,normalization


给你一段句子,然后将这段句子拆分成一个一个的单个的单词,同时对每个单词进行

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/th 
e/an ‐‐> 干掉,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, tran

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)" 
} 

3、定制分词器

1)默认的分词器


standard


standard tokenizer:以单词边界进行切分


standard token filter:什么都不做


lowercase token filter:将所有字母转换为小写


stop token filer(默认被禁用):移除停用词,比如a the it等等


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"
    }
  }
} 
复制代码

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,已经有了常用的中文停用词,可以补充自己的停用词,然后重启es1 IK分词器源码下载:


github.com/medcl/elast…


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> 

三. 高亮显示


在搜索中,经常需要对搜索关键字做高亮显示,高亮显示也有其常用的参数,在这个案例中做一些常用参数的介绍。


现在搜索cars索引中remark字段中包含“大众”的document。并对“XX关键字”做高亮显示,高亮效果使用html标签,并设定字体为红色。如果remark数据过长,则只显示前 20 个字符。


PUT /news_website
{
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "ik_max_word"
},
"content": {
"type": "text",
"analyzer": "ik_max_word"
}
}
}
}
PUT /news_website 
{ 
"settings" : { 
"index" : { 
"analysis.analyzer.default.type": "ik_max_word" 
} 
} 
} 
PUT /news_website/_doc/1 
{ 
"title": "这是我写的第一篇文章", 
"content": "大家好,这是我写的第一篇文章,特别喜欢这个文章门户网站!!!" 
} 


查询 title : "文章"


GET /news_website/_doc/_search
{
  "query":{
   "match":{
     "title":"文章"
   }
   },
   "highlight":{
     "fields":{
       "title":{}}
   }
}


查询结果


{
  "took" : 878,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "news_website",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.2876821,
        "_source" : {
          "title" : "这是我写的第一篇文章",
          "content" : "大家好,这是我写的第一篇文章,特别喜欢这个文章门户网站!!!"
        },
        "highlight" : {
          "title" : [
            "这是我写的第一篇<em>文章</em>"
          ]
        }
      }
    ]
  }
}


相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
4天前
|
存储 搜索推荐 数据挖掘
ElasticSearch架构介绍及原理解析
ElasticSearch架构介绍及原理解析
143 0
|
4天前
|
存储 固态存储 Java
Elasticsearch中查询性能优化
Elasticsearch中查询性能优化
199 0
|
4天前
Elasticsearch之RestClient查询文档
Elasticsearch之RestClient查询文档
140 1
|
4天前
|
自然语言处理 API 索引
Elasticsearch Analyzer原理分析并实现中文分词
Elasticsearch Analyzer原理分析并实现中文分词
75 0
|
4天前
|
存储 缓存 搜索推荐
深入理解Elasticsearch倒排索引原理与优化策略
总之,Elasticsearch的倒排索引是其高效全文搜索的核心。为了提高性能和可伸缩性,Elasticsearch采用了多种优化策略,包括压缩、分片、合并、位集合和近实时搜索等。这些策略使Elasticsearch成为处理大规模文本数据的强大工具。
13 0
|
4天前
|
存储 SQL 运维
Elasticsearch 查询革新:探索 Wildcard 类型的高效模糊匹配策略
Elasticsearch 查询革新:探索 Wildcard 类型的高效模糊匹配策略
24 0
|
4天前
|
运维 测试技术 数据处理
Elasticsearch 优化查询中获取字段内容的方式,性能提升5倍!
Elasticsearch 优化查询中获取字段内容的方式,性能提升5倍!
18 0
|
4天前
|
存储 缓存 Java
Elasticsearch 8.X 聚合查询下的精度问题及其解决方案
Elasticsearch 8.X 聚合查询下的精度问题及其解决方案
17 0
|
4天前
|
搜索推荐 算法 数据挖掘
探索 Elasticsearch 8.X Terms Set 检索的应用与原理
探索 Elasticsearch 8.X Terms Set 检索的应用与原理
13 0
|
4天前
|
自然语言处理 Java 索引
SpringBoot 实现 elasticsearch 查询操作(RestHighLevelClient 的案例实战)
SpringBoot 实现 elasticsearch 查询操作(RestHighLevelClient 的案例实战)
25 1

热门文章

最新文章