# Sort products by ids stored in product\_cat index

**URL:** <https://discuss.elastic.co/t/sort-products-by-ids-stored-in-product-cat-index/318772>\
**Category:** Elasticsearch\
**Created:** [November 12, 2022, 12:30pm UTC](https://discuss.elastic.co/t/sort-products-by-ids-stored-in-product-cat-index/318772 "2022-11-12T12:30:33Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kiko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kiko/32/89385_2.png) [@kiko](https://discuss.elastic.co/u/kiko)\
**Post date:** [November 12, 2022, 12:30pm UTC](https://discuss.elastic.co/t/sort-products-by-ids-stored-in-product-cat-index/318772/1 "2022-11-12T12:30:33Z")

</div>

I have a products index which I now need to sort using ordered list of IDs stored in each product category document (in product\_categories index), like so:

```auto
// product index
{
id:1,
name: "coco"
},
{
id:2,
name: "hazel"
}

```

```auto
// category index
{
product_cat: "nuts",
products_sort_order: "1,3,2,5,4"
}

```

Each product can belong to multiple product categories. Is there a way to provide list of ids to sort the product index by, and how viable is this for product categories with large number of products?

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [November 13, 2022, 11:39pm UTC](https://discuss.elastic.co/t/sort-products-by-ids-stored-in-product-cat-index/318772/2 "2022-11-13T23:39:43Z")

</div>

How large are you talking about here?

---

<div class="post-metadata">

**Author:** ![kiko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kiko/32/89385_2.png) [@kiko](https://discuss.elastic.co/u/kiko)\
**Post date:** [November 14, 2022, 7:08am UTC](https://discuss.elastic.co/t/sort-products-by-ids-stored-in-product-cat-index/318772/3 "2022-11-14T07:08:25Z")

</div>

Currently not very large, hundreds of products, but might have to scale to the nth level.

---

<div class="post-metadata">

**Author:** ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)\
**Post date:** [November 14, 2022, 7:20am UTC](https://discuss.elastic.co/t/sort-products-by-ids-stored-in-product-cat-index/318772/4 "2022-11-14T07:20:35Z")

</div>

What's the nth level? Cause ideally you don't want to be handling tens of thousands.

Why do you need to have them ordered anyway?

---

<div class="post-metadata">

**Author:** ![kiko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kiko/32/89385_2.png) [@kiko](https://discuss.elastic.co/u/kiko)\
**Post date:** [November 14, 2022, 5:55pm UTC](https://discuss.elastic.co/t/sort-products-by-ids-stored-in-product-cat-index/318772/5 "2022-11-14T17:55:35Z")

</div>

I'm using Elastic as a data source not only for search but also for product list/category pages. And it worked out fine for this small/medium project, but I guess I've hit a limit.

---

<div class="post-metadata">

**Author:** ![kiko](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kiko/32/89385_2.png) [@kiko](https://discuss.elastic.co/u/kiko)\
**Post date:** [November 19, 2022, 1:49pm UTC](https://discuss.elastic.co/t/sort-products-by-ids-stored-in-product-cat-index/318772/6 "2022-11-19T13:49:29Z")

</div>

If ayone else bumps into this, there is another solution using Painless scripting language for Elastic Search:

```auto
...
sort:
{
        "_script": {
          "type": "number",
          "script": {
            "lang": "painless",
            "source": "params.sortOrder.indexOf(doc['id'].value.intValue())", // id is the name of the document field to use in the sort operation, in my case it was another id field
            "params": {
              "sortOrder": [1,3,5,2,4] //this the array of IDs in the sort order
            }
          },
          "order": "asc"
        }
    }
...

```

---

<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:** [December 17, 2022, 1:49pm UTC](https://discuss.elastic.co/t/sort-products-by-ids-stored-in-product-cat-index/318772/7 "2022-12-17T13:49:50Z")

</div>

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