简体   繁体   中英

Get count of particular field in a document using Elasticsearch

Requirement: I want to find the count of aID for a particular category ID. (ie for categoryID 2532 i want the count as 2 that means it is assigned to two aID's).

I tried with aggregations but with that i can able to get only the doc count rather than field count.

Mappings

 "List": {
            "properties": {

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

Sample Document:

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

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

using cardinality aggregation will not help you getting the desired results. Cardinality aggregation returns the count of distinct values for the field, where are you want to find the count of appearance for number of times for a field.

You can use the following query, Here you can first filter the document for CategoryList.categoryId and then run a simple terms aggregation on this field

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

Response of above query -

{
  "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
        }
      ]
    }
  }
}

Or you can also chuck away the filter and running the aggregation only will return you all categoryId with their count of appearance.

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

Response of above query

    {
      "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
        }
      ]
    }
  }
}

Using cardinality aggregation you will get the following response with following query

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

Response of above query which doesn't give you desired result, since two documents matched both with categoryId as 252 so count of distinct is 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
    }
  }
}

Hope this helps Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM