Aggregate + sort + paginate on nested documents

Hi,
I'm managing a product index, with product sales and other KPIs under a nested field.
Trying to sort based on nested aggregation, and paginate - with no success.
Below is a simplified version of my mapping, for the sake of the example -

{
                "product_type":
                {
                    "type": "keyword"
                },
                "family":
                {
                    "type": "keyword"
                },
                "rootdomain":
                {
                    "type": "keyword"
                },
                "kpis":
                {
                    "type": "nested",
                    "properties":
                    {
                        "sales_1d":
                        {
                            "type": "float"
                        },
                        "timestamp":
                        {
                            "type": "date",
                            "format": "strict_date_optional_time_nanos"
                        },
                        "views_1d":
                        {
                            "type": "float"
                        }
                    }
                }
            }

My aggregation is similar to the one below-

{
  "aggs": {
    "group_by_family": {
      "aggs": {
        "nested_aggregation": {
          "aggs": {
            "range_filtered": {
              "aggs": {
                "sales_1d": {
                  "sum": {
                    "field": "kpis.sales_1d"
                  }
                },
                "views_1d": {
                  "sum": {
                    "field": "kpis.views_1d"
                  }
                },
                "reverse_nesting": {
                  "aggs": {
                    "docs": {
                      "top_hits": {
                        "size": 1,
                        "sort": [
                          {
                            "_id": {
                              "order": "asc"
                            }
                          }
                        ],
                        "_source": {
                          "includes": [
                            "_id",
                            "family",
                            "rootdomain",
                            "product_type"
                          ]
                        }
                      }
                    }
                  },
                  "reverse_nested": {}
                }
              },
              "filter": {
                "range": {
                  "kpis.timestamp": {
                    "format": "basic_date_time_no_millis",
                    "gte": "20220721T000000Z",
                    "lte": "20220918T235959Z"
                  }
                }
              }
            }
          },
          "nested": {
            "path": "kpis"
          }
        }
      },
      "terms": {
        "field": "family",
        "size": 10
      }
    }
  },
  "query": {
  //some query to filter by product-type and rootdomain  
},
  "size": 0
}

I'm aware that I can add an order clause to term aggregation to order the aggregated results.
My target though is to paginate the aggregated results - meaning I want to retrieve and order
1-10 best-selling products, and later retrieve 11-20 best-selling products and so on.
I've tried using bucket sort under range_filtered but I'm getting an error -

class org.elasticsearch.search.aggregations.bucket.filter.InternalFilter cannot be cast to class org.elasticsearch.search.aggregations.InternalMultiBucketAggregation

I'm not sure how to proceed from here, is this possible? if not, is there any workaround?

Thanks.

Any help?

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.