Elasticsearch 查询空检查两个具有 OR 条件的字段

问题描述 投票:0回答:1

我想用在 jboss 服务器上运行的 java 7 应用程序更改弹性搜索中的数据。在弹性搜索索引中有两个字段,分别是 city_id 和 country_id。通过新的实现,我需要搜索 city_name is null 或 country_name null 记录并用相关的 city_name 和 country_name 更新它们。

POST index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "timestamp": {
              "gte": "2023-02-27 06:40:00"
            }
          }
        }
      ],
      "must_not": [
         {
          "exists": {
            "field": "city_name"
          }
        },
        {
          "exists": {
            "field": "country_name"
          }
        }
      ]
    }
  },
  "size": 3,
  "sort": [{
            "timestamp": {
                "order": "asc"
            }
        }
    ]
}

我正在使用这个搜索查询。但是通过这个搜索 city_name 和 country_name 都是 null 记录。我想为此获得OR(city_name OR country_name null)条件。

有人可以帮我解决这个问题吗?

谢谢。

elasticsearch jboss java-7 elasticsearch-dsl
1个回答
0
投票

由于 should 大致等同于布尔值 OR,根据 this,您可以尝试使用嵌套的

should
->
bool
->
must_not
子句。例子:

{
    "query": {
        "bool": {
            "should": [{
                    "bool": {
                        "must_not": {
                            "exists": {
                                "field": "city_name"
                            }
                        }
                    }
                },
                {
                    "bool": {
                        "must_not": {
                            "exists": {
                                "field": "country_name"
                            }
                        }
                    }
                }
            ]
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.