# Like to apply sorting on given search query param

**URL:** <https://discuss.elastic.co/t/like-to-apply-sorting-on-given-search-query-param/277669>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [July 2, 2021, 4:22pm UTC](https://discuss.elastic.co/t/like-to-apply-sorting-on-given-search-query-param/277669 "2021-07-02T16:22:47Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![hariskhalique](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@hariskhalique](https://discuss.elastic.co/u/hariskhalique)\
**Post date:** [July 2, 2021, 4:22pm UTC](https://discuss.elastic.co/t/like-to-apply-sorting-on-given-search-query-param/277669/1 "2021-07-02T16:22:47Z")

</div>

I have product id list in terms query. I like to get result in same order as provide ids in terms query. Below is my query.

```
{
    "size": 24,
    "from": 0,
    "sort": [],  
    "query": {
        "bool": {
            "filter": {
                "script": {
                    "script": {
                        "source": "doc['image_src.keyword'].size()!=0",
                        "lang": "painless"
                    }
                }
            },
            "must": [
                {
                    "term": {
                        "product_to_show": {
                            "value": "1"
                        }
                    }
                },
                {
                    "terms": {
                        "product_id": [
                            66421,
                            66418,
                            66410,
                            66404
                        ]
                    }
                },
                {
                    "terms": {
                        "is_plus": [
                            0,
                            2
                        ]
                    }
                },
                {
                    "script": {
                        "script": "doc['gender.keyword'].value == 'f'"
                    }
                }
            ]
        }
    }
}

```

should return result in same order 66421,66418,66410,66404 but query return in following order 66418, 66421, 66404, 66410.

thanks is Advance

---

<div class="post-metadata">

**Author:** ![hariskhalique](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@hariskhalique](https://discuss.elastic.co/u/hariskhalique)\
**Post date:** [July 8, 2021, 2:23pm UTC](https://discuss.elastic.co/t/like-to-apply-sorting-on-given-search-query-param/277669/2 "2021-07-08T14:23:01Z")

</div>

Any suggestion or thought on my above question.

thanks

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 9, 2021, 8:19am UTC](https://discuss.elastic.co/t/like-to-apply-sorting-on-given-search-query-param/277669/3 "2021-07-09T08:19:06Z")

</div>

The only way I can imagine would be by passing the same list of values to a [Sort Script](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html#script-based-sorting) and use the position in the table to define the boost value for each doc.

Not sure how you can do that and write the painless script for that.

BTW, I'd put all the `must` clauses within the `filter` part as I believe they are just filtering the resultset and not contributing to the score.

Also, I'd avoid doing things like:

```
            {
                "script": {
                    "script": "doc['gender.keyword'].value == 'f'"
                }
            }

```

Instead, I'd just use a term query on field `gender.keyword` with value `f`. That will be much more efficient.

For this one:

```
            "script": {
                "script": {
                    "source": "doc['image_src.keyword'].size()!=0",
                    "lang": "painless"
                }
            }

```

I'd rather compute at index time the `image_src.keyword` size and add a `image_src_empty` boolean field so filtering by the size would be much faster.

---

<div class="post-metadata">

**Author:** ![hariskhalique](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@hariskhalique](https://discuss.elastic.co/u/hariskhalique)\
**Post date:** [July 12, 2021, 4:57pm UTC](https://discuss.elastic.co/t/like-to-apply-sorting-on-given-search-query-param/277669/4 "2021-07-12T16:57:00Z")

</div>

thanks for the tips. It really helps but I am still looking for a solution for my query.

Thanks

---

<div class="post-metadata">

**Author:** ![hariskhalique](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@hariskhalique](https://discuss.elastic.co/u/hariskhalique)\
**Post date:** [July 19, 2021, 6:43pm UTC](https://discuss.elastic.co/t/like-to-apply-sorting-on-given-search-query-param/277669/5 "2021-07-19T18:43:26Z")

</div>

any one I need help on this topic.

---

<div class="post-metadata">

**Author:** ![hariskhalique](https://avatars.discourse-cdn.com/v4/letter/h/a3d4f5/32.png) [@hariskhalique](https://discuss.elastic.co/u/hariskhalique)\
**Post date:** [August 13, 2021, 6:03pm UTC](https://discuss.elastic.co/t/like-to-apply-sorting-on-given-search-query-param/277669/6 "2021-08-13T18:03:40Z")

</div>

I am still looking for a solution

---

<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:** [August 13, 2021, 7:24pm UTC](https://discuss.elastic.co/t/like-to-apply-sorting-on-given-search-query-param/277669/7 "2021-08-13T19:24:52Z")

</div>

If David’s suggested solution with the sort script did not work it may not be possible unless you sortvthe results after they are retrieved in your application.

---

<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:** [September 10, 2021, 7:25pm UTC](https://discuss.elastic.co/t/like-to-apply-sorting-on-given-search-query-param/277669/8 "2021-09-10T19:25:47Z")

</div>

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