# Add another one sorting to lift 3 docs to positions 3,4,5

**URL:** https://discuss.elastic.co/t/add-another-one-sorting-to-lift-3-docs-to-positions-3-4-5/349918
**Category:** Elasticsearch
**Tags:** painless, eql-elastic-query-language
**Created:** [December 25, 2023, 1:42pm UTC](https://discuss.elastic.co/t/add-another-one-sorting-to-lift-3-docs-to-positions-3-4-5/349918 "2023-12-25T13:42:42Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![sahkdevel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sahkdevel/32/124571_2.png) [@sahkdevel](https://discuss.elastic.co/u/sahkdevel)
#### Post date: [December 25, 2023, 1:42pm UTC](https://discuss.elastic.co/t/add-another-one-sorting-to-lift-3-docs-to-positions-3-4-5/349918/1 "2023-12-25T13:42:42Z")

</div>

I have a query with several sortings. Here is the sorting part:

```auto
    "sort": [
        "isHistorical",
        "_score",
        {
            "_script": {
                "type": "number",
                "script": {
                    "lang": "painless",
                    "source": "double score = 0; for (int i = 0; i < params.scalar_values.length; ++i) { if (doc[params.scalar_values[i].field].value == params.scalar_values[i].value) { score += params.scalar_values[i].score; } } for (int i = 0; i < params.array_values.length; ++i) { if (doc[params.array_values[i].field].contains(params.array_values[i].value)) { score += params.array_values[i].score; } } return score;",
                    "params": {
                        "scalar_values": [],
                        "array_values": []
                    }
                },
                "order": "desc"
            }
        },
        {
            "inStock": {
                "order": "desc"
            },
            "daysUntilReadinessForPickup": {
                "order": "asc"
            },
            "numberTradePointsForNearestPickup": {
                "order": "desc"
            },
            "availableInTradePointsCount": {
                "order": "desc"
            }
        }
    ]

```

After all sortings applied (and before limit) I want to add another sorting. It is related to prioritized documents.  
Every doc has field "assortmentUnitId" of integer type. I have a list of assortmentUnitIds, each of them has sort order value. where sort order of 0 is the top priority. 1 is less, and so far.

```auto
assortmentUnitId = 123, sort order = 0
assortmentUnitId = 456, sort order = 1
assortmentUnitId = 789, sort order = 2
assortmentUnitId = 999, sort order = 5

```

I want to pass this map (assortmentUnitId =\> sortOrder) to the query so that  
after applying all the other sortings these prioritized docs go up in the result list to the position 3, 4, 5 (not right at the beginning! 😮 ). I'll be passing only 3 prioritized values to the query

Is it possible? Can you guide me how to achieve this? Should I give your the other parts of the request? Thanks

---

<div class="post-metadata">

### Author: ![sahkdevel](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sahkdevel/32/124571_2.png) [@sahkdevel](https://discuss.elastic.co/u/sahkdevel)
#### Post date: [December 26, 2023, 7:42am UTC](https://discuss.elastic.co/t/add-another-one-sorting-to-lift-3-docs-to-positions-3-4-5/349918/2 "2023-12-26T07:42:21Z")

</div>

> [@sahkdevel](#):
>
> I'll be passing only 3 prioritized values to the query

Sorry, my mistake. The big difficulty here is that I'll be probably passing more than 3 ids, but I need not more than 3 docs to float

---

<div class="post-metadata">

### Author: ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)
#### Post date: [December 26, 2023, 8:49am UTC](https://discuss.elastic.co/t/add-another-one-sorting-to-lift-3-docs-to-positions-3-4-5/349918/3 "2023-12-26T08:49:48Z")

</div>

Have you considered using the [multi search API](https://www.elastic.co/guide/en/elasticsearch/reference/8.11/search-multi-search.html) to send 2 separate queries, one for the query and one for the prioritised documents, and then merge the result sets client side? This is as far as I can recall the standard solution for the type of mixed sorting you are describing.

---

<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: [January 23, 2024, 8:50am UTC](https://discuss.elastic.co/t/add-another-one-sorting-to-lift-3-docs-to-positions-3-4-5/349918/4 "2024-01-23T08:50:03Z")

</div>

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