首页 文章

弹性搜索 - 或查询非匹配

提问于
浏览
2

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

以下查询仅使用“city_id_1”返回18个结果,使用“city_id_2”返回0结果 . 尽管如此,我返回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

    尝试这个尺寸 . 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
    }
    
  • 2

    如果你想要 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的记录 .

  • 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
    
    }
    

相关问题