ElasticSearch 如何查詢?nèi)笔В╩issing)字段數(shù)據(jù)

Filter Query Missing 已經(jīng)從 ES 5 版本移除
Refer to:
https://www.elastic.co/guide/en/elasticsearch/reference/6.1/query-dsl-exists-query.html#_literal_missing_literal_query

There isn’t a `missing` query. Instead use the `exists` query inside a `must_not` clause as follows:
GET /_search
{
    "query": {
        "bool": {
            "must_not": {
                "exists": {
                    "field": "user"
                }
            }
        }
    }
}


針對(duì) ES 5.2.2 之前版本(如 ES 2.3.4),查詢不包含某字段(無(wú) content 字段)的數(shù)據(jù), 支持使用 missing 或者 exists,DSL 如下:

### 不適用于 ES5.2.2 及以上版本
{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "must": [
              {
                "bool": {
                  "must": [
                    {
                      "match_phrase": {
                        "title": {
                          "query": "華為",
                          "slop": 0
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        },
        {
          "missing": {
            "field": "content"
          }
        },
        {
          "range": {
            "pubTime": {
              "from": 1514736000000,
              "to": 1517414400000,
              "include_lower": true,
              "include_upper": false
            }
          }
        }
      ]
    }
  }
}


針對(duì) ES 5.2.2 以上版本,查詢不包含某字段(無(wú) content 字段)的數(shù)據(jù),DSL 如下:

### 高低版本通用
{
    "query": {
        "bool": {
            "must": [
                {
                    "bool": {
                        "must": [
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "match_phrase": {
                                                "title": {
                                                    "query": "華為",
                                                    "slop": 0
                                                }
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                },
                {
                    "bool": {
                        "must_not": {
                            "exists": {
                                "field": "content"
                            }
                        }
                    }
                },
                {
                    "range": {
                        "pubTime": {
                            "from": 1514736000000,
                            "to": 1517414400000,
                            "include_lower": true,
                            "include_upper": false
                        }
                    }
                }
            ]
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容