Skip to content Skip to sidebar Skip to footer

How To Get The Individual Count Of Field From Elasticsearch

My content inside a dictionary is below test= [ { 'masterid': '1', 'name': 'Group1', 'BusinessArea': [ { 'id': '14', 'name': 'Accounting', 'parentname': 'Finance'}, { 'id': '3', 'n

Solution 1:

If you want to get the individual count of the field you can use the terms aggregation that is a multi-bucket value source-based aggregation where buckets are dynamically built - one per unique value.

Search Query:

{"size":0,"aggs":{"countNames":{"terms":{"field":"BusinessArea.name.keyword"}}}}

Search Result:

"aggregations":{"countNames":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":"Accounting","doc_count":2},{"key":"Research","doc_count":2},{"key":"Engineering","doc_count":1}]}

Update 1:

If you want to have an individual count of the field for Designation as well as BusinessArea

Search Query:

{"size":0,"aggs":{"countNames":{"terms":{"field":"BusinessArea.name.keyword"}},"designationNames":{"terms":{"field":"Designation.name.keyword"}}}}

Search Result:

"aggregations":{"designationNames":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":"L1","doc_count":3},{"key":"L2","doc_count":3}]},"countNames":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":"Accounting","doc_count":2},{"key":"Research","doc_count":2},{"key":"Engineering","doc_count":1}]}

Solution 2:

You can simply use the count API of elasticsearch to get the count of All the documents in the elasticsearch index or based on a condition as shown in the same doc.

For your case, it should be like

GET /<your-index-name>/_count?q=name:BusinessArea

Or, if masterid is the Unique-id in your document, you can simply use

 GET /<your-index-name>/_count

Post a Comment for "How To Get The Individual Count Of Field From Elasticsearch"