# Best way to build filter for query using FilterBuilder methods?

**URL:** <https://discuss.elastic.co/t/best-way-to-build-filter-for-query-using-filterbuilder-methods/6546>\
**Category:** Elasticsearch\
**Created:** [January 30, 2012, 10:12pm UTC](https://discuss.elastic.co/t/best-way-to-build-filter-for-query-using-filterbuilder-methods/6546 "2012-01-30T22:12:05Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![david\_webb](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@david\_webb](https://discuss.elastic.co/u/david_webb)\
**Post date:** [January 30, 2012, 10:12pm UTC](https://discuss.elastic.co/t/best-way-to-build-filter-for-query-using-filterbuilder-methods/6546/1 "2012-01-30T22:12:05Z")

</div>

Hello guys,

I'm new to this, please excuse me if this sounds like a dumb  
questions. I'm still trying to figure it out how the java API works.

So I'm in need of build filters for my query (I've got between 0 to 5  
filters) per search. And I'm using and "AndFilter.add" method to mix  
them if they were passed as a search filter.

I found out it works just fine if I've got 2 or more filters. But I've  
noticed that if I only pass one filter the generated JSON query fails  
since it ends building up something like

filter: {  
filters: { one\_filter\_here}  
}

And ES doesn't like that.

What's the best strategy to build those filters so it has the  
intelligence of just throw "filter" if one argument is passed, and  
fitler: {filters: ..}} if more that one is passed?

---

<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 31, 2012, 3:57pm UTC](https://discuss.elastic.co/t/best-way-to-build-filter-for-query-using-filterbuilder-methods/6546/2 "2012-01-31T15:57:43Z")

</div>

Which client are you using? Can you gist the full search request that fails? The and filter can support a single filter, but it needs to exists within an array still.

On Tuesday, January 31, 2012 at 12:12 AM, david webb wrote:

> Hello guys,
> 
> I'm new to this, please excuse me if this sounds like a dumb  
> questions. I'm still trying to figure it out how the java API works.
> 
> So I'm in need of build filters for my query (I've got between 0 to 5  
> filters) per search. And I'm using and "AndFilter.add" method to mix  
> them if they were passed as a search filter.
> 
> I found out it works just fine if I've got 2 or more filters. But I've  
> noticed that if I only pass one filter the generated JSON query fails  
> since it ends building up something like
> 
> filter: {  
> filters: { one\_filter\_here}  
> }
> 
> And ES doesn't like that.
> 
> What's the best strategy to build those filters so it has the  
> intelligence of just throw "filter" if one argument is passed, and  
> fitler: {filters: ..}} if more that one is passed?

---

<div class="post-metadata">

**Author:** ![david\_webb](https://avatars.discourse-cdn.com/v4/letter/d/b9bd4f/32.png) [@david\_webb](https://discuss.elastic.co/u/david_webb)\
**Post date:** [January 31, 2012, 7:39pm UTC](https://discuss.elastic.co/t/best-way-to-build-filter-for-query-using-filterbuilder-methods/6546/3 "2012-01-31T19:39:05Z")

</div>

Thanks Shay

I think I found the problem, it was related to a filter but in my  
facet it was going empty.

Cheers

David

On Jan 31, 10:57 am, Shay Banon [kim...@gmail.com](mailto:kim...@gmail.com) wrote:

> Which client are you using? Can you gist the full search request that fails? The and filter can support a single filter, but it needs to exists within an array still.
> 
> On Tuesday, January 31, 2012 at 12:12 AM, david webb wrote:
> 
> > Hello guys,
> 
> > I'm new to this, please excuse me if this sounds like a dumb  
> > questions. I'm still trying to figure it out how the java API works.
> 
> > So I'm in need of build filters for my query (I've got between 0 to 5  
> > filters) per search. And I'm using and "AndFilter.add" method to mix  
> > them if they were passed as a search filter.
> 
> > I found out it works just fine if I've got 2 or more filters. But I've  
> > noticed that if I only pass one filter the generated JSON query fails  
> > since it ends building up something like
> 
> > filter: {  
> > filters: { one\_filter\_here}  
> > }
> 
> > And ES doesn't like that.
> 
> > What's the best strategy to build those filters so it has the  
> > intelligence of just throw "filter" if one argument is passed, and  
> > fitler: {filters: ..}} if more that one is passed?

---

<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:40am UTC](https://discuss.elastic.co/t/best-way-to-build-filter-for-query-using-filterbuilder-methods/6546/4 "2017-07-06T03:40:56Z")

</div>

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


