search - elasticsearch ngrams:为什么匹配较短的 token 而不是较长?

标签 search lucene elasticsearch n-gram

我有一个带有以下映射和分析器的索引:

settings: {
    analysis: {
      char_filter: {
        custom_cleaner: {
          # remove - and * (we don't want them here)
          type: "mapping",
          mappings: ["-=>", "*=>"]
        }
      },
      analyzer: {
        custom_ngram: {
          tokenizer: "standard",
          filter: [ "lowercase", "custom_ngram_filter" ],
          char_filter: ["custom_cleaner"]
        }
      },
      filter: {
        custom_ngram_filter: {
          type: "nGram",
          min_gram: 3,
          max_gram: 20,
          token_chars: [ "letter", "digit" ]
        }
      }
    }
  },
  mappings: {
    attributes: {
      properties: {
        name: { type: "string"},
        words: { type: "string", similarity: "BM25", analyzer: "custom_ngram" }
      }
    }
  }
}

我在索引中有以下2个文档:
"name": "shirts", "words": [ "shirt"]

"name": "t-shirts", "words": ["t-shirt"]
我执行多重比对查询
"query": {

            "multi_match": {
               "query": "t-shirt",
               "fields": [
                  "words",
                  "name"
               ],
               "analyzer": "custom_ngram"
            }

   }

问题:

衬衫的得分为1.17,而T恤的得分为0.8。
为什么会这样,我如何才能使T恤(直接匹配)获得更高的分数?

在另一个用例中,我需要使用ngrams,其中必须检测包含匹配项。 (衬衫是肌肉衬衫,...)因此,我想我不能跳过ngram。

谢谢!

最佳答案

我相信这是因为您使用的是StandardTokenizer,它将字符串“t-shirt”标记化为标记“t”和“shirt”。但是,“t”短于最小克大小,因此不会生成任何 token 。因此,在每种情况下,您都将获得相同的匹配项,但是带有t-shirt的文档较长,因此得分较低。

您可以使用Explain API获得有关为什么文档获得分数的详细信息。

您确定需要使用ngram吗?您的示例“muscle-shirt”中的“shirt”应该由StandardAnalyzer处理,它将在连字符上标记化。

关于search - elasticsearch ngrams:为什么匹配较短的 token 而不是较长?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22665088/

相关文章:

c - 通过结构数组搜索用户输入的字符串

search - 当数据增长时,如何在不增加分片大小的情况下处理负载?

Elasticsearch:查找具有不同值的文档,然后对它们进行聚合

elasticsearch - stormcrawler:indexer.md.mapping-如果元数据标记不存在会发生什么?

elasticsearch - Elasticsearch 中一个节点可以有多个分片吗?

java - 返回二维数组的索引

html - 每次更新站点地图都需要提交给搜索引擎吗?

solr - 使用SOLR索引时使用Elasticsearch查询索引时要注意什么?

linux - 生产中的 Sunspot Solr

java - 如何在我的远程服务器上部署 elasticsearch-head 或其他浏览器前端监控工具