Elastic search is a great enterprise level full text search engine with multi-tenant capability distributed search server setup and which provides http based web API to communicate with the server. When communicating with Elastic search server, it is worth using Nest library as .NET client.
I was helping to a colleague where he needed to sort elastic search results by selling price of items by ascending order (the ES documents had a field for selling price for products). This is easily achievable if you use nest.
var searchDescriptor = new SearchDescriptor<MyEsDocument>(); searchDescriptor.SortAscending(x => x.SellingPrice);
However, one problem that he was facing is all the products that do not have a price will be returned on first set of results when sorted as depicted below for very simple data set.
This can be solved in multiple ways such as query’s score boosting, introducing a additional “flag field” (which is undesirable since index size might grow but queries perform better).
The solution I used is “script” sort for elastic search. This can be easily achievable with Nest using code similar to following.
var sortScriplet = "doc['sellingPrice'].value ? 0:1"; searchDescriptor.SortScript(x => x.Type("number") .Order(SortOrder.Descending).Script(sortScriplet)) .SortAscending(x => x. sellingPrice);
First if the selling price has acceptable value documents will be flagged with 1 and the rest with 0 and will be sorted with descending order. This will bring all resulting objects that have selling price to top. Then the actual selling price field will be used for sorting within the initially partitioned result set leads to effectively sending blank values to bottom of the result set!!