# Elasticsearch sort based on element in multivalue field

**URL:** <https://discuss.elastic.co/t/elasticsearch-sort-based-on-element-in-multivalue-field/60039>\
**Category:** Elasticsearch\
**Created:** [September 8, 2016, 5:17am UTC](https://discuss.elastic.co/t/elasticsearch-sort-based-on-element-in-multivalue-field/60039 "2016-09-08T05:17:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![seperman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seperman/32/11550_2.png) [@seperman](https://discuss.elastic.co/u/seperman)\
**Post date:** [September 8, 2016, 5:17am UTC](https://discuss.elastic.co/t/elasticsearch-sort-based-on-element-in-multivalue-field/60039/1 "2016-09-08T05:17:23Z")

</div>

Is it possible to sort based on element existing in multi value field?

Example:

```
a) document with "111" 

put test/test/1
{
   "bit_position" : [
        1,
        2,
        3
    ]
}

b) document with 010
put test/test/2
{
   "bit_position": [
      2
   ]
}

```

Sorting based on "bit\_position" = 3 should return document a and then b.

Thank you

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [September 8, 2016, 8:16am UTC](https://discuss.elastic.co/t/elasticsearch-sort-based-on-element-in-multivalue-field/60039/2 "2016-09-08T08:16:26Z")

</div>

> [@seperman](#):
>
> Sorting based on "bit\_position" = 3 should return document a and then b.

You'd need a script to do this. See [Sort | Elasticsearch Guide [2.4] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/2.4/search-request-sort.html#_script_based_sorting)

---

<div class="post-metadata">

**Author:** ![seperman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seperman/32/11550_2.png) [@seperman](https://discuss.elastic.co/u/seperman)\
**Post date:** [September 8, 2016, 5:24pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-based-on-element-in-multivalue-field/60039/3 "2016-09-08T17:24:49Z")

</div>

Is it not that using the script feature is memory expensive? I just had an idea:

- filter where bit\_position = 3
- filter where bit\_position != 3
- Chain the 2 filters in a way that it unions the results (basically extend the first result set with the second result set). (Not sure how to do it in ES)

This way anything with bit\_position = 3 shows up before the rest.  
What do you think? Is it possible to do it in ES or do I have to do 2 separate requests to ES and union the results manually?

---

<div class="post-metadata">

**Author:** ![seperman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/seperman/32/11550_2.png) [@seperman](https://discuss.elastic.co/u/seperman)\
**Post date:** [September 12, 2016, 11:23pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-based-on-element-in-multivalue-field/60039/4 "2016-09-12T23:23:14Z")

</div>

I ended up doing it using function\_score:

```
PUT test/test/6
{
   "bit_position" : [
        1,
        2,
        3,
        6
    ]
}

POST test/_search
{
    "query": {
        "function_score": {
            "match_all": {},
            "functions": [
                {
                    "filter": {
                        "terms": {
                           "bit_position": [
                              1,
                              3
                           ]
                        }
                    },
                    "weight": 2
                }
            ],
            "score_mode": "multiply"
        }
    }
}
```

---

<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 5, 2017, 10:20pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-based-on-element-in-multivalue-field/60039/5 "2017-07-05T22:20:55Z")

</div>


