Sort by array of strings in ElasticSearch

I use the following to sort documents in Elasticsearch that have the usernamesAssigned property. usernamesAssigned is an array of strings:

"sort": [
    {
        "_script": {
            "script": "doc["usernamesAssigned"].values.sort().join()",
            "type": "string",
            "lang": "js",
            "order": "asc"
        }
    }
]

I am wondering if there is a more efficient way to do this without using script-based sorting?

+5
source share
1 answer

This is an old question, but I recently wandered around it trying to solve the same problem ...

According to the documentation :

Elasticsearch supports sorting by arrays or multi-valued fields. mode determines which value of the array is selected for sorting the document belongs.

So you should be able to sort like this:

"sort" : [ {"usernamesAssigned" : {"order" : "asc", "mode" : "min"}} ]

It is available from version 0.90.0.Beta1.

+3
source

All Articles