Sort ascending with null values last

(Nick Hoffman) #1

Ahoy. When sorting on a string field in ascending order, is there a way to
move the documents with a null value for the sort-field to the end of the

For example, with the setup below, we sort by ascending city, but Bob has
no city and is the first result. Can we cause Bob to be the last result?

curl -s -X DELETE localhost:9200/test
curl -X PUT 'localhost:9200/test?pretty=true'

curl -XPUT localhost:9200/test/people/1 -d '{
"name" : "Jane",
"city" : "Toronto"

curl -XPUT localhost:9200/test/people/2 -d '{
"name": "Jack",
"city": "Ottawa"

curl -XPUT localhost:9200/test/people/3 -d '{
"name": "Bob"

curl 'localhost:9200/test/people/_search' -d '{ sort: [{"city": {"order":
"asc"} }] }'

(Jan Fiedler) #2

I created an issue for this some time ago (as I need the feature too): (just in case you
want to vote it up ... ;-))

(Nick Hoffman) #3

Thanks for the link, Jan. I just commented on it.

(Nick Hoffman) #4

The only workaround that I can think of is to modify one's query to exclude
documents with a null value for the sort-field.

Can you think of any other workarounds until #896 is implemented?

(system) #5