|
| 1 | +- [Getting Started with the OpenSearch Python Client](#getting-started-with-the-opensearch-python-client) |
| 2 | + - [Setup](#setup) |
| 3 | + - [Sample code](#sample-code) |
| 4 | + - [Using IAM credentials for authentication](#using-iam-credentials-for-authentication) |
| 5 | + - [Pre-requisites to use `AWSV4SignerAuth`](#pre-requisites-to-use-awsv4signerauth) |
| 6 | + |
| 7 | +# Getting Started with the OpenSearch Python Client |
| 8 | + |
| 9 | +## Setup |
| 10 | + |
| 11 | +To add the client to your project, install it using [pip](https://pip.pypa.io/): |
| 12 | + |
| 13 | +```bash |
| 14 | +pip install opensearch-py |
| 15 | +``` |
| 16 | + |
| 17 | +Then import it like any other module: |
| 18 | + |
| 19 | +```python |
| 20 | +from opensearchpy import OpenSearch |
| 21 | +``` |
| 22 | + |
| 23 | +If you prefer to add the client manually or just want to examine the source code, see [opensearch-py on GitHub](https://github.com/opensearch-project/opensearch-py). |
| 24 | + |
| 25 | + |
| 26 | +## Sample code |
| 27 | + |
| 28 | +```python |
| 29 | +from opensearchpy import OpenSearch |
| 30 | + |
| 31 | +host = 'localhost' |
| 32 | +port = 9200 |
| 33 | +auth = ('admin', 'admin') # For testing only. Don't store credentials in code. |
| 34 | +ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA. |
| 35 | + |
| 36 | +# Optional client certificates if you don't want to use HTTP basic authentication. |
| 37 | +# client_cert_path = '/full/path/to/client.pem' |
| 38 | +# client_key_path = '/full/path/to/client-key.pem' |
| 39 | + |
| 40 | +# Create the client with SSL/TLS enabled, but hostname verification disabled. |
| 41 | +client = OpenSearch( |
| 42 | + hosts = [{'host': host, 'port': port}], |
| 43 | + http_compress = True, # enables gzip compression for request bodies |
| 44 | + http_auth = auth, |
| 45 | + # client_cert = client_cert_path, |
| 46 | + # client_key = client_key_path, |
| 47 | + use_ssl = True, |
| 48 | + verify_certs = True, |
| 49 | + ssl_assert_hostname = False, |
| 50 | + ssl_show_warn = False, |
| 51 | + ca_certs = ca_certs_path |
| 52 | +) |
| 53 | + |
| 54 | +# Create an index with non-default settings. |
| 55 | +index_name = 'python-test-index3' |
| 56 | +index_body = { |
| 57 | + 'settings': { |
| 58 | + 'index': { |
| 59 | + 'number_of_shards': 4 |
| 60 | + } |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +response = client.indices.create(index_name, body=index_body) |
| 65 | +print('\nCreating index:') |
| 66 | +print(response) |
| 67 | + |
| 68 | +# Add a document to the index. |
| 69 | +document = { |
| 70 | + 'title': 'Moneyball', |
| 71 | + 'director': 'Bennett Miller', |
| 72 | + 'year': '2011' |
| 73 | +} |
| 74 | +id = '1' |
| 75 | + |
| 76 | +response = client.index( |
| 77 | + index = index_name, |
| 78 | + body = document, |
| 79 | + id = id, |
| 80 | + refresh = True |
| 81 | +) |
| 82 | + |
| 83 | +print('\nAdding document:') |
| 84 | +print(response) |
| 85 | + |
| 86 | +# Search for the document. |
| 87 | +q = 'miller' |
| 88 | +query = { |
| 89 | + 'size': 5, |
| 90 | + 'query': { |
| 91 | + 'multi_match': { |
| 92 | + 'query': q, |
| 93 | + 'fields': ['title^2', 'director'] |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +response = client.search( |
| 99 | + body = query, |
| 100 | + index = index_name |
| 101 | +) |
| 102 | +print('\nSearch results:') |
| 103 | +print(response) |
| 104 | + |
| 105 | +# Delete the document. |
| 106 | +response = client.delete( |
| 107 | + index = index_name, |
| 108 | + id = id |
| 109 | +) |
| 110 | + |
| 111 | +print('\nDeleting document:') |
| 112 | +print(response) |
| 113 | + |
| 114 | +# Delete the index. |
| 115 | +response = client.indices.delete( |
| 116 | + index = index_name |
| 117 | +) |
| 118 | + |
| 119 | +print('\nDeleting index:') |
| 120 | +print(response) |
| 121 | +``` |
| 122 | + |
| 123 | +## Using IAM credentials for authentication |
| 124 | + |
| 125 | +Refer the AWS documentation regarding usage of IAM credentials to sign requests to OpenSearch APIs - [Signing HTTP requests to Amazon OpenSearch Service.](https://docs.aws.amazon.com/opensearch-service/latest/developerguide/request-signing.html#request-signing-python) |
| 126 | + |
| 127 | +Opensearch-py client library also provides an in-house IAM based authentication feature, `AWSV4SignerAuth` that will help users to connect to their opensearch clusters by making use of IAM roles. |
| 128 | + |
| 129 | +#### Pre-requisites to use `AWSV4SignerAuth` |
| 130 | + - Python version 3.6 or above, |
| 131 | + - Install [botocore](https://pypi.org/project/botocore/) using pip |
| 132 | + |
| 133 | + `pip install botocore` |
| 134 | + |
| 135 | +Here is the sample code that uses `AWSV4SignerAuth` - |
| 136 | + |
| 137 | +```python |
| 138 | +from opensearchpy import OpenSearch, RequestsHttpConnection, AWSV4SignerAuth |
| 139 | +import boto3 |
| 140 | + |
| 141 | +host = '' # cluster endpoint, for example: my-test-domain.us-east-1.es.amazonaws.com |
| 142 | +region = 'us-west-2' |
| 143 | +credentials = boto3.Session().get_credentials() |
| 144 | +auth = AWSV4SignerAuth(credentials, region) |
| 145 | +index_name = 'python-test-index3' |
| 146 | + |
| 147 | +client = OpenSearch( |
| 148 | + hosts = [{'host': host, 'port': 443}], |
| 149 | + http_auth = auth, |
| 150 | + use_ssl = True, |
| 151 | + verify_certs = True, |
| 152 | + connection_class = RequestsHttpConnection |
| 153 | +) |
| 154 | + |
| 155 | +q = 'miller' |
| 156 | +query = { |
| 157 | + 'size': 5, |
| 158 | + 'query': { |
| 159 | + 'multi_match': { |
| 160 | + 'query': q, |
| 161 | + 'fields': ['title^2', 'director'] |
| 162 | + } |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +response = client.search( |
| 167 | + body = query, |
| 168 | + index = index_name |
| 169 | +) |
| 170 | + |
| 171 | +print('\nSearch results:') |
| 172 | +print(response) |
| 173 | +``` |
0 commit comments