You would need a custom sort using script to achieve what you want.
I’ve just made use of generic match_all
query for my query, you can probably go ahead and add your query logic there, but the solution that you are looking for is in the sort
section of the below query.
Make sure that status
is a keyword type
Custom Sorting Based on Values
POST <your_index_name>/_search { "query":{ "match_all":{ } }, "sort":[ { "_score": "desc" }, { "_script":{ "type":"number", "script":{ "lang":"painless", "inline":"if(params.scores.containsKey(doc['status'].value)) { return params.scores[doc['status'].value];} return 100000;", "params":{ "scores":{ "active":5, "old":4, "cancelled":3 } } }, "order":"desc" } } ] }
In the above query, go ahead and add the values in the scores
section of the query. For e.g. if your value is new
and you want it to be at say value 2
, then your scores would be in the below:
{ "scores":{ "active":5, "old":4, "cancelled":3, "new":6 } }
So basically the documents would first get sorted by _score
and then on that sorted documents, the script sort would be executed.
Note that the script sort is desc
by nature as I understand that you would want to show active
documents at the top, followed by other values. Feel free to play around with it.
Hope this helps!