簡體   English   中英

計算 Elasticsearch 中有多少文檔具有某個屬性或缺少該屬性

[英]Count how many documents have an attribute or are missing that attribute in Elasticsearch

我如何編寫單個 Elasticsearch 查詢來計算有多少文檔具有某個字段的值或缺少該字段?

此查詢成功計算了缺少該字段的文檔:

POST localhost:9200//<index_name_here>/_search
{
    "size": 0,
    "aggs" : {
        "Missing_Field" : {
            "missing": { "field": "group_doc_groupset_id" }
        }
    }
}

此查詢執行相反的操作,計算不缺少該字段的文檔:

POST localhost:9200//<index_name_here>/_search
{
    "size": 0,
    "aggs" : {
        "Not_Missing_Field" : {
            "exists": { "field": "group_doc_groupset_id" }
        }
    }
}

我怎樣才能寫出一個將兩者結合起來的呢? 例如,這會產生語法錯誤:

POST localhost:9200//<index_name_here>/_search
{
    "size": 0,
    "aggs" : {
        "Missing_Field_Or_Not" : {
            "missing": { "field": "group_doc_groupset_id" },
            "exists": { "field": "group_doc_groupset_id" }
        }
    }
}
GET indexname/_search?size=0
{
  "aggs": {
    "a1": {
      "missing": {
        "field": "status"
      }
    },
    "a2": {
      "filter": {
        "exists": {
          "field": "status"
        }
      }
    }
  }
}

根據文檔中新的 Elastic 搜索建議:

GET {your_index_name}/_search     #or _count, to see just the value
{
  "query": {
    "bool": {
      "must_not": {    # here can be also "must"
        "exists": {
          "field": "{field_to_be_searched}"
        }
      }
    }
  }
}

編輯:_count 允許有多少文檔被索引的精確值。 如果超過 10k,則總數顯示為:

  "hits" : {
    "total" : {
      "value" : 10000,     # 10k
      "relation" : "gte"   # Greater than
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM