Sudheer Keshav Bhat · October 17, 2021
Install dependencies
pip install elasticsearch
pip install elasticsearch_dsl
from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q
es_client = Elasticsearch([{"host": "es.domain.tld", "port": 9200}])
search = Search(using=es_client, index="employee_index").query(
"bool",
must=[
Q("range", created_date={"gte": "2021-10-01", "lt": "2021-10-18"}),
Q("terms", department=["IT", "HR"]),
Q("match", role="Manager")
]
)
terms
is similar to SQL in
clause.
match
is similar to SQL where column=value
.
range
allows to search for a range of values with arithmetic operators.
Check number of result records.
size = search.count()
Iterate through results
if size:
try:
for row in search.scan:
print(row[col])
print(row.to_dict())
except StopIteration:
pass