匹配elasticsearch中字段中的X或更多单词

时间:2016-06-23 04:39:00

标签: elasticsearch

例如,假设我有这些文件:

{ 
  "_id": 1
  "title": "The quick brown fox" 
}
{ 
  "_id": 2
  "title": "The quick brown fox jumps over the lazy dog" 
}
{ 
  "_id": 3
  "title": "The quick brown fox jumps over the quick dog" 
}
{ 
  "_id": 4
  "title": "Brown fox brown dog" 
}

如果我查询brown文件,将返回所有文件,但我想说“如果查询与两个或多个单词不匹配,则不匹配”或“如果查询与句子的某个百分比不匹配,它不匹配“

现在我正在使用分数来解决这个问题,但这很棘手,所以我想看看你是否知道更好的方法。

先谢谢。

1 个答案:

答案 0 :(得分:0)

documentation

中查看minimum_should_match

示例:

"query": {
    "bool": {
        "must": [{
            "term": {
                "thread.id": inputQuery 
            }
        }],
        "should": [
            {
                "match_phrase": {
                    "subject": inputQuery
                }
            },
            {
                "match_phrase": {
                    "body": inputQuery
                }
            }
        ],
        "minimum_should_match": 1    <-- in your case it will be 2
    }
}
相关问题