【问题标题】:Stemming queries elasticsearch词干查询弹性搜索
【发布时间】:2016-07-19 15:07:09
【问题描述】:

我已经成功地为 elasticsearch 实现了词干提取,因此当我搜索“代码”时,我会遇到“代码”和“编码”等。

当我尝试在查询中使用“must_not”字段时,出现了我的问题。当我在“must_not”字段中包含“code”时,这很好,我仍然可以按预期得到结果,但是当我搜索“codes”时,即使有包含单词“codes”的文档,我也没有得到任何结果" 肯定在其中。

我的查询如下:

for(i = 0; i < exclude_words.length; i++)
{
  must_not.push({term:{text:exclude_words[i].toLowerCase()}});
}
query = {
  "filtered": {
    "query": {
      "dis_max": {
        "queries": [
          {"match": {"text": term}},
          {"match": {"title": term}}
        ]
      }
    },
    "filter": {
      "bool": {
        "must_not": must_not
      }
    } 
  }
}

我正在使用 node.js 的 elasticsearch api 来构建我的查询并从 elasticsearch 获取结果。

我假设我遇到了这个问题是因为词干提取,并且“代码”在搜索索引中存储为“代码”。

有没有办法在不使用外部算法来阻止我的查询的情况下解决这个问题?或者有什么优雅的方法可以解决这个问题?

非常感谢任何帮助!

更新

这是我的分析仪:

{
 "settings": {
  "analysis": {
    "analyzer": {
      "stopword_analyzer": { 
        "type": "snowball", 
        "stopwords": ["a", "able", "about", "across", "after", "all",      "almost", "also", "am", "among", "an", "and", "any", "are", "as", "at", "be", "because", "been", "but", "by", "can", "cannot", "could", "dear", "did", "do", "does", "either", "else", "ever","every", "for", "from", "get", "got", "had", "has", "have", "he", "her", "hers", "him", "his", "how", "however", "i", "if", "in", "into", "is", "it", "its", "just", "least", "let", "like",  "may", "me", "might", "most", "must", "my", "neither", "no", "nor", "not", "of", "off", "often", "on", "only", "or", "other", "our", "own", "rather", "said", "say", "says", "she", "should", "since", "so", "some", "than", "that", "the", "their", "them", "then", "there", "these", "they", "this", "tis", "to", "too", "us", "wants", "was", "we", "were", "what", "when", "where", "which", "while", "who", "whom", "why", "will", "with", "would", "yet", "you", "your"]
     }
   }
 }
}

文本字段具有以下映射:

"text": {
    "type": "string",
    "analyzer": "stopword_analyzer"
  }

【问题讨论】:

  • text字段的映射是什么?
  • 用它更新了问题,谢谢!

标签: javascript node.js elasticsearch


【解决方案1】:

当我在“must_not”字段中包含“代码”时,这很好,我仍然可以得到预期的结果

这与must_not 无关,而与您在must_not 中使用的term 过滤器有关。 term 过滤器将获取您的搜索文本 - “代码”或“代码”或其他任何内容 - 它将使用确切的值进行过滤。

但是,您使用的分析器正在更改被索引的术语。 例如,如果您想要索引 "coding",您实际上将在索引 "code" 中有(作为倒排索引中的术语)。请记住,term 实际上会搜索确切的值。因此,如果您搜索“代码”,则不会找到它,因为您文档中的单个术语是“代码”。

我建议在must_not 部分尝试match 而不是term,因为这也会在搜索时使用分析器。像这样的:

  "filter": {
    "bool": {
      "must_not": [
        {
          "query": {
            "match": {
              "text": "codes"
            }
          }
        },
        {
          "query": {
            "match": {
              "text": "coding"
            }
          }
        }
      ]
    }
  }

【讨论】:

  • 完美!就像一个魅力,非常感谢你,你帮我省了很多工作!!!
猜你喜欢
  • 2012-07-11
  • 1970-01-01
  • 1970-01-01
  • 2013-10-07
  • 2014-11-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多