# How to construct a boolquery dynamically in java api?

**URL:** <https://discuss.elastic.co/t/how-to-construct-a-boolquery-dynamically-in-java-api/11396>\
**Category:** Elasticsearch\
**Created:** [April 1, 2013, 11:26am UTC](https://discuss.elastic.co/t/how-to-construct-a-boolquery-dynamically-in-java-api/11396 "2013-04-01T11:26:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jingang\_Wang](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Jingang\_Wang](https://discuss.elastic.co/u/Jingang_Wang)\
**Post date:** [April 1, 2013, 11:26am UTC](https://discuss.elastic.co/t/how-to-construct-a-boolquery-dynamically-in-java-api/11396/1 "2013-04-01T11:26:26Z")

</div>

Hi there,

In my program, I generated a list of termQuery dynamically and I want to  
combine them into a boolquery. How could I implement it in java api? Thanks.  
By the way, I want to accomplish the fuction as follows:

QueryBuilder qb = QueryBuilders  
.boolQuery()  
.must(matchPhraseQuery("body", "some text"))  
.should(termQuery1)  
.should(termQuery2)  
.....  
.should(termQueryN);

--  
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:** ![Jingang\_Wang](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Jingang\_Wang](https://discuss.elastic.co/u/Jingang_Wang)\
**Post date:** [April 1, 2013, 11:56am UTC](https://discuss.elastic.co/t/how-to-construct-a-boolquery-dynamically-in-java-api/11396/2 "2013-04-01T11:56:35Z")

</div>

more specifically, I want to accomplish the following lucene code in ES.

BooleanQuery booleanQuery = new BooleanQuery();  
booleanQuery.add(query1, BooleanClause.Occur.SHOULD);  
booleanQuery.add(query2, BooleanClause.Occur.SHOULD);

On Monday, April 1, 2013 7:26:26 PM UTC+8, Jingang Wang wrote:

> Hi there,
> 
> In my program, I generated a list of termQuery dynamically and I want to  
> combine them into a boolquery. How could I implement it in java api? Thanks.  
> By the way, I want to accomplish the fuction as follows:
> 
> QueryBuilder qb = QueryBuilders  
> .boolQuery()  
> .must(matchPhraseQuery("body", "some text"))  
> .should(termQuery1)  
> .should(termQuery2)  
> .....  
> .should(termQueryN);

--  
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:** ![mkleen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mkleen/32/1694_2.png) [@mkleen](https://discuss.elastic.co/u/mkleen)\
**Post date:** [April 1, 2013, 6:13pm UTC](https://discuss.elastic.co/t/how-to-construct-a-boolquery-dynamically-in-java-api/11396/3 "2013-04-01T18:13:23Z")

</div>

Hi Jingang,

In Elasticsearch, queries are built using the QueryBuilder interface, so  
you are on the right track:  
QueryBuilder finalQuery =  
org.elasticsearch.index.query.QueryBuilders.boolQuery().should(query1).should(query2)

Michael

On Monday, April 1, 2013 1:56:35 PM UTC+2, Jingang Wang wrote:

> more specifically, I want to accomplish the following lucene code in ES.
> 
> BooleanQuery booleanQuery = new BooleanQuery();  
> booleanQuery.add(query1, BooleanClause.Occur.SHOULD);  
> booleanQuery.add(query2, BooleanClause.Occur.SHOULD);

--  
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:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 1, 2013, 6:46pm UTC](https://discuss.elastic.co/t/how-to-construct-a-boolquery-dynamically-in-java-api/11396/4 "2013-04-01T18:46:46Z")

</div>

I just add a link to this: [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/java-api/query-dsl-queries/)

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 1 avr. 2013 à 20:13, mkleen [mkleen@gmail.com](mailto:mkleen@gmail.com) a écrit :

> Hi Jingang,
> 
> In Elasticsearch, queries are built using the QueryBuilder interface, so you are on the right track:  
> QueryBuilder finalQuery = org.elasticsearch.index.query.QueryBuilders.boolQuery().should(query1).should(query2)
> 
> Michael
> 
> On Monday, April 1, 2013 1:56:35 PM UTC+2, Jingang Wang wrote:
> 
> > more specifically, I want to accomplish the following lucene code in ES.  
> > BooleanQuery booleanQuery = new BooleanQuery();  
> > booleanQuery.add(query1, BooleanClause.Occur.SHOULD);  
> > booleanQuery.add(query2, BooleanClause.Occur.SHOULD);
> 
> --  
> 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:** ![mkleen](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mkleen/32/1694_2.png) [@mkleen](https://discuss.elastic.co/u/mkleen)\
**Post date:** [April 1, 2013, 7:05pm UTC](https://discuss.elastic.co/t/how-to-construct-a-boolquery-dynamically-in-java-api/11396/5 "2013-04-01T19:05:28Z")

</div>

Hi David,

new documentation is excellent, though not so sure about the new design.  
Maybe i just need to get used to it 🙂

Michael

On Monday, April 1, 2013 8:46:46 PM UTC+2, David Pilato wrote:

> I just add a link to this:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/java-api/query-dsl-queries/)
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--  
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:** ![Jingang\_Wang](https://avatars.discourse-cdn.com/v4/letter/j/ce7236/32.png) [@Jingang\_Wang](https://discuss.elastic.co/u/Jingang_Wang)\
**Post date:** [April 2, 2013, 10:02am UTC](https://discuss.elastic.co/t/how-to-construct-a-boolquery-dynamically-in-java-api/11396/6 "2013-04-02T10:02:45Z")

</div>

Hi Michael,

Thanks for your reply, it's helpful.  
At last, I accomplish the purpose as follows:  
BoolQueryBuilder qb = QueryBuilders  
.boolQuery()  
.must(matchPhraseQuery("body", “text”));

then in every loop:

qb.should(termQuery(termQuery1));

On Tuesday, April 2, 2013 2:13:23 AM UTC+8, mkleen wrote:

> Hi Jingang,
> 
> In Elasticsearch, queries are built using the QueryBuilder interface, so  
> you are on the right track:  
> QueryBuilder finalQuery =  
> org.elasticsearch.index.query.QueryBuilders.boolQuery().should(query1).should(query2)
> 
> Michael
> 
> On Monday, April 1, 2013 1:56:35 PM UTC+2, Jingang Wang wrote:
> 
> > more specifically, I want to accomplish the following lucene code in ES.
> > 
> > BooleanQuery booleanQuery = new BooleanQuery();  
> > booleanQuery.add(query1, BooleanClause.Occur.SHOULD);  
> > booleanQuery.add(query2, BooleanClause.Occur.SHOULD);

--  
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:43am UTC](https://discuss.elastic.co/t/how-to-construct-a-boolquery-dynamically-in-java-api/11396/7 "2017-07-06T02:43:16Z")

</div>


