Search code examples
elasticsearchelasticsearch-aggregationbucketelasticsearch-dsl

Get buckets containing documents in ElasticSearch


I have a query like that:

https://pastebin.com/9YK6WxEJ

this gives me:

https://pastebin.com/ranpCnzG

Now, the buckets are fine but I want to get the documents' data grouped by bucket name, not just their count in doc_count. Is there any way to do that?


Solution

  • Maybe this works for you?

    "aggs": {
        "rating_ranges": {
          "range": {
            "field": "AggregateRating",
            "keyed": true,
            "ranges": [
              {
                "key": "bad",
                "to": 3
              },
              {
                "key": "average",
                "from": 3,
                "to": 4
              },
              {
                "key": "good",
                "from": 4
              }
            ]
          },
          "aggs": {
            "hits": {
              "top_hits": {
                "size": 100,
                "sort": [
                  {
                    "AggregateRating": {
                      "order": "desc"
                    }
                  }
                ]
              }
            }
          }
        }
      }