# How to sort buckets in Elasticsearch on their first occurrence in a search?

**URL:** <https://discuss.elastic.co/t/how-to-sort-buckets-in-elasticsearch-on-their-first-occurrence-in-a-search/311288>\
**Category:** Elasticsearch\
**Created:** [August 3, 2022, 10:08am UTC](https://discuss.elastic.co/t/how-to-sort-buckets-in-elasticsearch-on-their-first-occurrence-in-a-search/311288 "2022-08-03T10:08:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter\_Dewachtere](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/peter_dewachtere/32/59981_2.png) [@Peter\_Dewachtere](https://discuss.elastic.co/u/Peter_Dewachtere)\
**Post date:** [August 3, 2022, 10:08am UTC](https://discuss.elastic.co/t/how-to-sort-buckets-in-elasticsearch-on-their-first-occurrence-in-a-search/311288/1 "2022-08-03T10:08:33Z")

</div>

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:

```auto
"aggs": {
  "images": {
    "aggs": {
      "bucket_sort": {
        "bucket_sort": {
          "from": 0,
          "size": 15
        }
      }
    },
    "terms": {
      "field": "jpegname.keyword"
    }
  }
}

```

with the following response (only the buckets part):

```auto
"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.

---

<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:** [August 31, 2022, 10:09am UTC](https://discuss.elastic.co/t/how-to-sort-buckets-in-elasticsearch-on-their-first-occurrence-in-a-search/311288/2 "2022-08-31T10:09:25Z")

</div>

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