簡體   English   中英

使用Elasticsearch獲取文檔中特定字段的數量

[英]Get count of particular field in a document using Elasticsearch

要求:我想查找特定類別ID的aID計數。 (即對於categoryID 2532,我希望計數為2,這意味着將其分配給兩個aID)。

我嘗試了聚合,但是有了它我只能得到文檔數而不是字段數。

對應

 "List": {
            "properties": {

              "aId": {
                "type": "long"
              },
              "CategoryList": {
                "properties": {                  
                  "categoryId": {
                    "type": "long"
                  },
                  "categoryName": {
                    "type": "string"
                  }
                }
              }              
            }
          }

樣本文件:

"List": [
            {
              "aId": 33074,           
              "CategoryList": [
                {
                  "categoryId": 2532,
                  "categoryName": "VODAFONE"                
                }
              ]
            },
        {
              "aId": 12074,           
              "CategoryList": [
                {
                  "categoryId": 2532,
                  "categoryName": "VODAFONE"                
                }
              ]
            },

        {
              "aId": 120755,           
              "CategoryList": [
                {
                  "categoryId": 1234,
                  "categoryName": "SMPLKE"                
                }
              ]
            }
          ]

使用基數聚合將無法幫助您獲得理想的結果。 基數聚合返回該字段的不同值的計數,您想在其中查找某個字段的出現次數。

您可以使用以下查詢,在這里您可以首先為CategoryList.categoryId過濾文檔,然后在此字段上運行簡單術語聚合

POST index_name1111/_search
{
    "query": {
        "bool": {
            "must": [{
                "term": {
                    "CategoryList.categoryId": {
                        "value": 2532
                    }
                }
            }]
        }
    },
    "aggs": {
        "count_is": {
            "terms": {
                "field": "CategoryList.categoryId",
                "size": 10
            }
        }
    }
}

上述查詢的響應-

{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "count_is": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": 2532,
          "doc_count": 2
        }
      ]
    }
  }
}

或者,您也可以放棄過濾器,僅運行聚合將返回所有categoryId及其外觀計數。

POST index_name1111/_search
{
size: 0,
  "aggs": {
    "count_is": {
      "terms": {
        "field": "CategoryList.categoryId",
        "size": 10
      }
    }
  }
}

以上查詢的回應

    {
      "took": 2,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 3,
        "max_score": 0,
        "hits": []
      },
      "aggregations": {
        "count_is": {
          "doc_count_error_upper_bound": 0,
          "sum_other_doc_count": 0,
          "buckets": [
            {
              "key": 2532,
              "doc_count": 2
            },
            {


        "key": 1234,
          "doc_count": 1
        }
      ]
    }
  }
}

使用基數聚合,您將通過以下查詢獲得以下響應

POST index_name1111/_search
{
    "size": 0,
    "query": {
        "bool": {
            "must": [{
                "term": {
                    "CategoryList.categoryId": {
                        "value": 2532
                    }
                }
            }]
        }
    },
    "aggs": {
        "id_count": {
            "cardinality": {
                "field": "CategoryList.categoryId"
            }
        }
    }
}

上面查詢的響應沒有得到您想要的結果,因為兩個文檔都將categoryId匹配為252,所以不重復計數為1。

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "id_count": {
      "value": 1
    }
  }
}

希望這對您有所幫助

暫無
暫無

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

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