I want sort by name
, but keep entries with null or missing price
data at the end of the results.
I have tried:
sort: {
sorting.name: {
order: "asc"
},
prices.minprice.price: {
missing: "_last"
}
}
but this only sorts by name.
If you sort a column with NULL values in ascending order, the NULLs will come first. Alternatively, if you add a DESC keyword to get a descending order, NULLs will appear last.
A null value cannot be indexed or searched. When a field is set to null , (or an empty array or an array of null values) it is treated as though that field has no values. Replace explicit null values with the term NULL . An empty array does not contain an explicit null , and so won't be replaced with the null_value .
Specifying the order of NULL valuesIn an ascending sort, the ASC NULLS FIRST keywords request the default order. In a descending sort, DESC NULLS FIRST specifies that rows with a NULL value in the sort key column precede non-NULL rows in the sorted result set.
In Elasticsearch, the relevance score is represented by the floating-point number returned in the search results as the _score , so the default sort order is _score descending.
Actually you could use the missing
order.
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-sort.html#_missing_values
To do this we can use a custom function score (which is faster than custom script based sorting according to elasticsearch documentation). When price is missing, the numeric value will return 0.
We then sort on score first to split into a section with missing price data, and a section without missing price data, and then sort each of these two sections based on name.
{
"query": {
"function_score": {
"boost_mode": "replace",
"query": {"match_all": {}},
"script_score": {
"script": "doc['price'].value == 0 ? 0 : 1"
}
}
},
"sort": [
"_score",
"name"
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With