我发现这类问题很难命名,但肯定是个简单的问题,我遗漏了一些基本的东西。
假设我有以下python dict():
import json
dct = dict()
dct['hits'] = dict()
dct['hits']['hits'] = dict()
dct['hits']['hits']['a'] = 'b'
dct['hits']['hits']['b'] = 'c'
dct['aggregations'] = dict()
dct['aggregations']['a'] = 1
dct['aggregations']['b'] = 2
print(json.dumps(dct, indent=2))
{
"hits": {
"hits": {
"a": "b",
"b": "c"
}
},
"aggregations": {
"a": 1,
"b": 2
}
}
这看起来可能很熟悉,因为它是elasticsearch返回结果的结构。
我正在构建一个使用该结果的函数。但有时我想接近
dct['hits']['hits']
有时我想接近
dct['aggregations']
.
自然地,我会使用一个带有变量的函数来建议我要访问哪个字段,如下所示:
def foo(field):
return dct[field]
如果
field='aggregations'
一切都很好。但如果我想让这个领域
['hits']['hits']
?
一种解决方法(但很难看),迭代方法:
def foo(fields=('hits','hits')):
wanted = dct
for field in fields:
wanted = wanted[field]
return wanted
a = foo()
a
Out[47]: {'a': 'b', 'b': 'c'}
a = foo(('aggregations',))
a
Out[51]: {'a': 1, 'b': 2}
我试图修改的实际函数:
def execute_scroll_query(es_client, query, indexes):
try:
response = es_client.search(index=indexes, scroll='2m', size=1000, body=query)
scroll_size = len(response['hits']['hits'])
sid = response['_scroll_id']
while scroll_size > 0:
try:
for hit in response['hits']['hits']:
yield hit
response = es_client.scroll(scroll_id=sid, scroll='2m')
sid = response['_scroll_id']
scroll_size = len(response['hits']['hits'])
except Exception:
print("Unexpected Exception while scrolling")
except Exception:
print("Unexpected Exception while fetching")