Elasticsearch sort based on element in multivalue field

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

You'd need a script to do this. See Sort | Elasticsearch Guide [2.4] | Elastic

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?

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"
        }
    }
}