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.
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
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.
Elasticsearch Cluster Security-Mode Settings | Details |
|---|---|
Non-security mode | Connecting to a Non-Security Mode Cluster with the Elasticsearch Python Client |
Security mode + HTTP | Connecting to a Security-Mode+HTTP Cluster with the Elasticsearch Python Client |
Security mode + HTTPS | Connecting to a Security-Mode+HTTPS 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:
123456789101112131415161718192021222324from elasticsearch import Elasticsearchclass ElasticFactory(object):def __init__(self, host: list, port: str, username: str, password: str):self.port = portself.host = hostself.username = usernameself.password = passworddef 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 elasticsearches = 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.
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:
123456789101112131415161718192021222324from elasticsearch import Elasticsearchclass ElasticFactory(object):def __init__(self, host: list, port: str, username: str, password: str):self.port = portself.host = hostself.username = usernameself.password = passworddef 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 elasticsearches = ElasticFactory(["xxx.xxx.xxx.xxx"], "9200", "username", "password").create()print(es.indices.exists(index='test'))
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.
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:
123456789101112131415161718192021222324252627from elasticsearch import Elasticsearchimport sslclass ElasticFactory(object):def __init__(self, host: list, port: str, username: str, password: str):self.port = portself.host = hostself.username = usernameself.password = passworddef 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 elasticsearches = ElasticFactory(["xxx.xxx.xxx.xxx"], "9200", "username", "password").create()print(es.indices.exists(index='test'))
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.