首页 > 解决方案 > Elastics 搜索查询过滤掉列表中值为 A 但不是 B 的结果

问题描述

我正在尝试形成一个查询,要求仅过滤国家为印度而不是美国的数据。

样本数据。

{
  "data": {
    "attributes": {
      "name": "test",
       "country": ["india","usa","japan"]
    }
  }
}

因为,在上面的例子中,我们有印度和美国,它不应该过滤结果。

而如果数据采用下面提到的格式,它应该过滤。

例1:

{
  "data": {
    "attributes": {
      "name": "test",
       "country": ["india","japan"]
    }
  }
}

例 2:

{
  "data": {
    "attributes": {
      "name": "test",
       "country": ["india"]
    }
  }
}

标签: elasticsearch

解决方案


这个问题,看起来有点混乱。我假设,你不希望印度和美国走到一起。基于上述假设,共享以下查询。

"query": {
        "bool": {
            "must": [{
                    "term": {
                        "tags": {
                            "value": "india"
                        }
                    }
                }
            ],
            "must_not": [{
                    "term": {
                        "tags": {
                            "value": "usa"
                        }
                    }
                }
            ]
        }
    }

推荐阅读