4

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"...]}}
}

1 Answer 1

3

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"
    }
}
Sign up to request clarification or add additional context in comments.

9 Comments

Thx, I will test it. But unfortunately my question was incomplete, I need to sort on 2 fields: first on letters and then on integer field(ranking) on same letters. How to do it with function score?
I need the following: if the letters are equal, order by ranking. in mysql: ORDER BY FIELD(letters, 'k', 'g', 'a'), ranking
it doesn't work as expected. More "weight" results in less score. i have played with boost mode and score mode, but the letters in results still can be mixed
So:) My rank is in range 1 to 1000, so i gave "weight" 50000 to 100000 to letters. It Works! Thx!
Is there a way of implementing this using the java api?
|

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.