当前位置: 首页 > 面试题库 >

ElasticSearch:使用edge_ngram和模糊性进行部分/完全评分

郑宇
2023-03-14
问题内容

在ElasticSearch中,我尝试使用带有模糊性的edge_ngram获得正确的评分。我希望精确匹配具有最高的分数,而子匹配具有较低的分数。以下是我的设置和评分结果。

settings: {
          number_of_shards: 1,
          analysis: {
             filter: {
                ngram_filter: {
                   type: 'edge_ngram',
                   min_gram: 2,
                   max_gram: 20
                }
             },
             analyzer: {
                ngram_analyzer: {
                   type: 'custom',
                   tokenizer: 'standard',
                   filter: [
                      'lowercase',
                      'ngram_filter'
                   ]
                }
             }
          }
       },
    mappings: [{
          name: 'voter',
          _all: {
                'type': 'string',
                'index_analyzer': 'ngram_analyzer',
                'search_analyzer': 'standard'
             },
             properties: {
                last: {
                   type: 'string',
                   required : true,
                   include_in_all: true,
                   term_vector: 'yes',
                   index_analyzer: 'ngram_analyzer',
                   search_analyzer: 'standard'
                },
                first: {
                   type: 'string',
                   required : true,
                   include_in_all: true,
                   term_vector: 'yes',
                   index_analyzer: 'ngram_analyzer',
                   search_analyzer: 'standard'
                },

             }

       }]

在执行了名字为“ Michael”的POST之后,我进行了如下查询,并更改了“ Michael”,“ Michae”,“ Micha”,“ Mich”,“
Mic”和“ Mi”。

GET voter/voter/_search
{
 "query": {
    "match": {
      "_all": {
        "query": "Michael",
        "fuzziness": 2,
        "prefix_length": 1
      }
    }
  }
}

我的成绩是:

-"Michael": 0.19535106
-"Michae": 0.2242768
-"Micha": 0.24513611
-"Mich": 0.22340237
-"Mic": 0.21408978
-"Mi": 0.15438235

如您所见,得分结果没有达到预期。我希望“ Michael”的得分最高,而“ Mi”的得分最低

任何帮助,将不胜感激!


问题答案:

解决此问题的一种方法是像这样在映射中添加文本的原始版本

                   last: {
                       type: 'string',
                       required : true,
                       include_in_all: true,
                       term_vector: 'yes',
                       index_analyzer: 'ngram_analyzer',
                       search_analyzer: 'standard',
                       "fields": {
                            "raw": { 
                               "type":  "string"  <--- index with standard analyzer
                              }
                          }
                    },
                    first: {
                       type: 'string',
                       required : true,
                       include_in_all: true,
                       term_vector: 'yes',
                       index_analyzer: 'ngram_analyzer',
                       search_analyzer: 'standard',
                       "fields": {
                            "raw": { 
                               "type":  "string"  <--- index with standard analyzer
                              }
                          }
                    },

你也可以把它 精确index : not_analyzed

然后您可以像这样查询

{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "_all": {
              "query": "Michael",
              "fuzziness": 2,
              "prefix_length": 1
            }
          }
        },
        {
          "match": {
            "last.raw": {
              "query": "Michael",
              "boost": 5
            }
          }
        },
        {
          "match": {
            "first.raw": {
              "query": "Michael",
              "boost": 5
            }
          }
        }
      ]
    }
  }
}

匹配更多条款的文档得分更高。您可以boost根据需要指定。



 类似资料:
  • 本文向大家介绍Java如何使用elasticsearch进行模糊查询,包括了Java如何使用elasticsearch进行模糊查询的使用技巧和注意事项,需要的朋友参考一下 这篇文章主要介绍了Java如何使用elasticsearch进行模糊查询,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 使用环境上篇文章本人已书写过,需要maven坐标,ES连

  • 问题内容: 看起来Elasticsearch支持模糊查询(http://www.elasticsearch.org/guide/reference/query- dsl/fuzzy-query/ ),但我想不出办法让django-haystack通过该选项。 我研究了django-haystack搜索,在使用elasticsearch后端时,好像在使用“ match_all”查询。是否有可能获得模

  • 在ES中,我看到了一个边缘标记器(https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-edgengram-tokenizer.html)和edgeNGram类型(ElasticSearch edgeNGram)。egde_ngram令牌化器不是edgeNGram类型的令牌化器的别名吗?

  • 问题内容: 我正在使用Elasticsearch构建URL索引。 我将一个URL提取为3个部分,分别是“域”,“路径”和“查询”。 例如:将分为 当我想在索引中部分搜索域时出现问题,例如“ user = who”或“ ing.com”。 甚至在索引时没有使用“ Analyzer”时,是否可以使用“ Analyzer”? 如何基于分析仪进行部分搜索? 非常感谢你。 问题答案: 2种方法: 1.通配符

  • 一些背景信息:这个项目是一个简单的图像,在项目结束时,它将成为我电脑屏幕的背景。 我想模糊背景的一部分,这样文本的一部分就更清晰了。我可以模糊图像中文本本身的部分,但这是我最后的选择。我不想这样做,因为在未来的项目中,我想随着一些东西的移动而主动模糊背景(我还没有开始这个未来的项目,所以我无法更好地描述这个项目)。 有人知道如何模糊背景的一部分吗?对于这个项目,它需要大约400x200像素,模糊1

  • 问题内容: 据我了解,为了使Elasticsearch执行涉及“相关性得分”的“模糊”查询,即使“限制”,它也必须遍历并计算所有可能匹配项(可能包括数千行或数百万行)的相关性该查询上的值仅为“ 10”。 Elasticsearch如何做到这一点,同时提供合理的响应时间? 问题答案: 在Lucene 4中,模糊搜索会修剪搜索空间,而不是像以前那样强行使用它: Lucene的FuzzyQuery在4.