Max
Max

Reputation: 9879

Does NEST support script-based sorting?

Here's the ElasticSearch query I'd like to build using NEST:

{
    "from": 0,
    "size": 10,
    "sort": {
        "_script": {
            "script": "doc['name'].value=='my perfect match' ? 1 : 0",
            "type" : "number",
            "order" : "desc"
        },
        "_score": {}
    },
    "query": {
       ....
    }
}

(simplified - really I'm sorting by a few more attributes. The point of the script-based sort is to promote exact matches to the top of the result set.)

Explicitly sorting by _score is not a problem...

.Sort(sort => sort.OnField("_score"))

... but NEST doesn't seem to have an equivalent operator for the script-based sort yet - or does it?

Upvotes: 1

Views: 538

Answers (1)

Martijn Laarman
Martijn Laarman

Reputation: 13536

NEST supports script sorts through SortScript()

See the unit tests for them here:

https://github.com/Mpdreamz/NEST/blob/master/src/Nest.Tests.Unit/Search/Sort/SortTests.cs#L171

Upvotes: 2

Related Questions