Help Center/ Cloud Search Service/ Best Practices/ Cluster Access/ Accessing a Cluster Using Python
Updated on 2023-03-29 GMT+08:00

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

  1. 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
    
  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.

  3. 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)
    
  4. 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)