# Elasticsearch query for "starts with "searching on whole string

**URL:** https://discuss.elastic.co/t/elasticsearch-query-for-starts-with-searching-on-whole-string/86503
**Category:** Elasticsearch
**Created:** [May 20, 2017, 12:31am UTC](https://discuss.elastic.co/t/elasticsearch-query-for-starts-with-searching-on-whole-string/86503 "2017-05-20T00:31:34Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Sharat\_Jagannath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sharat_jagannath/32/24299_2.png) [@Sharat\_Jagannath](https://discuss.elastic.co/u/Sharat_Jagannath)
#### Post date: [May 20, 2017, 12:31am UTC](https://discuss.elastic.co/t/elasticsearch-query-for-starts-with-searching-on-whole-string/86503/1 "2017-05-20T00:31:35Z")

</div>

i have documents with a field called title having data like "the lord of the rings","lord of the rings","the ring",etc  
I would like to do a search as you type feature.  
So if user types "th", the order of the results should be -  
"the lord of the ring",  
"the ring",  
"lord of the rings"  
since i want the strings that start with "th" to appear first and alphabetically.  
i tried looking into edgengrams, but that does it for every word in the string.  
I would like to do it only from beginning of string.

Can you please let me know what are the analyzers i need to use to achieve this?

Thanks

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [May 26, 2017, 1:59pm UTC](https://discuss.elastic.co/t/elasticsearch-query-for-starts-with-searching-on-whole-string/86503/2 "2017-05-26T13:59:56Z")

</div>

The two usual approaches for this are:

- Use edge n-grams, but with the `keyword` tokenizer. As you noted, if you use ngrams with a default tokenizer (`standard`, etc), you'll get ngrams for each word independently. The `keyword` tokenizer only emits a single token however; the original string. So `keyword` + `edge-ngram` will give you what you want.

- Use `match_phrase_prefix`. If you search for `"the lo"`, that query will essentially put a wildcard at the end so it becomes `"the lo*"`. It maintains phrase matching semantics, so it will only match `"the lord"`, `"the look"`, etc.

---

<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: [June 23, 2017, 2:00pm UTC](https://discuss.elastic.co/t/elasticsearch-query-for-starts-with-searching-on-whole-string/86503/3 "2017-06-23T14:00:16Z")

</div>

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