Updated on 2025-09-05 GMT+08:00

Accessing an OpenSearch Cluster with the Python Client

Connect to a CSS OpenSearch cluster with Python to query and manage data.

OpenSearch Python Client (or Elasticsearch Python Client) is the official Python client library provided for interacting with OpenSearch clusters using Python.

Prerequisites

  • The target OpenSearch cluster is available.
  • The server that runs the Python code can communicate with the OpenSearch 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.

CSS allows you to use the Elasticsearch 7.10 Python client to connect to OpenSearch clusters. To ensure better compatibility, however, you are advised to use a Python client library that has the same version as the target OpenSearch cluster.

Select a reference example based on the Python client you use.

  • Scenario 1 (recommended): Use the OpenSearch Python client to access an OpenSearch cluster and install the OpenSearch Python client library.
    pip install opensearch-py
  • Scenario 2: Use the Elasticsearch 7.10 Python client to access an OpenSearch cluster and install the Elasticsearch 7.10 Python client library.
    pip install Elasticsearch==7.10

Cluster Access Scenarios

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

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

Use the OpenSearch Python client to connect to an OpenSearch 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
from opensearchpy import OpenSearch

class OpenSearchFactory(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) -> OpenSearch:
        addrs = []
        for host in self.host:
            addr = {'host': host, 'port': self.port}
            addrs.append(addr)

        if self.username and self.password:
            opensearch = OpenSearch(addrs, http_auth=(self.username, self.password))
        else:
            opensearch = OpenSearch(addrs)
        return opensearch
os = OpenSearchFactory(["{cluster address}"], "9200", None, None).create()
print(os.indices.exists(index='test'))

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

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

Use the OpenSearch Python client to connect to a security-mode OpenSearch 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
from opensearchpy import OpenSearch

class OpenSearchFactory(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) -> OpenSearch:
        addrs = []
        for host in self.host:
            addr = {'host': host, 'port': self.port}
            addrs.append(addr)

        if self.username and self.password:
            opensearch = OpenSearch(addrs, http_auth=(self.username, self.password))
        else:
            opensearch = OpenSearch(addrs)
        return opensearch
os = OpenSearchFactory(["xxx.xxx.xxx.xxx"], "9200", "username", "password").create()
print(os.indices.exists(index='test'))
Table 2 Variables

Parameter

Description

host

IP address for accessing the cluster. If there are multiple IP addresses, separate them with commas (,).

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 or false is returned, it indicates that the cluster is connected.

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

Use the OpenSearch Python client to connect to a security-mode OpenSearch 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
from opensearchpy import OpenSearch
import ssl

class OpenSearchFactory(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) -> OpenSearch:
        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:
            opensearch = OpenSearch(addrs, http_auth=(self.username, self.password), scheme="https", ssl_context=context)
        else:
            opensearch = OpenSearch(addrs)
        return opensearch
os = OpenSearchFactory(["xxx.xxx.xxx.xxx"], "9200", "username", "password").create()
print(os.indices.exists(index='test'))
Table 3 Variables

Parameter

Description

host

IP address for accessing the cluster. If there are multiple IP addresses, separate them with commas (,).

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 or false is returned, it indicates that the cluster is connected.

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

Use the Elasticsearch 7.10.2 Python client to connect to an OpenSearch 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:

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 or false is returned, it indicates that the cluster is connected.

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

Use the Elasticsearch 7.10.2 Python client to connect to a security-mode OpenSearch 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:

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 4 Variables

Parameter

Description

host

IP address for accessing the cluster. If there are multiple IP addresses, separate them with commas (,).

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 or false is returned, it indicates that the cluster is connected.

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

Use the Elasticsearch 7.10.2 Python client to connect to a security-mode OpenSearch 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:

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 5 Variables

Parameter

Description

host

IP address for accessing the cluster. If there are multiple IP addresses, separate them with commas (,).

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 or false is returned, it indicates that the cluster is connected.