Python low-level client for OpenSearch
Project description
OpenSearch Python Client
Welcome!
opensearch-py is a community-driven, open source fork of elasticsearch-py licensed under the Apache v2.0 License. For more information, see opensearch.org.
This is the low-level client. A high-level Python client is in the works, and will be available soon.
Setup
To add the client to your project, install it using pip:
pip install opensearch-py
Then import it like any other module:
from opensearchpy import OpenSearch
If you prefer to add the client manually or just want to examine the source code, see opensearch-py on GitHub.
Sample code
from opensearchpy import OpenSearch
host = 'localhost'
port = 9200
auth = ('admin', 'admin') # For testing only. Don't store credentials in code.
ca_certs_path = '/full/path/to/root-ca.pem' # Provide a CA bundle if you use intermediate CAs with your root CA.
# Optional client certificates if you don't want to use HTTP basic authentication.
# client_cert_path = '/full/path/to/client.pem'
# client_key_path = '/full/path/to/client-key.pem'
# Create the client with SSL/TLS enabled, but hostname verification disabled.
client = OpenSearch(
hosts = [{'host': host, 'port': port}],
http_compress = True, # enables gzip compression for request bodies
http_auth = auth,
# client_cert = client_cert_path,
# client_key = client_key_path,
use_ssl = True,
verify_certs = True,
ssl_assert_hostname = False,
ssl_show_warn = False,
ca_certs = ca_certs_path
)
# Create an index with non-default settings.
index_name = 'python-test-index3'
index_body = {
'settings': {
'index': {
'number_of_shards': 4
}
}
}
response = client.indices.create(index_name, body=index_body)
print('\nCreating index:')
print(response)
# Add a document to the index.
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)
# 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(response)
# Delete the document.
response = client.delete(
index = index_name,
id = id
)
print('\nDeleting document:')
print(response)
# Delete the index.
response = client.indices.delete(
index = index_name
)
print('\nDeleting index:')
print(response)
Using IAM credentials for authentication
Refer the AWS documentation regarding usage of IAM credentials to sign requests to OpenSearch APIs - Signing HTTP requests to Amazon OpenSearch Service.
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.
Pre-requisites to use AWSV4SignerAuth
-
Python version 3.6 or above,
-
Install botocore using pip
pip install botocore
Here is the sample code that uses AWSV4SignerAuth
-
from opensearchpy import OpenSearch, RequestsHttpConnection, AWSV4SignerAuth
import boto3
host = '' # cluster endpoint, for example: my-test-domain.us-east-1.es.amazonaws.com
region = 'us-west-2'
credentials = boto3.Session().get_credentials()
auth = AWSV4SignerAuth(credentials, region)
index_name = 'python-test-index3'
client = OpenSearch(
hosts = [{'host': host, 'port': 443}],
http_auth = auth,
use_ssl = True,
verify_certs = True,
connection_class = RequestsHttpConnection
)
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)
Project Resources
- Project Website
- Downloads
- Documentation
- Need help? Try Forums
- Project Principles
- Contributing to OpenSearch
- Maintainer Responsibilities
- Release Management
- Admin Responsibilities
- Security
Code of Conduct
This project has adopted the Amazon Open Source Code of Conduct. For more information see the Code of Conduct FAQ, or contact opensource-codeofconduct@amazon.com with any additional questions or comments.
License
This project is licensed under the Apache v2.0 License.
Copyright
Copyright OpenSearch Contributors. See NOTICE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file opensearch-py-1.1.0.tar.gz
.
File metadata
- Download URL: opensearch-py-1.1.0.tar.gz
- Upload date:
- Size: 131.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d0c41cea61fedc34542be7fb9169931360134cf823c596f719106c3bd8466fe |
|
MD5 | e8c1344400dcd42bdf94c7d290687b4a |
|
BLAKE2b-256 | f3db38f052c36292c20f51a8a8c91e5226f2ed830212dd898f6df89f1ef65e39 |
File details
Details for the file opensearch_py-1.1.0-py2.py3-none-any.whl
.
File metadata
- Download URL: opensearch_py-1.1.0-py2.py3-none-any.whl
- Upload date:
- Size: 207.5 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.63.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb573546fb373dac8091be9b8eac2ba8da277713eea4b50b4a49ccd30dec25f1 |
|
MD5 | e7946faac706c58d15e496337564782b |
|
BLAKE2b-256 | 74fd59f57eaa83cf765a504d9cebdb99d1c9057c1987dc69fc5be61b6b756fa7 |