Hello Kevin,
Here's something that should work, but it's going to be slow:
"sort": {
"_script": {
"script": "if (_source.version == \"\") { \"zzz\" } else {
_source.version }",
"type": "string",
"order": "asc"
}
}
If you want it fast, I think you should look at indexing your versions as
numbers. Then you can use "missing" to put docs without a "version" field
either at the beginning or at the end:
In case your versioning format can't be easily indexed as numbers (because
of multiple dots), you can index parts in different fields. And then you
can sort on all of them by specifying an array there.
Best regards,
Radu
http://sematext.com/ -- Elasticsearch -- Solr -- Lucene
On Sat, Dec 15, 2012 at 10:43 PM, Kevin Tran hekevintran@gmail.com wrote:
I am using Django, Haystack, and Elasticsearch. I want to order my search
results so that results where the ordered field value is empty ("") come
after results where it is not empty. I cannot find an API in Haystack that
can do this. The request sent to Elasticsearch looks like:{ "sort":[ { "version":{ "order":"asc" } } ], "query":{ ... } }
Is there a way to rewrite this Elasticsearch query so that results with an
empty string for "version" will come after results where "version" exists?I have implemented this in Python as:
sorted(sqs, key=lambda x: getattr(x, 'version') == '')
--
--