弹性搜索 - 或查询非匹配

时间:2016-02-24 00:49:16

标签: elasticsearch

我在查询弹性搜索方面遇到了麻烦。我正在搜索state_id定义的一组特定数据,然后想要返回没有下面标识符定义的任何一个城市的所有状态。

以下查询返回18个结果,只有" city_id_1",0结果用" city_id_2"。尽管如此,我返回0结果(因为" city_id_2"在每个州记录上)。我想要做的仍然是返回18个结果,但查询两个城市。

我觉得我的查询应该正常工作,基本上做一个NOT(A或B)样式查询,相当于NOT A和NOT B,但基本上0结果似乎覆盖了18。

有没有办法可以更改我的查询以获得我想要的结果,或者这是弹性搜索不能做的事情?

{ 
  "query": {
    "bool": { 
      "must": [
        { "terms": { "state_id": ["4ca16f80-da79-11e5-9874-64006a4f57cb"]}}
      ],
      "must_not": [
        { 
          "nested": {
            "path": "cities",
            "query": {
              "bool": { 
                "should": [
                  {"term": { "cities.identifier": "city_id_1"}},
                  {"term": { "cities.identifier": "city_id_2"}}
                ]
              }
            }
          }
        }
      ]
    }
  },
 "size": 10

}

3 个答案:

答案 0 :(得分:2)

尝试使用此尺寸。 Elasticsearch很傻。过滤器需要位于每个嵌套查询中。

{
  "query": {
    "bool": { 
      "should": [
        {
          "query": {
            "bool": {
              "must_not": [
                { 
                  "nested": {
                    "path": "cities",
                    "query": {
                      "term": { "cities.identifier": "city_id_1"}
                    }
                  }
                }
              ],
              "filter":[
                {
                  "term":{
                    "state_id":"4ca16f80-da79-11e5-9874-64006a4f57cb"
                  }
                }
              ]
            }
          }
        },
        {
          "query": {
            "bool": {
              "must_not": [
                { 
                  "nested": {
                    "path": "cities",
                    "query": {
                      "term": { "cities.identifier": "city_id_2"}
                    }
                  }
                }
              ],
              "filter":[
                {
                  "term":{
                    "state_id":"4ca16f80-da79-11e5-9874-64006a4f57cb"
                  }
                }
              ]
            }
          }
        }
      ]  
    }
  },
  "size": 10
}

答案 1 :(得分:0)

如果您需要NOT A AND NOT B行为,则需要进行一些更改

{ 
 "query": {
"bool": { 
  "must": [
    { "terms": { "state_id": ["4ca16f80-da79-11e5-9874-64006a4f57cb"]}}
  ],
  "must_not": [
    { 
      "nested": {
        "path": "cities",
        "query": {
          "bool": { 
            "must": [          ====> Use must instead of should
              {"term": { "cities.identifier": "city_id_1"}},
              {"term": { "cities.identifier": "city_id_2"}}
            ]
          }
        }
      }
     }
   ]
  }
 },
 "size": 10

}

这将排除那些同时包含city_id_1和city_id_2的记录。

答案 2 :(得分:0)

根据我的理解,您正在寻找NOT A or NOT B种类的条款。请检查下面的查询,看看它是否符合您的要求

{ 
  "query": {
    "bool": { 
      "must": [
        { "terms": { "state_id": ["4ca16f80-da79-11e5-9874-64006a4f57cb"]}}
      ],
      "should": [
        { 
          "nested": {
            "path": "cities",
            "query": {
              "bool": { 
                "must_not": [
                  {"term": { "cities.identifier": "city_id_1"}}
                  ]
              }
            }
          }
        },
        { 
          "nested": {
            "path": "cities",
            "query": {
              "bool": { 
                "must_not": [
                  {"term": { "cities.identifier": "city_id_2"}}
                  ]
              }
            }
          }
        }
      ],
      "minimum_number_should_match": 1
    }
  },
 "size": 10

}