環境
Python3.8.2
elasticsearch-dsl 7.1
Elasticsearch側は面倒だったので、Python上でクエリのみ確認。
A, Q
test = A("terms", field="hoge")
print(test.to_dict())
print(type(test))
test2 = Q("terms", field="huge")
print(test2.to_dict())
print(type(test2))
{'terms': {'field': 'hoge'}}
<class 'elasticsearch_dsl.aggs.Terms'>
{'terms': {'field': 'huge'}}
<class 'elasticsearch_dsl.query.Terms'>
Agg Array
array_test = [A("terms", field="key_" +str(i)) for i in range(5)]
search = Search()
for i in range(5):
search.aggs.bucket("array_" + str(i), array_test[i])
print(search.to_dict())
{'aggs': {
'array_0': {'terms': {'field': 'key_0'}},
'array_1': {'terms': {'field': 'key_1'}},
'array_2': {'terms': {'field': 'key_2'}},
'array_3': {'terms': {'field': 'key_3'}},
'array_4': {'terms': {'field': 'key_4'}}
}}
Aggs Array ネストをつけたい
array_test = [A("terms", field="key_" +str(i)) for i in range(5)]
search = Search()
X = search.aggs.bucket("array_0", array_test[0])
for i in range(1, 5):
X = X.bucket("array_" + str(i), array_test[i])
print(search.to_dict())
{'aggs': {'array_0': {
'terms': {'field': 'key_0'},
'aggs': {'array_1': {
'terms': {'field': 'key_1'},
'aggs': {'array_2': {
'terms': {'field': 'key_2'},
'aggs': {'array_3': {
'terms': {'field': 'key_3'},
'aggs': {'array_4': {
'terms': {'field': 'key_4'}
}}
}}
}}
}}
}}}