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