Search for startsWith and contains combined with sorting

Hello,

I have a query that looks like the following:

    {
    "sort": [
        {
            "_score": "DESC",
            "name.sort": "ASC"
        }
    ],
    "size": "250",
    "query": {
        "bool": {
            "should": [
                {
                    "match_phrase": {
                        "name": "ka"
                    }
                },
                {
                    "match_phrase": {
                        "name.prefix": {
                            "query": "ka",
                            "boost": "10.0"
                        }
                    }
                }
            ],
            "minimum_should_match": 1
        }
    }
}

What I try to achieve is the following. When I query for 'ka', I want my result to first contain all the documents where 'name' starts with 'ka' (sorted alphabetically) and then the documents that where 'ka' appears in the 'name' but not starts with it, also sorted alphabetically.

so the result should look like something like this:
kabel
kado
kat
daka
podoka
...

I hope the question is clear. Is there an easy solution for this? It seems such a common practise.
I have tried with constant_score... my hope was to have all the documents with startsWith a constant score of 1, and the other words that just contain a score with 0. Then I could sort first by score and the by name in order to achieve this.

Any ideas?

Thanks in advance!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.