# How to filter facet result with a prefix

**URL:** <https://discuss.elastic.co/t/how-to-filter-facet-result-with-a-prefix/9446>\
**Category:** Elasticsearch\
**Created:** [October 23, 2012, 12:14pm UTC](https://discuss.elastic.co/t/how-to-filter-facet-result-with-a-prefix/9446 "2012-10-23T12:14:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Antoine\_Veret](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/antoine_veret/32/2670_2.png) [@Antoine\_Veret](https://discuss.elastic.co/u/Antoine_Veret)\
**Post date:** [October 23, 2012, 12:14pm UTC](https://discuss.elastic.co/t/how-to-filter-facet-result-with-a-prefix/9446/1 "2012-10-23T12:14:50Z")

</div>

Hello,

I would like to get data in order to feed a auto-complete / suggest  
component.  
I tried the following query, but i don't succeed to get only prefixed facet  
terms.

Any idea ?

Thanks

{

"size" : 10,

"query" : {

```
"query_string" : {

  "query" : "sam",

  "fields" : ["title.edge_ngram_front"]

}

```

},

"min\_score" : 0.5,

"facets" : {

```
"sugguest" : {

  "terms" : {

    "field" : "title.shingle",

    "size" : 6

  },

  "facet_filter" : {

    "prefix" : {

      "term" : "sam"

    }

  }

}

```

}

}

--

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [October 23, 2012, 1:01pm UTC](https://discuss.elastic.co/t/how-to-filter-facet-result-with-a-prefix/9446/2 "2012-10-23T13:01:22Z")

</div>

Hello Antoine,

I think the problem is that you put the facet\_filter under "facets",  
instead of under the "sugguest", which is your facet name. This way,  
you get results from the defined facet without the filter applied, and  
then a count of your facet\_filter matches.

Something like this should work as you expect it:

"facets": {  
"suggest": {  
"terms": {  
"field": "field\_name"  
},  
"size": 6,  
"facet\_filter": {  
"query": {  
"prefix": {  
"field\_name": "sam"  
}  
}  
}  
}  
}

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Tue, Oct 23, 2012 at 3:14 PM, Antoine Véret [antoine.veret@gmail.com](mailto:antoine.veret@gmail.com) wrote:

> Hello,
> 
> I would like to get data in order to feed a auto-complete / suggest  
> component.  
> I tried the following query, but i don't succeed to get only prefixed facet  
> terms.
> 
> Any idea ?
> 
> Thanks
> 
> {
> 
> "size" : 10,
> 
> "query" : {
> 
> ```
> "query_string" : {
> 
> "query" : "sam",
> 
> "fields" : ["title.edge_ngram_front"]
> 
> }
> 
> ```
> 
> },
> 
> "min\_score" : 0.5,
> 
> "facets" : {
> 
> ```
> "sugguest" : {
> 
> "terms" : {
> 
> "field" : "title.shingle",
> 
> "size" : 6
> 
> },
> 
> "facet_filter" : {
> 
> "prefix" : {
> 
> "term" : "sam"
> 
> }
> 
> }
> 
> }
> 
> ```
> 
> }
> 
> }

--

---

<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:07am UTC](https://discuss.elastic.co/t/how-to-filter-facet-result-with-a-prefix/9446/3 "2017-07-06T03:07:29Z")

</div>

Closed on July 6, 2017, 3:07am UTC


