Home > database >  Get buckets containing documents in ElasticSearch
Get buckets containing documents in ElasticSearch

Time:04-20

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?

CodePudding user response:

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"
                }
              }
            ]
          }
        }
      }
    }
  }
  • Related