Query String Search format to return strings starting with search term

Hello! I'm using query_string to perform search on my index and retrieve the documents that have a partial match of the search term.

So, before I introduce my question, I'll tell a little about the search environment. I have an index called Jobs and each document has a field called role. So, for the purpose of this question, I can say that I have 4 documents inserted and each one has the following roles: "Developer", "iOS Developer", "Android Developer", "Developer DevOps".

When I search for "dev", what I want it's something like this on the response: ["Developer", "Developer DevOps", "iOS Developer", "Android Developer"].

But what I get from the response it's not in the "sorted" form I was looking for: ["iOS Developer", "Developer", "Android Developer", "Developer DevOps"].

This is my search code:

GET jobs/doc/_search
{
    "query": {
        "query_string" : {
            "query" : "role: dev*"
        }
    }
}

So to summarize, I want based on the search term, retrieve the roles ordered first by the ones that start with it and the remaining strings that has the search term in the middle. I've also tried RegExp and filter, but none of them helped me with this issue.

If someone could help pointing me the best way to achieve this kind of thing, I'd very thankful.

Thanks!

in order to do something like this you need 2 fields, one that indexes the roles as keywords and one that indexes the roles as strings and splits on whitespaces then you get better scores for hits that start with your term. you also need to search on both field in that case.

Hello, thanks for the reply!
Good to know about that, I got stuck for days on it.

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