ElasticSearch 中的单词匹配

Word matching in ElasticSearch

这就是我映射索引的方式。

 settings = new ConnectionSettings(pool)
              .DefaultIndex(defaultIndex)
              .MapDefaultTypeNames(m => m.Add(typeof(MyClass), "content"))
              .PrettyJson()
              .DisableDirectStreaming());

假设我有一个索引到 ES 的文档

content: "Tim Cook revealed during the earnings call that iPhone sales in India grew by 56% on a yearly basis, despite the company's first global sales decline in 13 years."

现在,如果用户使用匹配查询搜索某个词

{
  "query": {
    "match": {
      "answer": {
        "query": "decline"
      }
    }
}}

假设我得到 0.047 分。

但是对于相同的查询,如果我搜索单词 "declining",我得到的分数是 0。我想检查该词是否部分出现在文档中。我该怎么做?

您需要通过声明一个特定的分析器来定义您的字段映射来分析您的内容字段。在这种情况下,我们可以使用 english language analyzer 来根据英语语法和词汇规则对标记进行词干处理。

PUT your_index
{
  "mappings": {
    "your_type": {
      "properties": {
        "content": {
          "type": "string",
          "analyzer": "english"
        }
      }
    }
  }
}

然后你可以索引你的内容

PUT /your_index/your_type/1
{
  "content": "Tim Cook revealed during the earnings call that iPhone sales in India grew by 56% on a yearly basis, despite the company's first global sales decline in 13 years."
}

最后您可以同时搜索 declinedeclining 并获得相同的分数

POST /your_index/_search 
{
  "query": {
    "match": {
      "content": {
        "query": "decline"
      }
    }
  }
}

POST /your_index/_search 
{
  "query": {
    "match": {
      "content": {
        "query": "declining"
      }
    }
  }
}