Connecting

edit

This page contains the information you need to connect the Client with Elasticsearch.

Authentication

edit

This section contains code snippets to show you how to connect to various Elasticsearch providers.

Elastic Cloud

edit

Cloud ID is an easy way to configure your client to work with your Elastic Cloud deployment. Combine the cloud_id with either http_auth or api_key to authenticate with your Elastic Cloud deployment.

Using cloud_id enables TLS verification and HTTP compression by default and sets the port to 443 unless otherwise overwritten via the port parameter or the port value encoded within cloud_id. Using Cloud ID also disables sniffing.

from elasticsearch import Elasticsearch

es = Elasticsearch(
    cloud_id=”cluster-1:dXMa5Fx...”
)

HTTP Authentication

edit

HTTP authentication uses the http_auth parameter by passing in a username and password within a tuple:

from elasticsearch import Elasticsearch

es = Elasticsearch(
    http_auth=(“username”, “password”)
)

ApiKey authentication

edit

You can configure the client to use Elasticsearch’s API Key for connecting to your cluster.

from elasticsearch import Elasticsearch

es = Elasticsearch(
    api_key=(“api_key_id”, “api_key_secret”)
)

Enabling the Compatibility Mode

edit

The Elasticsearch server version 8.0 is introducing a new compatibility mode that allows you a smoother upgrade experience from 7 to 8. In a nutshell, you can use the latest 7.x Python Elasticsearch Elasticsearch client with an 8.x Elasticsearch server, giving more room to coordinate the upgrade of your codebase to the next major version.

If you want to leverage this functionality, please make sure that you are using the latest 7.x Python Elasticsearch client and set the environment variable ELASTIC_CLIENT_APIVERSIONING to true. The client is handling the rest internally. For every 8.0 and beyond Python Elasticsearch client, you’re all set! The compatibility mode is enabled by default.

Using the Client in a Function-as-a-Service Environment

edit

This section illustrates the best practices for leveraging the Elasticsearch client in a Function-as-a-Service (FaaS) environment. The most influential optimization is to initialize the client outside of the function, the global scope. This practice does not only improve performance but also enables background functionality as – for example – sniffing. The following examples provide a skeleton for the best practices.

GCP Cloud Functions

edit
from elasticsearch import Elasticsearch

client = Elasticsearch(
    ... # Client initialization
)

def main(request):
    ... # Use the client

AWS Lambda

edit
from elasticsearch import Elasticsearch

client = Elasticsearch(
    ... # Client initialization
)

def main(event, context):
    ... # Use the client

Azure Functions

edit
import azure.functions as func
from elasticsearch import Elasticsearch

client = Elasticsearch(
    ... # Client initialization
)

def main(request: func.HttpRequest) -> func.HttpResponse:
    ... # Use the client

The async client shouldn’t be used within Function-as-a-Service as a new event loop must be started for each invocation. Instead the synchronous Elasticsearch client is recommended.

Resources used to assess these recommendations: