From and size as in query are not available in aggregations
You can use below options to paginate through aggegations:-
- Composite Aggregation: can combine multiple datasources in a single buckets and allow pagination and sorting on it. It can only paginate linearly using after_key i.e you cannot jump from page 1 to page 3. You can fetch "n" records , then pass returned after key and fetch next "n" records.
GET index22/_search
{
"size": 0,
"aggs": {
"pagination": {
"composite": {
"size": 1,
"sources": [
{
"source_list": {
"terms": {
"field": "sources.keyword"
}
}
}
]
}
}
}
}
Result:
"aggregations" : {
"pagination" : {
"after_key" : {
"source_list" : "a" --> used to fetch next records linearly
},
"buckets" : [
{
"key" : {
"source_list" : "a"
},
"doc_count" : 1
}
]
}
}
To fetch next record
{
"size": 0,
"aggs": {
"pagination": {
"composite": {
"size": 1,
"after": {"source_list" : "a"},
"sources": [
{
"source_list": {
"terms": {
"field": "sources.keyword"
}
}
}
]
}
}
}
}
- Include partition: group's the field’s values into a number of partitions at query-time and processing only one partition in each request. Term fields are evenly distributed in different partitions. So you must know number of terms beforehand. You can use cardinality aggregation to get count
GET index/_search
{
"size": 0,
"aggs": {
"source_list": {
"terms": {
"field": "sources.keyword",
"include": {
"partition": 1,
"num_partitions": 3
}
}
}
}
}
- Bucket Sort aggregation : sorts the buckets of its parents multi bucket aggreation. Each bucket may be sorted based on its _key, _count or its sub-aggregations. It only applies to buckets returned from parent aggregation. You will need to set term size to 10,000(max value) and truncate buckets in bucket_sort. You can paginate using from and size just like in query. If you have terms more that 10,000 you won't be able to use it since it only selects from buckets returned by term.
GET index/_search
{
"size": 0,
"aggs": {
"source_list": {
"terms": {
"field": "sources.keyword",
"size": 10000 --> use large value to get all terms
},
"aggs": {
"my_bucket": {
"bucket_sort": {
"sort": [
{
"_key": {
"order": "asc"
}
}
],
"from": 1,
"size": 1
}
}
}
}
}
}
In terms of performance composite aggregation is a better choice