5

I'm using the following to sort documents in Elasticsearch that have a 'usernamesAssigned' property. usernamesAssigned is an array of strings:

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

I'm wondering if there's a more efficient way to do this without using script based sorting?

1 Answer 1

2

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

According to the documentation:

Elasticsearch supports sorting by array or multi-valued fields. The mode option controls what array value is picked for sorting the document it belongs to.

So, you should be able to sort like this:

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

This has been available since version 0.90.0.Beta1.

Sign up to request clarification or add additional context in comments.

1 Comment

not working for me { "query": { "bool": { "must": [{ "range": { "date": { "gte": "2006-10-19T00:00:00.000Z", "lte": "2019-11-01T00:00:00.000Z" } } }] } }, "sort": [{ "from": { "order": "asc", "mode" : "min" } }] }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.