0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

elasticsearch_dsl 覚書

Posted at

環境

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'}
        }}
      }}
    }}
  }}
}}}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?