# Specifying filters in the suggest query

**URL:** <https://discuss.elastic.co/t/specifying-filters-in-the-suggest-query/13233>\
**Category:** Elasticsearch\
**Created:** [August 15, 2013, 8:26pm UTC](https://discuss.elastic.co/t/specifying-filters-in-the-suggest-query/13233 "2013-08-15T20:26:54Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amit\_Soni](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Amit\_Soni](https://discuss.elastic.co/u/Amit_Soni)\
**Post date:** [August 15, 2013, 8:26pm UTC](https://discuss.elastic.co/t/specifying-filters-in-the-suggest-query/13233/1 "2013-08-15T20:26:54Z")

</div>

Hi all - I see that \_suggest API has been introduced recently although it  
is still in experiment phase. I am wondering if there is a way to specify  
filter in the suggest API. This is to ensure that the suggestions are  
confined based on the specified filter.  
I tried putting a top level filter (basically like below) but that just  
filtered my query result and didn't apply for suggestions.

Any thoughts on how I can add a filter to the suggest request?

{  
"query": {  
"query\_string": {  
"query": "........",  
"default\_operator": "and"  
}  
},  
"filter": {  
"term": {  
"...........": "..........."  
}  
},  
"explain": true,  
"suggest": {  
"myTermSuggestionBuilder": {  
"text": "...............",  
"term": {  
"field": "................",  
"size": 5,  
"suggest\_mode": "always",  
"min\_word\_len": 3  
}  
},  
"myPhraseSuggestionBuilder": {  
"text": "...........",  
"phrase": {  
"field": "..................",  
"size": 5,  
"max\_errors": 0.5,  
"gram\_size": 3,  
"direct\_generator": [  
{  
"field": "...................",  
"suggest\_mode": "always",  
"min\_word\_len": 3  
}  
]  
}  
}  
}  
}

Amit

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![simonw\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/simonw_2/32/1130_2.png) [@simonw\_2](https://discuss.elastic.co/u/simonw_2)\
**Post date:** [August 15, 2013, 8:47pm UTC](https://discuss.elastic.co/t/specifying-filters-in-the-suggest-query/13233/2 "2013-08-15T20:47:05Z")

</div>

the short answer is "no" - there is no way to apply filters. I don't think  
we are able to add those without killing performance.

simon

On Thursday, August 15, 2013 10:26:54 PM UTC+2, amit.soni wrote:

> Hi all - I see that \_suggest API has been introduced recently although it  
> is still in experiment phase. I am wondering if there is a way to specify  
> filter in the suggest API. This is to ensure that the suggestions are  
> confined based on the specified filter.  
> I tried putting a top level filter (basically like below) but that just  
> filtered my query result and didn't apply for suggestions.
> 
> Any thoughts on how I can add a filter to the suggest request?
> 
> {  
> "query": {  
> "query\_string": {  
> "query": "........",  
> "default\_operator": "and"  
> }  
> },  
> "filter": {  
> "term": {  
> "...........": "..........."  
> }  
> },  
> "explain": true,  
> "suggest": {  
> "myTermSuggestionBuilder": {  
> "text": "...............",  
> "term": {  
> "field": "................",  
> "size": 5,  
> "suggest\_mode": "always",  
> "min\_word\_len": 3  
> }  
> },  
> "myPhraseSuggestionBuilder": {  
> "text": "...........",  
> "phrase": {  
> "field": "..................",  
> "size": 5,  
> "max\_errors": 0.5,  
> "gram\_size": 3,  
> "direct\_generator": [  
> {  
> "field": "...................",  
> "suggest\_mode": "always",  
> "min\_word\_len": 3  
> }  
> ]  
> }  
> }  
> }  
> }
> 
> Amit

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Amit\_Soni](https://avatars.discourse-cdn.com/v4/letter/a/c0e974/32.png) [@Amit\_Soni](https://discuss.elastic.co/u/Amit_Soni)\
**Post date:** [August 15, 2013, 9:02pm UTC](https://discuss.elastic.co/t/specifying-filters-in-the-suggest-query/13233/3 "2013-08-15T21:02:01Z")

</div>

Thanks for the quick reply Simon. In our use case, we try to main logical  
partitions of data (say we index user account information and hence we want  
to offer suggestions only relevant to user data).

Do you have recommendation on how to proceed?

-Amit.

On Thu, Aug 15, 2013 at 1:47 PM, simonw  
[simon.willnauer@elasticsearch.com](mailto:simon.willnauer@elasticsearch.com)wrote:

> the short answer is "no" - there is no way to apply filters. I don't think  
> we are able to add those without killing performance.
> 
> simon
> 
> On Thursday, August 15, 2013 10:26:54 PM UTC+2, amit.soni wrote:
> 
> > Hi all - I see that \_suggest API has been introduced recently although it  
> > is still in experiment phase. I am wondering if there is a way to specify  
> > filter in the suggest API. This is to ensure that the suggestions are  
> > confined based on the specified filter.  
> > I tried putting a top level filter (basically like below) but that just  
> > filtered my query result and didn't apply for suggestions.
> > 
> > Any thoughts on how I can add a filter to the suggest request?
> > 
> > {  
> > "query": {  
> > "query\_string": {  
> > "query": "........",  
> > "default\_operator": "and"  
> > }  
> > },  
> > "filter": {  
> > "term": {  
> > "...........": "..........."  
> > }  
> > },  
> > "explain": true,  
> > "suggest": {  
> > "myTermSuggestionBuilder": {  
> > "text": "...............",  
> > "term": {  
> > "field": "................",  
> > "size": 5,  
> > "suggest\_mode": "always",  
> > "min\_word\_len": 3  
> > }  
> > },  
> > "myPhraseSuggestionBuilder": {  
> > "text": "...........",  
> > "phrase": {  
> > "field": "..................",  
> > "size": 5,  
> > "max\_errors": 0.5,  
> > "gram\_size": 3,  
> > "direct\_generator": [  
> > {  
> > "field": "...................",  
> > "suggest\_mode": "always",  
> > "min\_word\_len": 3  
> > }  
> > ]  
> > }  
> > }  
> > }  
> > }
> > 
> > Amit
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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, 2:20am UTC](https://discuss.elastic.co/t/specifying-filters-in-the-suggest-query/13233/4 "2017-07-06T02:20:56Z")

</div>


