# Query String Search format to return strings starting with search term

**URL:** <https://discuss.elastic.co/t/query-string-search-format-to-return-strings-starting-with-search-term/154494>\
**Category:** Elasticsearch\
**Created:** [October 29, 2018, 7:22pm UTC](https://discuss.elastic.co/t/query-string-search-format-to-return-strings-starting-with-search-term/154494 "2018-10-29T19:22:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![gutiago](https://avatars.discourse-cdn.com/v4/letter/g/f04885/32.png) [@gutiago](https://discuss.elastic.co/u/gutiago)\
**Post date:** [October 29, 2018, 7:22pm UTC](https://discuss.elastic.co/t/query-string-search-format-to-return-strings-starting-with-search-term/154494/1 "2018-10-29T19:22:44Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![s1monw](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/s1monw/32/3637_2.png) [@s1monw](https://discuss.elastic.co/u/s1monw)\
**Post date:** [November 5, 2018, 1:51pm UTC](https://discuss.elastic.co/t/query-string-search-format-to-return-strings-starting-with-search-term/154494/2 "2018-11-05T13:51:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![gutiago](https://avatars.discourse-cdn.com/v4/letter/g/f04885/32.png) [@gutiago](https://discuss.elastic.co/u/gutiago)\
**Post date:** [November 10, 2018, 12:12pm UTC](https://discuss.elastic.co/t/query-string-search-format-to-return-strings-starting-with-search-term/154494/3 "2018-11-10T12:12:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 8, 2018, 12:12pm UTC](https://discuss.elastic.co/t/query-string-search-format-to-return-strings-starting-with-search-term/154494/4 "2018-12-08T12:12:18Z")

</div>

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