You need to use a function_score
The function_score allows you to modify the score of documents that are retrieved by a query. This can be useful if, for example, a score function is computationally expensive and it is sufficient to compute the score on a filtered set of documents.
Query
{
"query": {
"function_score": {
"query": {
"ids": {
"values": [
"BqQI7ncBXJtfdGR-NWLA",
"P6QAX3gBXJtfdGR-oWLL",
"NKSJP3gBXJtfdGR-vWIY",
"PqT-XXgBXJtfdGR-O2Kg"
]
}
},
"script_score": {
"script": {
"source": """
def count = params.ids.size();
def id = doc['_id'].value;
for (int i = 0; i < count; i++) {
if (id == params.ids[i]) { return count - i; }
}
""",
"params": {
"ids": [
"BqQI7ncBXJtfdGR-NWLA",
"P6QAX3gBXJtfdGR-oWLL",
"NKSJP3gBXJtfdGR-vWIY",
"PqT-XXgBXJtfdGR-O2Kg"
]
}
}
}
}
},
"size": 10,
"from": 0
}