首页 > 解决方案 > Elastic Search / Elasica php - 对包含 NULL 的字段的 OR 查询不起作用

问题描述

如何仅查询 location_idnull locations.country_id 在一组给定 ID 中的文档?这是当前的查询,但它根本没有返回任何内容......如果我从查询中删除 location_id 它可以工作并至少返回与国家 ID 匹配的文档。

{
   "bool":{
      "must":[
         {
            "bool":{
               "must_not":[
                  {
                     "exists":{
                        "field":"location_id"
                     }
                  }
               ],
               "must":[
                  {
                     "terms":{
                        "locations.country_id":[
                           18
                        ]
                     }
                  }
               ]
            }
         }
      ]
   }
}

标签: phpelasticsearchelastica

解决方案


在弹性搜索中,必须是 AND 并且应该表现得像 OR 您的查询转换为 locationId 是 null AND locations.country_id 在 [id 集] 中。必须需要替换为应该。

询问:

{
  "query": {
    "bool": {
      "should": [
        {
          "bool": {
            "must_not": [
              {
                "exists": {
                  "field": "location_id"
                }
              }
            ]
          }
        },
        {
          "terms": {
            "locations.country_id": [
              18
            ]
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

推荐阅读