Accessing a Cluster Using Python
You can access a CSS cluster using Python.
Prerequisites
- The CSS cluster is available.
- Ensure that the server running Python can communicate with the CSS cluster.
Procedure
- Install the Elasticsearch Python client. You are advised to use the client version that matches the Elasticsearch version. For example, if the cluster version is 7.6.2, install the Elasticsearch Python client 7.6.
1
pip install Elasticsearch==7.6.2
- Create an Elasticsearch client and check whether the test index exists. The examples for clusters in different security modes are as follows:
- Cluster in non-security mode
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", None, None).create() print(es.indices.exists(index='test'))
- Cluster in security mode + HTTP
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'))
- Cluster in security mode + HTTPS
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 1 Variables Name
Description
host
List of the IP addresses of Elasticsearch nodes (or independent Client node). Multiple IP addresses are separated using commas (,).
port
Access port of the Elasticsearch cluster. Enter 9200.
username
Username for accessing the cluster.
password
Password of the user.
- Cluster in non-security mode
- Create a cluster index through the Elasticsearch client.
1 2 3 4 5 6 7 8 9 10 11 12
mappings = { "settings": { "index": { "number_of_shards": number_of_shards, "number_of_replicas": 1, }, }, "mappings": { properties } } result = es.indices.create(index=index, body=mappings)
- Query the index created in the previous step through the Elasticsearch client.
1 2 3 4 5 6 7 8
body = { "query": { "match": { "Query field": "Query content" } } } result = es.search(index=index, body=body)
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot