elasticsearch_dsl Memorandum of Understanding

environment

Python3.8.2 elasticsearch-dsl 7.1

Elasticsearch side was troublesome, so only check the query on 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'}}
}}

I want to add Aggs Array nesting

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

Recommended Posts

elasticsearch_dsl Memorandum of Understanding
A memorandum of understanding about django's QueryDict
Memorandum of Understanding when migrating with GORM
Memorandum of fastText (editing)
memorandum of vi command
Analysis of measurement data ①-Memorandum of understanding for scipy fitting-
Memorandum of understanding for environment construction of AutoML library PyCaret
Intuitive understanding of Jensen's inequality
A memorandum of kernel compilation
Complete understanding of numpy.pad functions
A small memorandum of openpyxl
Full understanding of Python debugging
A memorandum of using eigen3
A memorandum of understanding for the Python package management tool ez_setup
Memorandum on Memoization of recursive series
Memorandum of saving and loading model
[Python] A memorandum of beautiful soup4
A memorandum of files under conf.d
Understanding memo of collective intelligence programming
[Python] Understanding the potential_field_planning of Python Robotics
Memorandum of beginners Python "isdigit" movement
A memorandum of closure survey contents
Memorandum of understanding when Python is run on EC2 with Apache
[Understanding in 3 minutes] The beginning of Linux
A complete understanding of Python's asynchronous programming
A rough understanding of python-fire and a memo
A memorandum of using Python's input function
A memorandum of speed of arbitrary degree diagonalization
A complete understanding of Python's object-oriented programming
Full understanding of Python threading and multiprocessing
A memorandum of python string deletion process
A memorandum of trouble when formatting data