0

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.

I read about this being possible as nested field but can't find any information about it when bit_position is not nested.

I found this question: Sorting by value in multivalued field in elasticsearch but it has not been answered.

Thank you

Community
  • 1
  • 1
Seperman
  • 4,254
  • 1
  • 28
  • 27

1 Answers1

0

I solved this by using function_score:

POST test/_search
{
    "query": {
        "function_score": {
            "match_all": {},
            "functions": [
                {
                    "filter": {
                        "terms": {
                           "bit_position": [
                              1,
                              3
                           ]
                        }
                    },
                    "weight": 2
                }
            ],
            "score_mode": "multiply"
        }
    }
}
Seperman
  • 4,254
  • 1
  • 28
  • 27