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

Aggregation on terms and intervals in elasticsearch

My documents are like this below:

{
    "uri" : "post:1130a8ef197882bc3ebd",
    "topic_list" : [
        "bye",
        "hello"
    ],
    "datetime" : "2010-06-06T22:08:49"
}

I want to make a query to aggregate on both datetime and topic_list. My desired output is to tell me that on each time interval, how many docs has the hello topic in topic_list.

What I’ve tried was this:

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

{
  "size": 0, 
  "aggs": {
    "test": {
      "terms": {
        "field": "topic_list"
        
      }
    }
  }
}

But the output just tell me how many docs containing every topic at all times and not in the intervals.
How can I create such aggregation?

>Solution :

You need to add two more things:

  1. a query to only restrict the results to documents containing the topic "hello". If you’re only interested in the document count per time interval, you don’t need the terms aggregation on the topic_list field
  2. a date_histogram aggregation to create time intervals

Here is the query:

{
  "size": 0,
  "query": {
    "term": {
      "topic_list": "hello"
    }
  },
  "aggs": {
    "intervals": {
      "date_histogram": {
        "field": "date",
        "calendar_interval": "1d"
      }
    }
  }
}
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