Elasticsearch Client Builder

9.0.1 · active · verified Thu Apr 16

es-client is an Elasticsearch Client builder for Python, complete with advanced schema validation. It provides features like master-only detection, Elasticsearch version checking, and robust configuration value validation, including file paths for SSL certificates. The library is currently at version 9.0.1 and maintains an active development cycle with frequent updates and patches.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the es-client Builder with a configuration dictionary, establish a connection to an Elasticsearch cluster, and then retrieve the underlying Elasticsearch client to perform basic operations like checking cluster info and creating an index. Sensitive connection details are sourced from environment variables for security.

import os
from es_client import Builder

# Configure Elasticsearch connection using environment variables for sensitive data
config = {
    'elasticsearch': {
        'client': {
            'hosts': os.environ.get('ES_HOSTS', 'https://localhost:9200').split(','),
            'api_key': os.environ.get('ES_API_KEY', ''),
            'ca_certs': os.environ.get('ES_CA_CERTS', ''), # e.g., '/etc/elasticsearch/certs/ca.crt'
            'request_timeout': 60,
        },
        'other_settings': {
            'master_only': False,
            'username': os.environ.get('ES_USERNAME', ''),
            'password': os.environ.get('ES_PASSWORD', ''),
        }
    },
    'logging': {
        'loglevel': 'INFO',
        'logfile': '', # Path to log file, e.g., '/var/log/es_client.log'
        'logformat': 'default',
    }
}

builder = Builder(configdict=config)
try:
    builder.connect()
    client = builder.client # Get the connected Elasticsearch client instance
    
    # Example: Check cluster info
    info = client.info()
    print("Connected to Elasticsearch cluster:", info['cluster_name'])
    
    # Example: Create an index
    index_name = "my_test_index"
    if not client.indices.exists(index=index_name):
        client.indices.create(index=index_name)
        print(f"Index '{index_name}' created.")
    else:
        print(f"Index '{index_name}' already exists.")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →