简体   繁体   English

写一个Elasticsearch查询过滤掉一句话

[英]Writing a Elasticsearch query to filter out a sentence

I'm writing a python program to query my elastic search index with a constraint that I have to retrieve all documents that contains three words (apple, mango, strawberry) while not containing a sentence ("this is not a fruit").我正在编写一个 python 程序来查询我的弹性搜索索引,其约束条件是我必须检索包含三个单词(苹果、芒果、草莓)但不包含句子(“这不是水果”)的所有文档。 It seems like my query is okay except for the must_not part.除了 must_not 部分外,我的查询似乎没问题。 It is retrieving correct documents that contain either of the three keywords, but seems like it doesn't filter out based on the sentence.它正在检索包含三个关键字之一的正确文档,但似乎它没有根据句子过滤掉。 The following is my query:以下是我的查询:

  res= es.search(index='fruit_box', body={

     'query': {

        "bool": {
            "must": [
                {
                   "query_string": {
                       "query": "(apple) OR (mango) OR (strawberry)"
                   }
                }
            ],
            "must_not": [
                {
                    "match": {
                        "text": "this is not a fruit"
                    }
                }
            ]
        }
    }
})

Am I missing something here?我在这里错过了什么吗? Thanks for any help in advance感谢您提前提供任何帮助

your must_not clause is not in AND with the query_string one.您的must_not子句不在与query_string的 AND 中。 For doing so, you have to move the must_not clause within the must array.为此,您必须在must数组中移动must_not子句。 Ie, IE,

"query": {
  "bool": {
    "must": [
      {
        "query_string": {
          "query": "(apple) OR (mango) OR (strawberry)"
        }
      },
      {
        "bool": {
          "must_not": {
            "match": {
              "text": "this is not a fruit"
            }
          }
        }
      }
    ]
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM