SQL to Elasticsearch DSL: A Guide with Python Examples
Elasticsearch DSL is a Python library for building Elasticsearch queries. It provides a more Pythonic and readable way to interact with Elasticsearch compared to the raw JSON format. This guide demonstrates how to convert common SQL queries into their Elasticsearch DSL equivalents using Python code.
Basic Queries
- SELECT * FROM my_index
from elasticsearch_dsl import Search
s = Search().index('my_index')
response = s.execute()
- SELECT field1, field2 FROM my_index WHERE field1 = 'value1'
from elasticsearch_dsl import Search, Q
s = Search().index('my_index').source(['field1', 'field2'])
q = Q('match', field1='value1')
s = s.query(q)
response = s.execute()
Filtering
- SELECT * FROM my_index WHERE field1 = 'value1' AND field2 = 'value2'
from elasticsearch_dsl import Search, Q
s = Search().index('my_index')
q = Q('bool', must=[Q('match', field1='value1'), Q('match', field2='value2')])
s = s.query(q)
response = s.execute()
Aggregation
- SELECT COUNT(*) FROM my_index WHERE field1 = 'value1'
from elasticsearch_dsl import Search, Q
s = Search().index('my_index')
q = Q('match', field1='value1')
s = s.query(q)
response = s.execute()
count = response.hits.total
- SELECT field1, AVG(field2) FROM my_index GROUP BY field1
from elasticsearch_dsl import Search, A
s = Search().index('my_index').source(['field1'])
a = A('avg', field='field2')
s.aggs.bucket('by_field1', 'terms', field='field1').metric('avg_field2', a)
response = s.execute()
buckets = response.aggregations.by_field1.buckets
for bucket in buckets:
field1_value = bucket.key
avg_field2_value = bucket.avg_field2.value
This guide provides a starting point for using Elasticsearch DSL with Python. You can find more advanced queries and examples in the official Elasticsearch DSL documentation.
原文地址: https://www.cveoy.top/t/topic/murz 著作权归作者所有。请勿转载和采集!