在ElasticSearch Query中组合must_not

时间:2016-07-28 15:31:30

标签: elasticsearch

我目前正在努力解决目前看起来如下的ElastSearch查询:

...
"query": {
    "bool": {
        "must_not": [
            {
                "term": {
                    "bool-facet.criteria1": {
                        "value": false
                    }
                }
            },
            {
                "term": {
                    "bool-facet.criteria2": {
                        "value": false
                    }
                }
            }
        ]
    }
}
...

因此,当条件1或条件2匹配时,文档将被忽略。查询必须如何,以便只忽略与criteria1 AND criteria2匹配的文档?

4 个答案:

答案 0 :(得分:9)

以下安排对我在5.5.1中的类似查询起作用:

"query": {
    "bool": {
        "must": [
            {
                "bool":{
                    "must_not":{
                        "term":{
                            "bool-facet.criteria1": false
                        }
                    }
                }
            },
            {
                "bool":{
                    "must_not":{
                        "term":{
                            "bool-facet.criteria2": true
                        }
                    }
                }
            }
        ]
    }
} 

对于其他版本,其他答案可能是正确的,但在5.5.1中对我不起作用。

答案 1 :(得分:6)

如果您想要简单的AND - 行为,那么只需在其中嵌套另一个bool查询:

"query": {
  "bool": {
    "must_not": [
      {
        "bool": {
          "filter": [
            {
              "term": {
                "bool-facet.criteria1": false
              }
            },
            {
              "term": {
                "bool-facet.criteria2": false
              }
            }
          ]
        }
      }
    ]
  }
}

请注意,使用filter(因为它是一个是/否的问题,不需要评分,但如果你想得分,那么你会使用must而不是filter)你获得所需的AND行为。这会将问题更改为“not(任何具有criteria1 == false和criteria2 == false的文档)”。

答案 2 :(得分:1)

由于无法更新elasticsearch版本,我必须找到另一种解决方案。这对我有用:

"query": {
    "bool": {
        "must_not" : [
            {
                "query": {
                    "bool": {
                        "must": [
                            {
                               "term": {
                                 "bool-facet.criteria1": false
                               }
                            },
                            {
                               "term": {
                                 "bool-facet.criteria2": false
                               }
                            }
                        ]
                    }
                }
            }
        ]
    }
}

答案 3 :(得分:-1)

此行为在6.5.4下对我有效:

GET model/_search
{
    "query": {
        "bool": {
            "must_not": [
                {
                    "exists": {
                        "field": "field1"
                    }
                },
                {
                    "exists": {
                        "field": "field2"
                    }
                },
                {
                    "exists": {
                        "field": "field3"
                    }
                }
            ]
        }
    }
}