# Prefix question

**URL:** <https://discuss.elastic.co/t/prefix-question/6413>\
**Category:** Elasticsearch\
**Created:** [January 17, 2012, 4:13pm UTC](https://discuss.elastic.co/t/prefix-question/6413 "2012-01-17T16:13:32Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Valentin\_Pletzer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/valentin_pletzer/32/105548_2.png) [@Valentin\_Pletzer](https://discuss.elastic.co/u/Valentin_Pletzer)\
**Post date:** [January 17, 2012, 4:13pm UTC](https://discuss.elastic.co/t/prefix-question/6413/1 "2012-01-17T16:13:32Z")

</div>

Hi,

I have a question about the use of the prefix filter. Suppose I have  
two records with a field "phrase" which have the values "apple banana"  
and "banana cherry" and lets assume I want to make a prefix-search for  
"b" on phrase (autocompletion). What do I have to do, to only get the  
record "banana cherry" but not the record "apple banana"?

Right now I use the following filter-query:

{  
"filter": {  
"bool": {  
"must": [  
{  
"prefix": {  
"phrase": "z"  
}  
}  
]  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![otisg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/otisg/32/492_2.png) [@otisg](https://discuss.elastic.co/u/otisg)\
**Post date:** [January 18, 2012, 5:38am UTC](https://discuss.elastic.co/t/prefix-question/6413/2 "2012-01-18T05:38:52Z")

</div>

Hi Valentin,

You need to make sure your fields are not broken into tokens on space,  
but just "left edge n-gramed".

## Otis

Sematext is hiring Elasticsearch / Solr developers --

> **[Jobs](https://sematext.com/jobs/)**
>
> We’re Hiring We are always looking for smart, passionate, motivated, and independent people regardless of where on the planet they may be. Learn more about the company Agent & Backend Engineer Full Stack Developer Backend Engineer Frontend...

On Jan 17, 11:13 am, Valentin [plet...@gmail.com](mailto:plet...@gmail.com) wrote:

> Hi,
> 
> I have a question about the use of the prefix filter. Suppose I have  
> two records with a field "phrase" which have the values "apple banana"  
> and "banana cherry" and lets assume I want to make a prefix-search for  
> "b" on phrase (autocompletion). What do I have to do, to only get the  
> record "banana cherry" but not the record "apple banana"?
> 
> Right now I use the following filter-query:
> 
> {  
> "filter": {  
> "bool": {  
> "must": [  
> {  
> "prefix": {  
> "phrase": "z"  
> }  
> }  
> ]  
> }  
> }
> 
> }

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [January 18, 2012, 8:58pm UTC](https://discuss.elastic.co/t/prefix-question/6413/3 "2012-01-18T20:58:18Z")

</div>

Another option is to use text query with the phrase prefix option:  
[Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/text-query.html).

On Wed, Jan 18, 2012 at 7:38 AM, Otis Gospodnetic \<  
[otis.gospodnetic@gmail.com](mailto:otis.gospodnetic@gmail.com)\> wrote:

> Hi Valentin,
> 
> You need to make sure your fields are not broken into tokens on space,  
> but just "left edge n-gramed".
> 
> ## Otis
> 
> Sematext is hiring Elasticsearch / Solr developers --  
> [Jobs - Sematext](http://sematext.com/about/jobs.html)
> 
> On Jan 17, 11:13 am, Valentin [plet...@gmail.com](mailto:plet...@gmail.com) wrote:
> 
> > Hi,
> > 
> > I have a question about the use of the prefix filter. Suppose I have  
> > two records with a field "phrase" which have the values "apple banana"  
> > and "banana cherry" and lets assume I want to make a prefix-search for  
> > "b" on phrase (autocompletion). What do I have to do, to only get the  
> > record "banana cherry" but not the record "apple banana"?
> > 
> > Right now I use the following filter-query:
> > 
> > {  
> > "filter": {  
> > "bool": {  
> > "must": [  
> > {  
> > "prefix": {  
> > "phrase": "z"  
> > }  
> > }  
> > ]  
> > }  
> > }
> > 
> > }

---

<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:** [July 6, 2017, 3:42am UTC](https://discuss.elastic.co/t/prefix-question/6413/4 "2017-07-06T03:42:15Z")

</div>


