Hi,
I would like to know how to sort an aggregation result by a field in the document.
My indice configuration
{
"id": {
"type": "long"
},
"film_id": {
"type": "long"
},
"film_group_id": {
"type": "long"
},
"created": {
"type": "date"
},
"metadata": {
"properties": {
"production": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 256
}
}
},
"duration": {
"type": "long"
}
}
}
}
What's important to know here, is that the metadata
content is the same for every documents with the same film_group_id
.
What I'm trying to do, is to get only one document by film_group_id
(because the metadata
is the same for every documents), sort the results by whatever field in metadata
(let's say the name), and do a pagination.
So far this is what I have
{
"size": 0,
"query": {
"range": {
"created": {
"gt": "2020-06-01"
}
}
},
"aggs": {
"docs": {
"terms": {
"field": "film_group_id",
"size": page*page_size
},
"aggs": {
"fields": {
"top_hits": {
"size": 1
}
},
"pagination": {
"bucket_sort": {
"size": page_size,
"from": (page_size-1)*page
}
}
}
}
}
}
This query works, I have my pagination, but I couldn't find a way to sort on a field in metadata
like metadata.name
. I tried adding order
in the terms
aggregation and sort
in the bucket_sort
aggregation, but none works.
Is there a way to achieve this or should I rework my indice mapping ?
Thanks.