0

Below is my sample document

       {
          "totalPrice": 122.24173769217346,
          "createdAt": "2023-07-24T06:25:44.961",
          "domain": "mystore.wix.com"
        }

My requirement is to retrieve the records where the domain must match mystore.wix.com and should be grouped by the date createdAt with the totalPrice sum aggregated and also sorted in descending order by the same createdAt. I want the size of aggregation to be 10, that is only 10 buckets need to be returned and no more.

I tried the query,

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "domain": "mystore.wix.com"
          }
        }
      ]
    }
  },
  "aggs": {
    "groupByDate": {
      "date_histogram": {
        "field": "createdAt",
        "calendar_interval": "1d",
        "min_doc_count": 1,
        "order": {
          "_key": "desc"
        }
      },
        "aggs": {
          "totalPrice": {
          "sum": {
            "field": "totalPrice",
            "script": {"source": "Math.round(doc['totalPrice'].value * 1000.0)/1000.0"}
        }
    }
      }
    }
  }
}

But i could not see how i could restrict the aggregation size to 10. Hence i am getting a big size result. What am i missing here ?

Vivek
  • 341
  • 1
  • 5
  • 15

1 Answers1

0

I don't think that it is posibble to limit elastic to the number of buckets used.

One option is to use the pagination parameters and fetching only the first page.

By default, searches return the top 10 matching hits. To page through a larger set of results, you can use the search API's from and size parameters. The from parameter defines the number of hits to skip, defaulting to 0. The size parameter is the maximum number of hits to return. Together, these two parameters define a page of results.

https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#paginate-search-results

Tal Taub
  • 23
  • 7