Connect to OpenSearch with Python
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-py client package using pip
1 |
pip install opensearch-py |
To connect to clusters using HTTPS with the Python client, you will also need to install certifi:
1 |
pip install certifi |
2. Connect to Cluster
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
from opensearchpy import OpenSearch host = [{'host': 'xxx.xxx.xxx.xxx', 'port': 9200}, {'host': 'xxx.xxx.xxx.xxx', 'port': 9200}, {'host': 'xxx.xxx.xxx.xxx', 'port': 9200}] auth = ('icopensearch', '<Password>') client = OpenSearch( hosts=host, http_compress=True, http_auth=auth, use_ssl=True, verify_certs=True, ssl_assert_hostname='xxx.xxx.xxx.xxx', ssl_show_warn=True ) |
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 |
# Create an index with non-default settings. index_name = 'python-test-index' index_body = { 'settings': { 'index': { 'number_of_shards': 2 } } } response = client.indices.create(index_name, body=index_body) print('\nCreating index:') print(response) 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(response) |
4. Getting a document
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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(response) |
5. Deleting a document and an index
1 2 3 4 5 6 7 8 9 10 11 12 |
response = client.delete( index=index_name, id=id ) print('\nDeleting document:') print(response) response = client.indices.delete(index=index_name) print('\nDeleting index:') print(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 |
from opensearchpy import OpenSearch host = [{'host': 'xxx.xxx.xxx.xxx', 'port': 9200}, {'host': 'xxx.xxx.xxx.xxx', 'port': 9200}, {'host': 'xxx.xxx.xxx.xxx', 'port': 9200}] auth = ('icopensearch', '<Password>') client = OpenSearch( hosts=host, http_compress=True, http_auth=auth, use_ssl=True, verify_certs=True, ssl_assert_hostname='xxx.xxx.xxx.xxx', ssl_show_warn=True ) # Create an index with non-default settings. index_name = 'python-test-index' index_body = { 'settings': { 'index': { 'number_of_shards': 2 } } } response = client.indices.create(index_name, body=index_body) print('\nCreating index:') print(response) 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(response) 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(response) response = client.delete( index=index_name, id=id ) print('\nDeleting document:') print(response) response = client.indices.delete(index=index_name) print('\nDeleting index:') print(response) |
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?