# To provide the result of two queries to Filter

**URL:** https://discuss.elastic.co/t/to-provide-the-result-of-two-queries-to-filter/5665
**Category:** Elasticsearch
**Created:** [October 24, 2011, 8:59am UTC](https://discuss.elastic.co/t/to-provide-the-result-of-two-queries-to-filter/5665 "2011-10-24T08:59:09Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![arien](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@arien](https://discuss.elastic.co/u/arien)
#### Post date: [October 24, 2011, 8:59am UTC](https://discuss.elastic.co/t/to-provide-the-result-of-two-queries-to-filter/5665/1 "2011-10-24T08:59:09Z")

</div>

Hi;  
I am Using Two queries

QueryBuilder qb1=textQuery("Subject", "Biometric");  
QueryBuilder qb2=textPhraseQuery("Subject", "Biometric");  
Filterbuilder fb =FilterBuilders.termFilter("UsersOnMail",  
"ajay.padvi")

And in SearchRequestBuilder I want to give the result of qb1 AND qb2  
to FilterBuilder fb.Here I am able to provide single query either qb1  
or qb2;  
SearchRequestBuilder request =  
client.prepareSearch(index).setFilter(fb).setTypes(mapping).setQuery(qb1);

But i want this filter to be applied to results of both qb1 and qb2.  
Please Help me with this.

Thanks;

---

<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: [October 24, 2011, 10:38pm UTC](https://discuss.elastic.co/t/to-provide-the-result-of-two-queries-to-filter/5665/2 "2011-10-24T22:38:40Z")

</div>

Create a bool filter and add qb1 and qb2 as either must or should clauses  
(not sure which behavior you want between the two). Then, use a  
filteredQuery, provide the bool query you have to it, and the filter you  
create as the filter.

On Mon, Oct 24, 2011 at 10:59 AM, arien [ajaypadvi@gmail.com](mailto:ajaypadvi@gmail.com) wrote:

> Hi;  
> I am Using Two queries
> 
> QueryBuilder qb1=textQuery("Subject", "Biometric");  
> QueryBuilder qb2=textPhraseQuery("Subject", "Biometric");  
> Filterbuilder fb =FilterBuilders.termFilter("UsersOnMail",  
> "ajay.padvi")
> 
> And in SearchRequestBuilder I want to give the result of qb1 AND qb2  
> to FilterBuilder fb.Here I am able to provide single query either qb1  
> or qb2;  
> SearchRequestBuilder request =  
> client.prepareSearch(index).setFilter(fb).setTypes(mapping).setQuery(qb1);
> 
> But i want this filter to be applied to results of both qb1 and qb2.  
> Please Help me with this.
> 
> Thanks;

---

<div class="post-metadata">

### Author: ![arien](https://avatars.discourse-cdn.com/v4/letter/a/e0b2c6/32.png) [@arien](https://discuss.elastic.co/u/arien)
#### Post date: [October 25, 2011, 6:35am UTC](https://discuss.elastic.co/t/to-provide-the-result-of-two-queries-to-filter/5665/3 "2011-10-25T06:35:00Z")

</div>

Thank you shay,filteredQuery helped me achieving expected results.

On Tue, Oct 25, 2011 at 4:08 AM, Shay Banon [kimchy@gmail.com](mailto:kimchy@gmail.com) wrote:

> Create a bool filter and add qb1 and qb2 as either must or should clauses  
> (not sure which behavior you want between the two). Then, use a  
> filteredQuery, provide the bool query you have to it, and the filter you  
> create as the filter.
> 
> On Mon, Oct 24, 2011 at 10:59 AM, arien [ajaypadvi@gmail.com](mailto:ajaypadvi@gmail.com) wrote:
> 
> > Hi;  
> > I am Using Two queries
> > 
> > QueryBuilder qb1=textQuery("Subject", "Biometric");  
> > QueryBuilder qb2=textPhraseQuery("Subject", "Biometric");  
> > Filterbuilder fb =FilterBuilders.termFilter("UsersOnMail",  
> > "ajay.padvi")
> > 
> > And in SearchRequestBuilder I want to give the result of qb1 AND qb2  
> > to FilterBuilder fb.Here I am able to provide single query either qb1  
> > or qb2;  
> > SearchRequestBuilder request =  
> > client.prepareSearch(index).setFilter(fb).setTypes(mapping).setQuery(qb1);
> > 
> > But i want this filter to be applied to results of both qb1 and qb2.  
> > Please Help me with this.
> > 
> > Thanks;

---

<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:51am UTC](https://discuss.elastic.co/t/to-provide-the-result-of-two-queries-to-filter/5665/4 "2017-07-06T03:51:03Z")

</div>


