Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get the count of value for particular field using aggregation in Elasticsearch

In my index I have multiple documents having field event.code with different values like below –

"event": {"code": "98"},
"event": {"code": "99"},
"event": {"code": "98"},
"event": {"code": "88"}.

I want Elasticsearch aggregation query which gives me count of particular event code. For eg: count of event.code 98 as 2.(Basically count for particular value is needed not for the field)

There are many count aggregation questions available on stack overflow itself but none of them answering my doubt. Kindly help me on the same.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance.

>Solution :

Here is an example

  "size": 0,
  "aggs": {
    "event_code_count": {
      "filter": {
        "term": {
          "event.code": "98"
        }
      },
      "aggs": {
        "event_code_count": {
          "value_count": {
            "field": "event.code"
          }
        }
      }
    }
  }
}

In this example, we are using the terms aggregation in combination with a filter to get the count of a particular event code, which is 98 in this case. The value_count aggregation is nested within to count the occurrences of that specific event code.

To explain the query structure:

  • The size parameter is set to 0, which means we only want the aggregation results without returning any documents.
  • The outer event_code_count aggregation is a filter aggregation targeting the field event.code with the term filter for value 98.
  • Within the event_code_count aggregation, the value_count aggregation is used to count the occurrences of the event.code field.
    The result will provide you with the count of the specific event code
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading