How to aggregate and page results

Hello people,

I know that it is not possible to page aggregations and the recommended way is to use composite.
My problem is that with composite the Key is the aggregation key and I need to paginate by a "number" like page 1,2 and so on.

Here are the model and the queries we have tried, any help is appreciated.

Model

{
	"_index": "car-catalog",
	"_type": "catalogvehicle",
	"_id": "9afc0cb6d1899df2fbdb0d004bd64be5529d0874",
	"_score": 1.0,
	"_source": {
		"vehicleSpecification": {
			"body": {
				"size": null,
				"type": "Cabriolet 2 portes",
				"doors": "2",
				"seats": 2
			}
		},
		"id": "9afc0cb6d1899df2fbdb0d004bd64be5529d0874",
		"make": "ABARTH",
		"model": "124 Spider",
		"shortModel": "124",
		"trimline": "1.4 Turbo 170ch Bvm Gt",
		"isConfigurable": true
	}
}

I need to group by Make, Model and BodyType get one document per aggregation and paginate the results.
eg: Page 1 should contain 10 groups each with one document representing the vehicle, page 2 the next 10 vehicles and so on.
I can't use the AfterKey because I would need to keep the state of these keys and it is not a viable solution(at least for now)

Attempt 1 - Aggregates - Closest I got to the desired results

GET car-catalog-fr-fr-1.0.0/_search/
{
   "aggs":{
      "make":{
         "aggs":{
            "model":{
               "aggs":{
                  "body":{
                     "terms":{
                        "field":"vehicleSpecification.body.type.idx",
                        "order":[
                           {
                              "_key":"asc"
                           }
                        ],
                        "size":10000
                     },
                     "aggs":{
                        "top_sales_hits":{
                           "top_hits":{
                              "size":1
                           }
                        }
                     }
                  }
               },
               "terms":{
                  "field":"shortModel.idx",
                  "order":[
                     {
                        "_key":"asc"
                     }
                  ],
                  "size":10000
               }
            }
         },
         "terms":{
            "field":"make.idx",
            "order":[
               {
                  "_key":"asc"
               }
            ],
            "size":10000
         }
      }
   },
   "size":0
}

Attempt 2 - Composite - missing the document per bucket and the pagination is not as needed.

GET car-catalog-fr-fr-1.0.0/_search
    {
       "aggs":{
          "my_buckets" : {
            "composite": {
              "sources": [
                { "make": { "terms": { "field": "make.idx", "order": "asc" } } },
                { "model": { "terms": { "field": "model.idx", "order": "asc" } } },
                { "body": { "terms": { "field": "vehicleSpecification.body.type.idx" } } }
              ]
            }
          }
       }
    }

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