🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# Similarity module(相似模块) 原文链接 : [https://www.elastic.co/guide/en/elasticsearch/reference/5.3/index-modules-similarity.html](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/index-modules-similarity.html) 译文链接 : [Similarity module(相似模块)](/pages/viewpage.action?pageId=10028541) 贡献者 : @苏涛,[ApacheCN](/display/~apachecn),[Apache中文网](/display/~apachechina) 相似性(得分/排名模型)定义了匹配文档如何进行评分。相似性是针对字段的,意味着通过映射可以对每个字段定义不同的相似性模块。 ### 配置相似性 大多数现有或自定义相似性具有可以通过索引设置进行配置的配置选项,如下所示,可以在创建索引或更新索引设置时提供索引选项。 ``` "similarity" : { "my_similarity" : { "type" : "DFR", "basic_model" : "g", "after_effect" : "l", "normalization" : "h2", "normalization.h2.c" : "3.0" } } ``` 这里我们配置DFRSimilarity,所以它可以在映射中被引用为my_similarity,如下例所示: ``` { "book" : { "properties" : { "title" : { "type" : "text", "similarity" : "my_similarity" } } } ``` ### 可用的相似性 #### BM25相似性(默认值) 基于TF / IDF的相似性,内置了tf标准化,并且对短字段(如名称)效率更高。有关详细信息,请参阅[Okapi_BM25](https://en.wikipedia.org/wiki/Okapi_BM25)。这种相似性有以下选项: | k1 | 控制非线性索引词频率标准(饱和度)。默认值为**1.2** | | b | 控制文档长度标准化到tf值的程度。默认值为**0.75** | | discount_overlaps | 决定重叠词元(词元的位置增量为0)是否要被忽略。默认值为**true**,意味着重叠词元不会被统计 | 类型名:BM25 #### Classic相似性 基于TF / IDF模型的classic相似性。这种相似性有以下选项: | discount_overlaps | 决定重叠词元(词元的位置增量为0)是否要被忽略。默认值为**true**,意味着重叠词元不会被统计 | 类型名:classic #### DFR相似性 实现[divergence from randomness](http://lucene.apache.org/core/5_2_1/core/org/apache/lucene/search/similarities/DFRSimilarity.html)框架。这种相似性有以下选项: | basic_model | 可能的值有:`**be**,``**d**,` `**g**,` `**if**,` `**in**,` **`ine`** 和 **`p`** | | after_effect | 可能的值有: **`no`**,**`b`** 和 **`l`** | | normalization | 可能的值有: `**no**,``**h1**,` `**h2**,`**`h3`** 和 **`z`** | 类型名:DFR #### DFI相似性 实现[divergence from independence](http://trec.nist.gov/pubs/trec21/papers/irra.web.nb.pdf)模块,这种相似性有以下选项: | independence_measure | 可能的值 `standardized`,`**saturated**`,**`chisquared`** | 类型名:DFI #### IB相似模块 [Information based model](http://lucene.apache.org/core/5_2_1/core/org/apache/lucene/search/similarities/IBSimilarity.html) 。该算法基于以下概念:任何符号分布序列中的信息内容主要由其基本元素的重复使用决定。对于书面文本,这个方式会对比不同作者的写作风格。这种相似性有以下选项: | distribution | 可能的值: **`ll`** 和 **`spl`** | | lambda | 可能的值:**`df`** 和 **`ttf`** | | normalization | 与DFR相似度相同。 | 类型名:IB #### LM Dirichlet相似模块 [LM Dirichlet similarity](http://lucene.apache.org/core/5_2_1/core/org/apache/lucene/search/similarities/LMDirichletSimilarity.html)。这种相似性有以下选项: | mu | 默认值为**2000** | 类型名:LMDirichlet #### LM Jelinek Mercer相似模块[](http://lucene.apache.org/core/5_2_1/core/org/apache/lucene/search/similarities/LMJelinekMercerSimilarity.html) [LM Jelinek Mercer similarity](http://lucene.apache.org/core/5_2_1/core/org/apache/lucene/search/similarities/LMJelinekMercerSimilarity.html)。该算法尝试捕获文本中的图像并去除噪点。这种相似性有以下选项: | lambda | 最优值取决于采集和查询。标题查询的最佳值约为**0.1**,长查询的最优值为**0.7**。默认为**0.1**。当值接近0时,匹配更多查询索引词的文档会比匹配较少索引词的文档的排列位置更靠前。 | 类型名:LMJelinekMercer ### 默认和基础相似性 默认情况下,Elasticsearch将使用任何配置为default的相似性模块。然而,queryNorm()和coord()的相似度函数不是每个字段都会执行。因此,对于想要更改用于这两种方法的实现的专家用户,在不更改默认值的情况下,可以使用base名配置相似性。这种相似性将用于这两种方法。 [创建索引](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/indices-create-index.html)时,可以更改索引中所有字段的默认相似度: ``` PUT /my_index { "settings": { "index": { "similarity": { "default": { "type": "classic" } } } } } ``` 如果要在创建索引后更改默认的相似性,则必须[关闭](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/indices-open-close.html)索引,然后发送以下请求并重新[打开](https://www.elastic.co/guide/en/elasticsearch/reference/5.3/indices-open-close.html): ``` PUT /my_index/_settings { "settings": { "index": { "similarity": { "default": { "type": "classic" } } } } } ```