Help Center/ Cloud Search Service/ User Guide/ Elasticsearch/ Accessing Elasticsearch Clusters/ Accessing an Elasticsearch Cluster Using Python
Updated on 2026-01-09 GMT+08:00

Accessing an Elasticsearch Cluster Using Python

When using CSS's Elasticsearch clusters for data query and management, developers traditionally rely on programming languages. However, directly working with Elasticsearch's HTTP APIs often involves complex and error-prone coding. To simplify development, CSS Elasticsearch supports the official Elasticsearch Python client. The client library (elasticsearch-py) provides comprehensive encapsulation of Elasticsearch APIs, enabling developers to perform index management, CRUD (Create, Read, Update, Delete) operations, and document searches through native Python APIs. For further information, see Elasticsearch Python Client.

Prerequisites

  • The target Elasticsearch cluster is available.
  • The server that runs the Python code can communicate with the Elasticsearch cluster.
  • Depending on the network configuration method used, obtain the cluster access address. For details, see Network Configuration.
  • Python 3 has been installed on the server.

Installing the Python Client Library

Install the Python client library on the server that runs the Python code. To ensure better compatibility, use a Python client library that has the same version as the target Elasticsearch cluster.

Replace 7.10 with the actual Python client version.

pip install Elasticsearch==7.10

Accessing a Cluster

The sample code varies depending on the security mode settings of the target Elasticsearch cluster. Select the right reference document based on your service scenario.

Connecting to a Non-Security Mode Cluster with the Elasticsearch Python Client

Use the Elasticsearch Python client to connect to an Elasticsearch cluster for which the security mode is disabled, and query whether the test index exists.

The following is an example of the code for creating a client instance using the cluster connection information:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from elasticsearch import Elasticsearch

class ElasticFactory(object):

    def __init__(self, host: list, port: str, username: str, password: str):
        self.port = port
        self.host = host
        self.username = username
        self.password = password

    def create(self) -> Elasticsearch:
        addrs = []
        for host in self.host:
            addr = {'host': host, 'port': self.port}
            addrs.append(addr)

        if self.username and self.password:
            elasticsearch = Elasticsearch(addrs, http_auth=(self.username, self.password))
        else:
            elasticsearch = Elasticsearch(addrs)
        return elasticsearch

es = ElasticFactory(["{cluster address}"], "9200", None, None).create()
print(es.indices.exists(index='test'))

This piece of code checks whether the test index exists in the cluster. If true (the index exists) or false (the index does not exist) is returned, it indicates that the cluster is connected.

Connecting to a Security-Mode+HTTP Cluster with the Elasticsearch Python Client

Use the Elasticsearch Python client to connect to a security-mode Elasticsearch cluster that uses HTTP, and query whether the test index exists.

The following is an example of the code for creating a client instance using the cluster connection information:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from elasticsearch import Elasticsearch

class ElasticFactory(object):

    def __init__(self, host: list, port: str, username: str, password: str):
        self.port = port
        self.host = host
        self.username = username
        self.password = password

    def create(self) -> Elasticsearch:
        addrs = []
        for host in self.host:
            addr = {'host': host, 'port': self.port}
            addrs.append(addr)

        if self.username and self.password:
            elasticsearch = Elasticsearch(addrs, http_auth=(self.username, self.password))
        else:
            elasticsearch = Elasticsearch(addrs)
        return elasticsearch

es = ElasticFactory(["xxx.xxx.xxx.xxx"], "9200", "username", "password").create()
print(es.indices.exists(index='test'))
Table 2 Variables

Parameter

Description

host

IP address for accessing the cluster. If there are multiple IP addresses, separate them using a comma (,).

port

Access port of the cluster. Enter 9200.

username

Username for accessing the cluster.

password

Password of the user.

This piece of code checks whether the test index exists in the cluster. If true (the index exists) or false (the index does not exist) is returned, it indicates that the cluster is connected.

Connecting to a Security-Mode+HTTPS Cluster with the Elasticsearch Python Client

Use the Elasticsearch Python client to connect to a security-mode Elasticsearch cluster that uses HTTPS, and query whether the test index exists.

The following is an example of the code for creating a client instance using the cluster connection information:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from elasticsearch import Elasticsearch
import ssl

class ElasticFactory(object):

    def __init__(self, host: list, port: str, username: str, password: str):
        self.port = port
        self.host = host
        self.username = username
        self.password = password

    def create(self) -> Elasticsearch:
        context = ssl._create_unverified_context()

        addrs = []
        for host in self.host:
            addr = {'host': host, 'port': self.port}
            addrs.append(addr)

        if self.username and self.password:
            elasticsearch = Elasticsearch(addrs, http_auth=(self.username, self.password), scheme="https", ssl_context=context)
        else:
            elasticsearch = Elasticsearch(addrs)
        return elasticsearch

es = ElasticFactory(["xxx.xxx.xxx.xxx"], "9200", "username", "password").create()
print(es.indices.exists(index='test'))
Table 3 Variables

Parameter

Description

host

IP address for accessing the cluster. If there are multiple IP addresses, separate them using a comma (,).

port

Access port of the cluster. Enter 9200.

username

Username for accessing the cluster.

password

Password of the user.

This piece of code checks whether the test index exists in the cluster. If true (the index exists) or false (the index does not exist) is returned, it indicates that the cluster is connected.