I have an Elasticsearch query (version 7) that returns a paged result of products.
The query has a specific sort on multiple fields:
- Product A - image 1
- Product B - image 1
- Product C - image 2
- Product D - image 2
- Product E - image 2
- Product F - image 2
- Product G - image 2
- Product H - image 2
- Product I - image 4
- Product J - image 4
- Product K - image 4
- Product L - image 1
- Product M - image 2
- Product N - image 3
- Product O - image 4
Each image occurs in multiple products.
I would like to show an image slider (also paged) above my product grid with unique images in the order that they first appear in the product list like so:
IMAGE 1 - IMAGE 2 - IMAGE 4 - IMAGE 3 ...
But I get the images sorted descending by doc_count like this:
(largest doc_count) IMAGE 2 - IMAGE 4 - IMAGE 1 - IMAGE 3 ... (smallest doc_count)
This is the aggregation part of my paged query:
"aggs": {
"images": {
"aggs": {
"bucket_sort": {
"bucket_sort": {
"from": 0,
"size": 15
}
}
},
"terms": {
"field": "jpegname.keyword"
}
}
}
with the following response (only the buckets part):
"buckets": [
{
"key": "IMAGE2.jpg",
"doc_count": 5357
},
{
"key": "IMAGE4.jpg",
"doc_count": 1492
},
{
"key": "IMAGE1.jpg",
"doc_count": 720
},
{
"key": "IMAGE3.jpg",
"doc_count": 708
}]
How can I sort the images by their first appearance in the product list ?
Product A contains IMAGE 1, so IMAGE 1 comes first
Product C contains IMAGE 2, so IMAGE 2 comes second
Product I contains IMAGE 4, so IMAGE 4 comes third
Product N contains IMAGE 3, so IMAGE 3 comes fourth
Thanks in advance for any hints.