Elastic search#
Elasticsearch is a distributed search and analitycs engine. You can add documents, and perform analitycs over them.
For more details check:
Try Elasticsearch and Kibana: for guide on local deployment using docker or k8s.
Elasticsearch labs: for projects that use Elasticsearch and notebooks with code snippets.
Connection#
Elasticsearch is designed to be run as a separate service. For local installation, check out try Elasticsesarch and Kibana.
Note: don’t forget to set: ELASTICSEARCH_USER=elastic
and ELASTICSEARCH_PASSWORD=elastic
environment variables before starting the service.
An instance of the Elasticsearch
class represents a connection Elasticsearch.
The following cell shows the connection to elasticsearch.
from elasticsearch import Elasticsearch
es = Elasticsearch(
hosts=["http://localhost:9200"],
basic_auth=("elastic", "elastic")
)
es
<Elasticsearch(['http://localhost:9200'])>
Use the info
method to unsure the connection is established correctly. The following cell shows possible outputs of the info
method.
from pprint import pprint
pprint(dict(es.info()))
{'cluster_name': 'docker-cluster',
'cluster_uuid': 'BDjLMAgzRxa9TP634NQHew',
'name': '5a0053e0a328',
'tagline': 'You Know, for Search',
'version': {'build_date': '2025-08-24T22:05:04.526302670Z',
'build_flavor': 'default',
'build_hash': '0c781091a2f57de895a73a1391ff8426c0153c8d',
'build_snapshot': False,
'build_type': 'docker',
'lucene_version': '10.2.2',
'minimum_index_compatibility_version': '8.0.0',
'minimum_wire_compatibility_version': '8.19.0',
'number': '9.1.3'}}
Index#
In the context of Elasticsearch, an index is a collection of similar documents. Analog in relational databases is “table”.
Action |
Python Method |
Description |
---|---|---|
Create an Index |
|
Creates a new index. You can pass settings and mappings as keyword arguments to define the index structure. |
Check for an Index |
|
Returns |
Get Index Information |
|
Retrieves detailed information about an index, including its settings and mappings. |
Delete an Index |
|
Permanently deletes an index and all of its data. |
Consider the basic operations of indices.
The following cell creates the “book_index”. mappings
specify the attributes index contains and their types.
mappings = {
"properties": {
"prop1": {"type": "text"},
"prop2": {"type": "keyword"}
}
}
es.indices.create(index="book_index", mappings=mappings)
ObjectApiResponse({'acknowledged': True, 'shards_acknowledged': True, 'index': 'book_index'})
Now, the method es.indices.exists
returns True
as index has been created.
es.indices.exists(index="book_index")
HeadApiResponse(True)
The information provided by the get
method is represented above.
pprint(dict(es.indices.get(index="book_index")))
{'book_index': {'aliases': {},
'mappings': {'properties': {'prop1': {'type': 'text'},
'prop2': {'type': 'keyword'}}},
'settings': {'index': {'creation_date': '1756809943562',
'number_of_replicas': '1',
'number_of_shards': '1',
'provided_name': 'book_index',
'routing': {'allocation': {'include': {'_tier_preference': 'data_content'}}},
'uuid': 'G9uweKCnSFyU_aHWSsI8Zw',
'version': {'created': '9033000'}}}}}
As expected, the exists
method returns False
after the index is deleted.
es.indices.delete(index="book_index")
es.indices.exists(index="book_index")
HeadApiResponse(False)