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

**URL:** <https://discuss.elastic.co/t/how-to-use-a-loop-to-keep-adding-terms-to-a-boolquery-in-python/180933>\
**Category:** Elasticsearch\
**Created:** [May 14, 2019, 7:07am UTC](https://discuss.elastic.co/t/how-to-use-a-loop-to-keep-adding-terms-to-a-boolquery-in-python/180933 "2019-05-14T07:07:21Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![idris](https://avatars.discourse-cdn.com/v4/letter/i/13edae/32.png) [@idris](https://discuss.elastic.co/u/idris)\
**Post date:** [May 14, 2019, 7:07am UTC](https://discuss.elastic.co/t/how-to-use-a-loop-to-keep-adding-terms-to-a-boolquery-in-python/180933/1 "2019-05-14T07:07:21Z")

</div>

i m wondering how can we create dynamic query with a loop ,here below there is my code,please take a look at it 🙂  
indent preformatted text by 4 spaces

```
import requests
import csv
from addict import Dict
csv_file = 'names of parties.csv

body = Dict()
with open(csv_file, encoding="utf8") as csvfile:

spamreader = csv.reader(csvfile, delimiter=',', quotechar='|')
print (spamreader)
for i in range (int(len(data))):
         body.query.bool.should.match.description=data[i] 
         print(body.to_dict())

```

addict is a python Library to create a json object see this link [https://github.com/mewwts/addict](https://github.com/mewwts/addict)

my query is based on this python function `body.query.bool.should.match.description`,the main objective is to define a bool query using this structure (should--\>OR).

the output from this code is:  
{'query': {'bool': {'should': {'match': {'description': ['Parti libéral-radical']}}}}}  
{'query': {'bool': {'should': {'match': {'description': ['Union Démocratique du Centre']}}}}}  
{'query': {'bool': {'should': {'match': {'description': ['Parti socialiste suisse']}}}}}  
{'query': {'bool': {'should': {'match': {'description': [' Vert libéraux']}}}}}  
{'query': {'bool': {'should': {'match': {'description': [' Les Verts ']}}}}}  
{'query': {'bool': {'should': {'match': {'description': [' Parti bourgeois démocratique']}}}}}  
{'query': {'bool': {'should': {'match': {'description': [' Parti évangélique suisse']}}}}}  
{'query': {'bool': {'should': {'match': {'description': [' Parti démocrate-chrétien ']}}}}}  
{'query': {'bool': {'should': {'match': {'description': [' Parti libéral suisse']}}}}}  
{'query': {'bool': {'should': {'match': {'description': [' Mouvement citoyens genevois']}}}}}  
{'query': {'bool': {'should': {'match': {'description': [' Parti suisse du travail']}}}}}  
{'query': {'bool': {'should': {'match': {'description': [' Parti Chrétien Social du Canton dObwald ']}}}}}  
{'query': {'bool': {'should': {'match': {'description': [' Ligue des Tessinois']}}}}}  
{'query': {'bool': {'should': {'match': {'description': [' Parti suisse du travail']}}}}}  
{'query': {'bool': {'should': {'match': {'description': [' Parti socialiste ']}}}}}

there is a probleme here because i want to add each value 'Parti libéral-radical',Union Démocratique du Centre ..etc in the same query .

thank you guys!!!!

---

<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:** [May 14, 2019, 7:31am UTC](https://discuss.elastic.co/t/how-to-use-a-loop-to-keep-adding-terms-to-a-boolquery-in-python/180933/2 "2019-05-14T07:31:34Z")

</div>

That's a Python question IMO, not an elasticsearch one. If you don't get an answer here, may be ask on a Python forum. (I can't really help as I'm not a Python dev).

> [@idris](#):
>
> thank you guys!!!!

We are not all guys here luckily.

---

<div class="post-metadata">

**Author:** ![idris](https://avatars.discourse-cdn.com/v4/letter/i/13edae/32.png) [@idris](https://discuss.elastic.co/u/idris)\
**Post date:** [May 21, 2019, 6:43am UTC](https://discuss.elastic.co/t/how-to-use-a-loop-to-keep-adding-terms-to-a-boolquery-in-python/180933/3 "2019-05-21T06:43:32Z")

</div>

thank you David

i m sorry i didn't mean what you thought when i put`thank you guys!!!!`,anyway

when i ve got time i will publish my solution .

we stay in touch ,  
idris

---

<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:** [June 18, 2019, 6:43am UTC](https://discuss.elastic.co/t/how-to-use-a-loop-to-keep-adding-terms-to-a-boolquery-in-python/180933/4 "2019-06-18T06:43:34Z")

</div>

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