Stefan Weiss
Stefan Weiss

Reputation: 461

ElasticSearch: sort in specific order

i have an indexed string field in my docs with letters a-z as possible values.
Is it possible to sort the query result in specific(given) order?
Something like:

{
    "sort" : [
        { "letters" : {"order" : ["k", "g", "a"...]}}
}

Upvotes: 4

Views: 4647

Answers (1)

zt1983811
zt1983811

Reputation: 1017

I think instead of sorting you can use function score

"query": {
    "function_score": {
        "query": {},
        "functions": [
           {
                "field_value_factor": 
                {
                    "field": "rank",
                    "factor": 1
                }
            },
            {
                "filter": {"term": {"letters": "k"}},
                "weight": 50
            },
            {
                "filter": {"term": {"letters": "g"}},
                "weight": 40
            },
            {
                "filter": {"term": {"letters": "a"}},
                "weight": 30
            }
        ],
        "score_mode": "sum",
        "boost_mode": "sum"
    }
}

Upvotes: 3

Related Questions