# How to aggregate and page results

**URL:** <https://discuss.elastic.co/t/how-to-aggregate-and-page-results/194369>\
**Category:** Elasticsearch\
**Created:** [August 8, 2019, 8:04am UTC](https://discuss.elastic.co/t/how-to-aggregate-and-page-results/194369 "2019-08-08T08:04:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Higor\_Cesar\_Ramos](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/higor_cesar_ramos/32/51911_2.png) [@Higor\_Cesar\_Ramos](https://discuss.elastic.co/u/Higor_Cesar_Ramos)\
**Post date:** [August 8, 2019, 8:04am UTC](https://discuss.elastic.co/t/how-to-aggregate-and-page-results/194369/1 "2019-08-08T08:04:44Z")

</div>

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" } } }
              ]
            }
          }
       }
    }
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 5, 2019, 8:04am UTC](https://discuss.elastic.co/t/how-to-aggregate-and-page-results/194369/2 "2019-09-05T08:04:49Z")

</div>

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