Jordan Reiter
Jordan Reiter

Reputation: 20992

Sorting by the first element in a nested type array

According to this link describing the addition of sorting by nested fields to ElasticSearch:

Either the highest (max) or lowest (min) inner object is picked for during sorting depending on the sort_mode being used.

Is there any way, instead of having it use the highest or lowest value, it uses the first value of the array? So for example, if the record looks like this:

{
    "title": "Blah blah blah",
    "authors": [
        {
            "last": "Reiter",
            "first": "Jordan",
        },
        {
            "last": "Jim",
            "first": "Lee",
        }
    ]
}

And I want to sort by authors.last, authors.first for the given record it would use Reiter, Jordan for sorting?

Upvotes: 4

Views: 301

Answers (1)

Alex Brasetvik
Alex Brasetvik

Reputation: 11744

Unfortunately not.

I'd suggest storing the author you want to sort by in the parent document, i.e.

title: "Blah blah"
sortByAuthor: "Jordan Reiter"
authors: [
    ...
]

You can possibly get away with your current structure using a script, but you'll be loading a lot more into memory than necessary then.

Upvotes: 2

Related Questions