# Combining multiple text queries

**URL:** <https://discuss.elastic.co/t/combining-multiple-text-queries/7666>\
**Category:** Elasticsearch\
**Created:** [May 11, 2012, 11:49pm UTC](https://discuss.elastic.co/t/combining-multiple-text-queries/7666 "2012-05-11T23:49:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![coys](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@coys](https://discuss.elastic.co/u/coys)\
**Post date:** [May 11, 2012, 11:49pm UTC](https://discuss.elastic.co/t/combining-multiple-text-queries/7666/1 "2012-05-11T23:49:21Z")

</div>

Hi, I have two different text queries(more specifically of type  
phrase\_prefix) and each of them individually return what I'm expecting  
but I can't figure out how to combine the two..more specifically AND  
or OR them. I would also later want to perform this operation on more  
than 2 queries. Can you please give me an example of how I should do  
this? Also, any links to tutorials on advanced text queries would be  
very helpful! Thanks a lot!

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [May 14, 2012, 1:15pm UTC](https://discuss.elastic.co/t/combining-multiple-text-queries/7666/2 "2012-05-14T13:15:34Z")

</div>

Take a look at the Boolean  
Query [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)  
You can use the "must" clause to "AND" queries and the "should" clause to  
"OR" them. If you still have a problem with formulating the query, please  
post an example of your query that doesn't work so we would understand  
better what you have tried to do.

On Friday, May 11, 2012 7:49:21 PM UTC-4, coys wrote:

> Hi, I have two different text queries(more specifically of type  
> phrase\_prefix) and each of them individually return what I'm expecting  
> but I can't figure out how to combine the two..more specifically AND  
> or OR them. I would also later want to perform this operation on more  
> than 2 queries. Can you please give me an example of how I should do  
> this? Also, any links to tutorials on advanced text queries would be  
> very helpful! Thanks a lot!

---

<div class="post-metadata">

**Author:** ![coys](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@coys](https://discuss.elastic.co/u/coys)\
**Post date:** [May 15, 2012, 5:53pm UTC](https://discuss.elastic.co/t/combining-multiple-text-queries/7666/3 "2012-05-15T17:53:35Z")

</div>

On May 14, 7:15 am, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:

> Take a look at the Boolean  
> Queryhttp://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html  
> You can use the "must" clause to "AND" queries and the "should" clause to  
> "OR" them. If you still have a problem with formulating the query, please  
> post an example of your query that doesn't work so we would understand  
> better what you have tried to do.
> 
> On Friday, May 11, 2012 7:49:21 PM UTC-4, coys wrote:
> 
> > Hi, I have two different text queries(more specifically of type  
> > phrase\_prefix) and each of them individually return what I'm expecting  
> > but I can't figure out how to combine the two..more specifically AND  
> > or OR them. I would also later want to perform this operation on more  
> > than 2 queries. Can you please give me an example of how I should do  
> > this? Also, any links to tutorials on advanced text queries would be  
> > very helpful! Thanks a lot!- Hide quoted text -
> 
> - Show quoted text -

---

<div class="post-metadata">

**Author:** ![coys](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@coys](https://discuss.elastic.co/u/coys)\
**Post date:** [May 15, 2012, 5:54pm UTC](https://discuss.elastic.co/t/combining-multiple-text-queries/7666/4 "2012-05-15T17:54:43Z")

</div>

Hello, Thanks for the reply.  
Basically, my two queries are :  
string query1 = new QueryBuilder()  
.Query(qry =\> qry  
.Text(text =\> text  
.Field("field1")  
.Query(field1TB.Text)  
.Type(TextQueryType.phrase\_prefix)  
)  
).Build();  
and string query2 = new QueryBuilder()  
.Query(qry =\> qry  
.Text(text =\> text  
.Field("field2")  
.Query(field2TB.Text)  
.Type(TextQueryType.phrase\_prefix)  
)  
).Build();  
I'm using YegoRoff's [PlainElastic.Net](http://PlainElastic.Net) from github. These two queries  
work for me induvidually, but I now want to combine them - I tried,  
string query1 = new QueryBuilder()  
.Query(qry =\> qry  
.Text(text =\> text  
.Field("field1")  
.Query(field1TB.Text)  
.Type(TextQueryType.phrase\_prefix)  
.Field("field2")  
.Query(field2TB.Text)  
.Type(TextQueryType.phrase\_prefix)  
.Operator(Operator.AND) // could also be  
operator.OR and would like to add multiple fields later on  
)  
).Build();  
but this did not work for me. Also, I'm unable to figure out how to  
use a must query, can I pass a text query in there instead of a term?  
Thanks!

On May 15, 11:53 am, coys [gautam...@gmail.com](mailto:gautam...@gmail.com) wrote:

> On May 14, 7:15 am, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> 
> > Take a look at the Boolean  
> > Queryhttp://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html  
> > You can use the "must" clause to "AND" queries and the "should" clause to  
> > "OR" them. If you still have a problem with formulating the query, please  
> > post an example of your query that doesn't work so we would understand  
> > better what you have tried to do.
> 
> > On Friday, May 11, 2012 7:49:21 PM UTC-4, coys wrote:
> 
> > > Hi, I have two different text queries(more specifically of type  
> > > phrase\_prefix) and each of them individually return what I'm expecting  
> > > but I can't figure out how to combine the two..more specifically AND  
> > > or OR them. I would also later want to perform this operation on more  
> > > than 2 queries. Can you please give me an example of how I should do  
> > > this? Also, any links to tutorials on advanced text queries would be  
> > > very helpful! Thanks a lot!- Hide quoted text -
> 
> > - Show quoted text -- Hide quoted text -
> 
> - Show quoted text -

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [May 15, 2012, 6:06pm UTC](https://discuss.elastic.co/t/combining-multiple-text-queries/7666/5 "2012-05-15T18:06:50Z")

</div>

Unfortunately, I don't have any experience with YegoRoff's  
[PlainElastic.Net](http://PlainElastic.Net), so I wouldn't be able to help you here. Have you seen how  
Bool Queries are used in tests  
[https://github.com/Yegoroff/PlainElastic.Net/tree/master/src/PlainElastic.Net.Tests/Builders/Queries/Bool](https://github.com/Yegoroff/PlainElastic.Net/tree/master/src/PlainElastic.Net.Tests/Builders/Queries/Bool)  
?

On Tuesday, May 15, 2012 1:54:43 PM UTC-4, coys wrote:

> Hello, Thanks for the reply.  
> Basically, my two queries are :  
> string query1 = new QueryBuilder()  
> .Query(qry =\> qry  
> .Text(text =\> text  
> .Field("field1")  
> .Query(field1TB.Text)  
> .Type(TextQueryType.phrase\_prefix)  
> )  
> ).Build();  
> and string query2 = new QueryBuilder()  
> .Query(qry =\> qry  
> .Text(text =\> text  
> .Field("field2")  
> .Query(field2TB.Text)  
> .Type(TextQueryType.phrase\_prefix)  
> )  
> ).Build();  
> I'm using YegoRoff's [PlainElastic.Net](http://PlainElastic.Net) from github. These two queries  
> work for me induvidually, but I now want to combine them - I tried,  
> string query1 = new QueryBuilder()  
> .Query(qry =\> qry  
> .Text(text =\> text  
> .Field("field1")  
> .Query(field1TB.Text)  
> .Type(TextQueryType.phrase\_prefix)  
> .Field("field2")  
> .Query(field2TB.Text)  
> .Type(TextQueryType.phrase\_prefix)  
> .Operator(Operator.AND) // could also be  
> operator.OR and would like to add multiple fields later on  
> )  
> ).Build();  
> but this did not work for me. Also, I'm unable to figure out how to  
> use a must query, can I pass a text query in there instead of a term?  
> Thanks!
> 
> On May 15, 11:53 am, coys [gautam...@gmail.com](mailto:gautam...@gmail.com) wrote:
> 
> > On May 14, 7:15 am, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> > 
> > > Take a look at the Boolean  
> > > Queryhttp://  
> > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)  
> > > You can use the "must" clause to "AND" queries and the "should" clause  
> > > to  
> > > "OR" them. If you still have a problem with formulating the query,  
> > > please  
> > > post an example of your query that doesn't work so we would understand  
> > > better what you have tried to do.
> > 
> > > On Friday, May 11, 2012 7:49:21 PM UTC-4, coys wrote:
> > 
> > > > Hi, I have two different text queries(more specifically of type  
> > > > phrase\_prefix) and each of them individually return what I'm  
> > > > expecting  
> > > > but I can't figure out how to combine the two..more specifically AND  
> > > > or OR them. I would also later want to perform this operation on  
> > > > more  
> > > > than 2 queries. Can you please give me an example of how I should do  
> > > > this? Also, any links to tutorials on advanced text queries would be  
> > > > very helpful! Thanks a lot!- Hide quoted text -
> > 
> > > - Show quoted text -- Hide quoted text -
> > 
> > - Show quoted text -

---

<div class="post-metadata">

**Author:** ![coys](https://avatars.discourse-cdn.com/v4/letter/c/eb8c5e/32.png) [@coys](https://discuss.elastic.co/u/coys)\
**Post date:** [May 16, 2012, 2:58pm UTC](https://discuss.elastic.co/t/combining-multiple-text-queries/7666/6 "2012-05-16T14:58:41Z")

</div>

Thanks a lot Igor for pointing me in the right direction! Just thought  
I'd share the solution here,  
query = new QueryBuilder()  
.Query(q =\> q  
.Bool(b =\> b  
.Must(m =\> m  
.Text(text =\> text  
.Field("field1")  
.Query(field1.Text)  
.Type(TextQueryType.phrase\_prefix)  
)  
.Text(text =\> text  
.Field("field2")  
.Query(field2.Text)  
.Type(TextQueryType.phrase\_prefix)  
)  
)  
)).Build();  
return query;

On May 15, 12:06 pm, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:

> Unfortunately, I don't have any experience with YegoRoff's  
> [PlainElastic.Net](http://PlainElastic.Net), so I wouldn't be able to help you here. Have you seen how  
> Bool Queries are used in testshttps://github.com/Yegoroff/PlainElastic.Net/tree/master/src/PlainEla...  
> ?
> 
> On Tuesday, May 15, 2012 1:54:43 PM UTC-4, coys wrote:
> 
> > Hello, Thanks for the reply.  
> > Basically, my two queries are :  
> > string query1 = new QueryBuilder()  
> > .Query(qry =\> qry  
> > .Text(text =\> text  
> > .Field("field1")  
> > .Query(field1TB.Text)  
> > .Type(TextQueryType.phrase\_prefix)  
> > )  
> > ).Build();  
> > and string query2 = new QueryBuilder()  
> > .Query(qry =\> qry  
> > .Text(text =\> text  
> > .Field("field2")  
> > .Query(field2TB.Text)  
> > .Type(TextQueryType.phrase\_prefix)  
> > )  
> > ).Build();  
> > I'm using YegoRoff's [PlainElastic.Net](http://PlainElastic.Net) from github. These two queries  
> > work for me induvidually, but I now want to combine them - I tried,  
> > string query1 = new QueryBuilder()  
> > .Query(qry =\> qry  
> > .Text(text =\> text  
> > .Field("field1")  
> > .Query(field1TB.Text)  
> > .Type(TextQueryType.phrase\_prefix)  
> > .Field("field2")  
> > .Query(field2TB.Text)  
> > .Type(TextQueryType.phrase\_prefix)  
> > .Operator(Operator.AND) // could also be  
> > operator.OR and would like to add multiple fields later on  
> > )  
> > ).Build();  
> > but this did not work for me. Also, I'm unable to figure out how to  
> > use a must query, can I pass a text query in there instead of a term?  
> > Thanks!
> 
> > On May 15, 11:53 am, coys [gautam...@gmail.com](mailto:gautam...@gmail.com) wrote:
> > 
> > > On May 14, 7:15 am, Igor Motov [imo...@gmail.com](mailto:imo...@gmail.com) wrote:
> 
> > > > Take a look at the Boolean  
> > > > Queryhttp://  
> > > > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-query.html)  
> > > > You can use the "must" clause to "AND" queries and the "should" clause  
> > > > to  
> > > > "OR" them. If you still have a problem with formulating the query,  
> > > > please  
> > > > post an example of your query that doesn't work so we would understand  
> > > > better what you have tried to do.
> 
> > > > On Friday, May 11, 2012 7:49:21 PM UTC-4, coys wrote:
> 
> > > > > Hi, I have two different text queries(more specifically of type  
> > > > > phrase\_prefix) and each of them individually return what I'm  
> > > > > expecting  
> > > > > but I can't figure out how to combine the two..more specifically AND  
> > > > > or OR them. I would also later want to perform this operation on  
> > > > > more  
> > > > > than 2 queries. Can you please give me an example of how I should do  
> > > > > this? Also, any links to tutorials on advanced text queries would be  
> > > > > very helpful! Thanks a lot!- Hide quoted text -
> 
> > > > - Show quoted text -- Hide quoted text -
> 
> > > - Show quoted text -- Hide quoted text -
> 
> - Show quoted text -

---

<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:28am UTC](https://discuss.elastic.co/t/combining-multiple-text-queries/7666/7 "2017-07-06T03:28:34Z")

</div>


