# Sort based on a Python List (array) and this list is not stored in the Elastic search

**URL:** <https://discuss.elastic.co/t/sort-based-on-a-python-list-array-and-this-list-is-not-stored-in-the-elastic-search/306528>\
**Category:** Elasticsearch\
**Created:** [June 7, 2022, 6:30am UTC](https://discuss.elastic.co/t/sort-based-on-a-python-list-array-and-this-list-is-not-stored-in-the-elastic-search/306528 "2022-06-07T06:30:17Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kv\_Karthik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kv_karthik/32/68273_2.png) [@Kv\_Karthik](https://discuss.elastic.co/u/Kv_Karthik)\
**Post date:** [June 7, 2022, 6:30am UTC](https://discuss.elastic.co/t/sort-based-on-a-python-list-array-and-this-list-is-not-stored-in-the-elastic-search/306528/1 "2022-06-07T06:30:17Z")

</div>

I would like to sort the hits in my results in a specific order and this order can be made available from a Python list or a python dictionary.

I already tried using the script fields like mentioned in Medium website( [Medium](https://medium.com/tensult/elasticsearch-sort-by-manual-input-order-5f366b6ea31e)) but that is also not working.

The field that i want to sort on is part of the \_source and the sorting array/object is not from es and is available externally.

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [June 8, 2022, 8:04am UTC](https://discuss.elastic.co/t/sort-based-on-a-python-list-array-and-this-list-is-not-stored-in-the-elastic-search/306528/2 "2022-06-08T08:04:26Z")

</div>

Hmm, could you clarify what do you want to do more or show some simple example?

There is a python client of Elasticsearch, but it only works according to its API. I didn't understand what is your trouble.

As it can be said at the moment, why the "part of the \_source" is not indexed as fields in the Elasticsearch index (if then, you can sort the "hits" by the field)?

---

<div class="post-metadata">

**Author:** ![Kv\_Karthik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kv_karthik/32/68273_2.png) [@Kv\_Karthik](https://discuss.elastic.co/u/Kv_Karthik)\
**Post date:** [June 8, 2022, 1:38pm UTC](https://discuss.elastic.co/t/sort-based-on-a-python-list-array-and-this-list-is-not-stored-in-the-elastic-search/306528/3 "2022-06-08T13:38:47Z")

</div>

First of thanks for your reply, here is the explanation of the sort problem  
I query Elasticsearch with list of document\_id's let's say

```auto
"document_id": [123456, 456789,123789, 789456,789123,465132,456321]

```

the above field(document\_id) is in already present in the es index and i get the results,

But the resulting hits doesn't have the same sort as I passed it above.

Now I want the hits to be sorted in the same order as the the original list  
Original list is --\> [123456, 456789,123789, 789456,789123,465132,456321]

---

<div class="post-metadata">

**Author:** ![Tomo\_M](https://avatars.discourse-cdn.com/v4/letter/t/848f3c/32.png) [@Tomo\_M](https://discuss.elastic.co/u/Tomo_M)\
**Post date:** [June 8, 2022, 2:24pm UTC](https://discuss.elastic.co/t/sort-based-on-a-python-list-array-and-this-list-is-not-stored-in-the-elastic-search/306528/4 "2022-06-08T14:24:54Z")

</div>

I suppose you queried `terms query` to query those documents whose "document\_id" field is in the list of terms.

Terms query only filter documents which match the condition. The documents are of course not aligned as the same position as the terms list and they are sorted by `_score` as default.

[Sorting search results](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html) mainly spports descending or ascending of some fields and not supports sorting according to some list.

If you want to align the sort result, you have to use

- [Script based sorting](https://www.elastic.co/guide/en/elasticsearch/reference/current/sort-search-results.html#script-based-sorting)
- [Function score query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-field-value-factor)
- [Constant score query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-constant-score-query.html) to wrap each term query  
... etc

If you are using python client and the result size is small, I recommend to sort by client side. If the result size is big, consider querying multiple times for each single term query.

---

<div class="post-metadata">

**Author:** ![Kv\_Karthik](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kv_karthik/32/68273_2.png) [@Kv\_Karthik](https://discuss.elastic.co/u/Kv_Karthik)\
**Post date:** [June 16, 2022, 2:32pm UTC](https://discuss.elastic.co/t/sort-based-on-a-python-list-array-and-this-list-is-not-stored-in-the-elastic-search/306528/5 "2022-06-16T14:32:52Z")

</div>

Thanks alot for your reply, I used the script based sorting to solve the problem

---

<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:** [July 14, 2022, 2:33pm UTC](https://discuss.elastic.co/t/sort-based-on-a-python-list-array-and-this-list-is-not-stored-in-the-elastic-search/306528/6 "2022-07-14T14:33:45Z")

</div>

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