RSS

Category Archives: Dev tools

Moving Null/Empty Values to End of Results Collection in Elastic Search Results

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.

Problem

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.

sorting elastic search

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!!

Advertisements
 
Leave a comment

Posted by on May 17, 2016 in ASP.NET, Dev tools, Elastic Search

 

Tags: , ,

Using Google Chrome As A Simple Web Text Editor

All web developers have awesome text editors or beyond text editors which we call IDEs which do many services such as Visual Studio, Web Storm, Sublime….. (names are a very few here).
However when we develop websites when it comes to debugging many developers use either firebug or chrome devtools. These tools also allows you to do “live edit” which is very compelling. For chrome devtools most of the time the work flow is as follows,
• Using chrome devtools edit CSS, JavaScript etc. within chrome devtools itself
• Copy the edited CSS,JavaScript etc from chrome devtools and copy paste to corresponding source files in the disk
However above workflow bit annoying but most of the time developers live with. The reason is it is not only additional work but error prone thing where you might miss certain lines of code from copy pasting process especially if you edit multiple source files through chrome devtools.

What you can do is to use Chrome devtools “workspaces” feature. In summary what workspaces allow you to save edited CSS, Javascript into the disk through chrome devtools itself which by removing additional step of copy pasting them into source files which is the annoying and error prone step. If you wonder how this is possible you just map your location of source files to disk. Yes this might not always work for all virtual folders but most of the time it will.

First step is to open chrome devtools and right click on source tabs white area as shown below.
add-workspace

Clicking on Add Folder to workplace will open OS file/folder browser.

browse-local-folder

After you do it, the edits you do will be persistable to your disk right from chrome devtools it self(with Ctrl + S)
3. save to disk after edit

There are number of other operations you could do such as deleting files, duplicating souce files… etc. from chrome dev tools, if you just right click on the source file in “Sources” tab you will see some of these options.

 
Leave a comment

Posted by on March 11, 2015 in Dev tools

 

Tags: , , ,