Elasticsearch 6 Python Client

6.8.2 · maintenance · verified Thu Apr 16

The `elasticsearch6` library is the official low-level Python client for Elasticsearch, specifically designed for compatibility with Elasticsearch 6.x servers. It provides a foundation for interacting with Elasticsearch, focusing on being opinion-free and highly extendable. While the main `elasticsearch` client has progressed to newer major versions (e.g., 9.x), `elasticsearch6` remains available on PyPI to support applications tied to Elasticsearch 6.x. It was last released in March 2021.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to an Elasticsearch 6.x instance, create an index, index a document, retrieve it, perform a basic search, and delete the index. Note the explicit use of `doc_type` which is mandatory for Elasticsearch 6.x.

from datetime import datetime
from elasticsearch import Elasticsearch
import os

# By default, connects to localhost:9200. 
# Ensure an Elasticsearch 6.x instance is running or specify host/port.
# For local testing, you might run Elasticsearch via Docker, e.g.:
# docker run -p 9200:9200 -e "discovery.type=single-node" -e "xpack.security.enabled=false" docker.elastic.co/elasticsearch/elasticsearch:6.8.23

# Configure connection. Use environment variables for production credentials.
# Example for basic auth (if security is enabled on ES 6.x):
# ELASTIC_HOST = os.environ.get('ELASTIC_HOST', 'http://localhost:9200')
# ELASTIC_USER = os.environ.get('ELASTIC_USER', 'elastic')
# ELASTIC_PASSWORD = os.environ.get('ELASTIC_PASSWORD', 'changeme')
# es = Elasticsearch(
#     [ELASTIC_HOST],
#     http_auth=(ELASTIC_USER, ELASTIC_PASSWORD),
#     verify_certs=False,  # Use with caution for self-signed or development certs
# )
es = Elasticsearch('http://localhost:9200') # Connects to a local ES instance without security

if not es.ping():
    raise ValueError("Connection to Elasticsearch failed!")

print("Connected to Elasticsearch!")

index_name = 'my_test_index_6'
doc_type_name = 'my_document_type' # Required in ES 6.x

# Create an index (ignore 400 if it already exists)
es.indices.create(index=index_name, ignore=400)

# Index a document
doc = {
    'author': 'kimchy',
    'text': 'Elasticsearch 6.x is stable.',
    'timestamp': datetime.now()
}
res = es.index(index=index_name, doc_type=doc_type_name, id=1, body=doc)
print(f"Indexed document ID 1: {res['result']}")

# Get a document
res = es.get(index=index_name, doc_type=doc_type_name, id=1)
print(f"Retrieved document: {res['_source']}")

# Search for documents
res = es.search(index=index_name, doc_type=doc_type_name, body={
    "query": {"match": {"author": "kimchy"}}
})

print(f"Found {res['hits']['total']['value']} hits:")
for hit in res['hits']['hits']:
    print(f"  {hit['_source']['author']}: {hit['_source']['text']}")

# Delete the index
es.indices.delete(index=index_name, ignore=[400, 404])
print(f"Index '{index_name}' deleted.")

view raw JSON →