Posts

Showing posts with the label Mvel

Conditional Sorting In ElasticSearch

Answer : Yes this is possible in ElasticSearch using a script, either for sorting or for scoring. My preference would be for a scoring script because 'script based score' is going to be quicker (according to the documentation). Using a scoring script, you could use the Unix timestamp for the date field of type int/long and an mvel sorting script in the custom_score query. You might need to re-index your documents. You would also need to be able to convert the searched for time into a Unix timestamp to pump it at ElasticSearch. The sorting script would then deduct the requested timestamp from each document's timestamp and make an absolute value. Then the results are sorted in ascending order - the lowest 'distance' is the best. So when looking for documents dated about a year ago, it would look something like: "query": { "custom_score" : { "query" : { .... }, "params" : { ...