# How to use a loop to keep adding terms to a boolQuery in JAVA

**URL:** <https://discuss.elastic.co/t/how-to-use-a-loop-to-keep-adding-terms-to-a-boolquery-in-java/65114>\
**Category:** Elasticsearch\
**Created:** [November 6, 2016, 2:04am UTC](https://discuss.elastic.co/t/how-to-use-a-loop-to-keep-adding-terms-to-a-boolquery-in-java/65114 "2016-11-06T02:04:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nisamponery](https://avatars.discourse-cdn.com/v4/letter/n/3be4f8/32.png) [@nisamponery](https://discuss.elastic.co/u/nisamponery)\
**Post date:** [November 6, 2016, 2:04am UTC](https://discuss.elastic.co/t/how-to-use-a-loop-to-keep-adding-terms-to-a-boolquery-in-java/65114/1 "2016-11-06T02:04:17Z")

</div>

Hi,  
i am trying to see if whether i can use a for loop to to construct a boolQuery in order to dynamically add as many terms i want

for example i have some Java code like this :

```
          QueryBuilder qb = QueryBuilder.boolQuery()
                                            .should(termQuery("name","joe"))
                                            .should(termQuery("age","j23"))
                                             .should(termQuery("ID","12345")) ;

```

the thing is with this i have to manually add all the terms, so i was wondering if could use a loop to add all the terms and i am not sure exactly about the syntax

Here is what i want to achieve ideally :  
// assume "fields" is an array with a lot of strings  
//assume "fields2: is another array with a lot of strings

```
                                   QueryBuilder qb;
                                  for(int i =0 ; i < fields.length; i++) {
                                             
                                               QueryBuilder.boolQuery()
                                    .should( termQuery( field[i], field2[i] ) ) ;

                                            }

```

// so by doing this i am hoping to append all the term queries dynamically without me hard coding it

I know the syntax for what i am trying to do is wrong but that is what i am trying to achieve and i am not exactly sure on the syntax for doing it

So if you can please Help

---

<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:** [November 6, 2016, 3:16am UTC](https://discuss.elastic.co/t/how-to-use-a-loop-to-keep-adding-terms-to-a-boolquery-in-java/65114/2 "2016-11-06T03:16:48Z")

</div>

Try something like:

```
BoolQueryBuilder qb = QueryBuilders.boolQuery();
for(int i =0 ; i < fields.length; i++) {
     qb.should( termQuery( field[i], field2[i] ) ) ;
}
```

---

<div class="post-metadata">

**Author:** ![nisamponery](https://avatars.discourse-cdn.com/v4/letter/n/3be4f8/32.png) [@nisamponery](https://discuss.elastic.co/u/nisamponery)\
**Post date:** [November 10, 2016, 3:17am UTC](https://discuss.elastic.co/t/how-to-use-a-loop-to-keep-adding-terms-to-a-boolquery-in-java/65114/3 "2016-11-10T03:17:15Z")

</div>

thank you that helped

---

<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:** [December 8, 2016, 3:17am UTC](https://discuss.elastic.co/t/how-to-use-a-loop-to-keep-adding-terms-to-a-boolquery-in-java/65114/4 "2016-12-08T03:17:16Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
