I have an elasticsearch body dictionary stored in the variable body:
body = {"fields": ["dates","c_u", "tawgs.db_id"], "query": { "filtered": { "query": { "match": { "dates": "2019-07-26" } }, "filter": { "terms": { "tawgs.db_id": products } } } } })
My issue is that in the "match" field, I need to increment the
{ "dates": "2019-07-26" }
part by day based on a date lookback that is user inputed.
I developed the date lookback variable to output a list of dates going back a # of days based on the # of days_back the user inputs.
I am just very lost on integrating this list of dates into the body dictionary individually to be called on multiple searches in elasticsearch.
My code to develop the list of dates is below:
# dates desired outputted in a list
base = dt.date.today()
days_back = 5 # USER INPUT
date_list = [base - dt.timedelta(days=x) for x in reversed(range(days_back + 1))]
dates = ['{}-{}-{}'.format(y,m,d) for y, m, d in map(lambda x: str(x).split('-'), date_list)]
And it outputs the list: ['2019-07-21', '2019-07-22', '2019-07-23', '2019-07-24', '2019-07-25','2019-07-26']
So what I need is multiple body dictionaries to be outputted like:
body = {"fields": ["dates","c_u", "tawgs.db_id"], "query": { "filtered": { "query": { "match": { "dates": "2019-07-26" } }, "filter": { "terms": { "tawgs.db_id": products } } } } })
body = {"fields": ["dates","c_u", "tawgs.db_id"], "query": { "filtered": { "query": { "match": { "dates": "2019-07-25" } }, "filter": { "terms": { "tawgs.db_id": products } } } } })
body = {"fields": ["dates","c_u", "tawgs.db_id"], "query": { "filtered": { "query": { "match": { "dates": "2019-07-24" } }, "filter": { "terms": { "tawgs.db_id": products } } } } })
body = {"fields": ["dates","c_u", "tawgs.db_id"], "query": { "filtered": { "query": { "match": { "dates": "2019-07-23" } }, "filter": { "terms": { "tawgs.db_id": products } } } } })
etc. with only the dates field changing.
Please help! I haven't been successful with inserting a for loop. Thanks in advance!
Create a base dictionary without that explicit dates
key/value pair, then assign it the date you want in the loop for that query:
import datetime as dt
import copy
base = dt.date.today()
days_back = 5 # USER INPUT
date_list = [base - dt.timedelta(days=x) for x in reversed(range(days_back + 1))]
base_body = {"fields": ["dates","c_u", "tawgs.db_id"], "query": { "filtered": { "query": { "match": {} }, "filter": { "terms": { "tawgs.db_id": [] } } } }}
for date in date_list:
body = copy.deepcopy(base_body)
body['query']['filtered']['query']['match']['dates'] = date.strftime("%Y-%m-%d")
print(body)
Result:
{'fields': ['dates', 'c_u', 'tawgs.db_id'], 'query': {'filtered': {'query': {'match': {'dates': '2019/07/21'}}, 'filter': {'terms': {'tawgs.db_id': []}}}}}
{'fields': ['dates', 'c_u', 'tawgs.db_id'], 'query': {'filtered': {'query': {'match': {'dates': '2019/07/22'}}, 'filter': {'terms': {'tawgs.db_id': []}}}}}
{'fields': ['dates', 'c_u', 'tawgs.db_id'], 'query': {'filtered': {'query': {'match': {'dates': '2019/07/23'}}, 'filter': {'terms': {'tawgs.db_id': []}}}}}
{'fields': ['dates', 'c_u', 'tawgs.db_id'], 'query': {'filtered': {'query': {'match': {'dates': '2019/07/24'}}, 'filter': {'terms': {'tawgs.db_id': []}}}}}
{'fields': ['dates', 'c_u', 'tawgs.db_id'], 'query': {'filtered': {'query': {'match': {'dates': '2019/07/25'}}, 'filter': {'terms': {'tawgs.db_id': []}}}}}
{'fields': ['dates', 'c_u', 'tawgs.db_id'], 'query': {'filtered': {'query': {'match': {'dates': '2019/07/26'}}, 'filter': {'terms': {'tawgs.db_id': []}}}}}