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

i m wondering how can we create dynamic query with a loop ,here below there is my code,please take a look at it :slight_smile:
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

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!!!!

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).

We are not all guys here luckily.

thank you David

i m sorry i didn't mean what you thought when i putthank you guys!!!!,anyway

when i ve got time i will publish my solution .

we stay in touch ,
idris

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