Elasticsearch 6 Python Client
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
-
elasticsearch.exceptions.ConnectionError: Connection refused
cause The Elasticsearch server is not running, is configured on a different host/port, or a firewall is blocking the connection to the specified host and port (default 9200).fixEnsure your Elasticsearch 6.x instance is running and accessible. Verify the host and port in your `Elasticsearch` client constructor (e.g., `Elasticsearch('http://localhost:9200')`). Check firewall rules to ensure port 9200 (and 9300 for transport) is open. Use `ping` or `telnet` to confirm network connectivity. -
TypeError: index() got an unexpected keyword argument 'doc_type'
cause This error typically occurs when attempting to use a newer version of the `elasticsearch` Python client (e.g., `elasticsearch>=7.0.0`) with code written for Elasticsearch 6.x. The `doc_type` parameter was fully removed in Elasticsearch 7.x+ clients, as Elasticsearch 7+ no longer supports multiple document types per index.fixIf you are using `elasticsearch6`, ensure you are actually running an Elasticsearch 6.x server. If you are trying to upgrade to a newer Elasticsearch server (7.x+), you must also upgrade your client to `elasticsearch>=7.0.0` and remove the `doc_type` parameter from your `index`, `get`, and `search` calls, using the typeless endpoints (e.g., `_doc`). -
SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:XXXX)
cause The Python client is attempting to connect to an Elasticsearch instance over HTTPS with an untrusted or improperly configured SSL certificate. By default, `elasticsearch-py` will attempt to verify SSL certificates.fixIf connecting to an Elastic Cloud instance, ensure you are using a `cloud_id`. For self-managed clusters with TLS, provide the path to your CA certificate bundle via the `ca_certs` parameter in the `Elasticsearch` constructor (e.g., `ca_certs='/path/to/http_ca.crt'`). Alternatively, you can disable certificate verification with `verify_certs=False` (use with extreme caution in production).
Warnings
- breaking The `_doc_type` parameter is mandatory for indexing and searching in Elasticsearch 6.x. Migrating to Elasticsearch 7.x or later servers (and their corresponding Python clients) will deprecate and eventually remove the `_doc_type` concept, requiring changes to API calls.
- deprecated Elasticsearch 6.x deprecated the use of the `_version` field and similar metadata fields in certain contexts, which were fully removed in Elasticsearch 7.x. Using `elasticsearch6` client against an Elasticsearch 6.x server may still generate deprecation warnings if these features are utilized.
- gotcha Connecting to Elasticsearch 6.x requires careful attention to security settings. By default, recent Elasticsearch versions have security enabled. If connecting to a secure cluster without proper `http_auth`, `api_key`, or `ca_certs` configuration, `SSLError` or `AuthenticationException` will occur.
Install
-
pip install elasticsearch6
Imports
- Elasticsearch
from elasticsearch6 import Elasticsearch
from elasticsearch import Elasticsearch
Quickstart
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.")