You can access an Elasticsearch cluster created in CSS using Python.
1 | pip install Elasticsearch==7.6 |
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')) |
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')) |
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')) |
Parameter |
Description |
|---|---|
host |
IP address for accessing the Elasticsearch cluster. If there are multiple IP addresses, separate them with commas (,). |
port |
Access port of the Elasticsearch cluster. Enter 9200. |
username |
Username for accessing the cluster. |
password |
Password of the user. |
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) |
1 2 3 4 5 6 7 8 | body = { "query": { "match": { "Query field": "Query content" } } } result = es.search(index=index, body=body) |