Connecting to Elasticsearch with Python
For Legacy Support Purposes Only |
---|
In this example, we will use Python OpenSearch client library.
Prerequisites
You need to install Python on your machine. Depending on your operating system, download and install python. Note: This sample code uses Python3.
1. Installation
Install Python OpenSearch client package using pip
1 |
pip install opensearch-py |
2. Connecting to cluster
# SSL client authentication using client_cert and client_key
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
ca_certs_path = '/full path to cluster-ca-certificate.pem' hosts = ["xxx.xxx.xxx.xxx", "xxx.xxx.xxx.xxx", "xxx.xxx.xxx.xxx"] port = 9200 auth = ("icelasticsearch", "<Password>") client = OpenSearch( hosts = hosts, port = port, http_auth = auth, use_ssl = True, verify_certs = True, ca_certs = ca_certs_path ) |
3. Indexing
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 28 29 30 |
index_name = 'python-test-index' index_body = { 'settings': { 'index': { 'number_of_shards': 4 } } } response = client.indices.create(index_name, body=index_body) print('\nCreating index:') print('We get response:', response) # Add a document to the index created before. document = { 'title': 'Moneyball', 'director': 'Bennett Miller', 'year': '2011' } id = '1' response = client.index( index = index_name, body = document, id = id, refresh = True ) print('\nAdding document:') print('We get response:', response) |
4. Getting a document
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# Search for the document. q = 'miller' query = { 'size': 5, 'query': { 'multi_match': { 'query': q, 'fields': ['title^2', 'director'] } } } response = client.search( body = query, index = index_name ) print('\nSearch results:') print('We get response:', response) |
Full sample code
Replace public IP, username and password with your real setting
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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
from opensearchpy import OpenSearch ca_certs_path = '/full/path/to/cluster-ca-certificate.pem' hosts = ["xxx.xxx.xxx.xxx", "xxx.xxx.xxx.xxx", "xxx.xxx.xxx.xxx"] port = 9200 auth = ("icelasticsearch", "<Password>") client = OpenSearch( hosts = hosts, port = port, http_auth = auth, use_ssl = True, verify_certs = True, ca_certs = ca_certs_path ) # Testing # Create an index with non-default settings. index_name = 'python-test-index' index_body = { 'settings': { 'index': { 'number_of_shards': 4 } } } response = client.indices.create(index_name, body=index_body) print('\nCreating index:') print('We get response:', response) # Add a document to the index created before. document = { 'title': 'Moneyball', 'director': 'Bennett Miller', 'year': '2011' } id = '1' response = client.index( index = index_name, body = document, id = id, refresh = True ) print('\nAdding document:') print('We get response:', response) # Search for the document. q = 'miller' query = { 'size': 5, 'query': { 'multi_match': { 'query': q, 'fields': ['title^2', 'director'] } } } response = client.search( body = query, index = index_name ) print('\nSearch results:') print('We get response:', response) # Delete the document. response = client.delete( index = index_name, id = id ) print('\nDeleting document:') print('We get response:', response) # Delete the index. response = client.indices.delete( index = index_name ) print('\nDeleting index:') print('We get response:', response) |
Transparent, fair, and flexible pricing for your data infrastructure: See Instaclustr Pricing Here
By Instaclustr Support
Need Support?
Experiencing difficulties on the website or console?
Already have an account?
Need help with your cluster?
Contact Support
Why sign up?