Elastic Search quick reference

Sudheer Keshav Bhat · October 17, 2021

Data Engineering   Big Data   Elastic Search

Install

Install dependencies

pip install elasticsearch
pip install elasticsearch_dsl

Import

from elasticsearch import Elasticsearch
from elasticsearch_dsl import Search, Q

Connect

es_client = Elasticsearch([{"host": "es.domain.tld", "port": 9200}])

Query

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.

Use Result

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