You can access an Elasticsearch cluster created in CSS using Python.
1pip install Elasticsearch==7.6
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", None, None).create()print(es.indices.exists(index='test'))
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'))
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 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. |
123456789101112mappings = {"settings": {"index": {"number_of_shards": number_of_shards,"number_of_replicas": 1,},},"mappings": {properties}}result = es.indices.create(index=index, body=mappings)
12345678body = {"query": {"match": {"Query field": "Query content"}}}result = es.search(index=index, body=body)