2021-08-21 00:17:30 +05:30
[](https://github.com/opensearch-project/opensearch-py/actions/workflows/unified-release.yml)
[](https://github.com/opensearch-project/opensearch-py/actions/workflows/ci.yml)
[](https://github.com/opensearch-project/opensearch-py/actions/workflows/integration.yml)
[](https://discuss.opendistrocommunity.dev/c/clients/)

2021-08-31 10:34:02 -05:00

2021-04-26 10:12:40 -05:00
2021-08-03 19:48:23 +05:30
OpenSearch Python Client
2021-04-26 10:12:40 -05:00
2021-08-03 19:48:23 +05:30
- [Welcome! ](#welcome )
- [Project Resources ](#project-resources )
- [Code of Conduct ](#code-of-conduct )
- [License ](#license )
- [Copyright ](#copyright )
2021-04-26 10:12:40 -05:00
2021-08-03 19:48:23 +05:30
## Welcome!
2021-04-26 10:12:40 -05:00
2021-08-03 19:48:23 +05:30
**opensearch-py ** is [a community-driven, open source fork ](https://aws.amazon.com/blogs/opensource/introducing-opensearch/ ) of elasticsearch-py licensed under the [Apache v2.0 License ](LICENSE.txt ). For more information, see [opensearch.org ](https://opensearch.org/ ).
2021-04-26 10:12:40 -05:00
2021-08-20 18:28:14 +05:30
This is the low-level client. A high-level Python client is in the works, and will be available soon.
2021-08-28 00:02:52 +05:30
## Setup
2021-08-19 08:34:25 +05:30
2021-08-28 00:02:52 +05:30
To add the client to your project, install it using [pip ](https://pip.pypa.io/ ):
2021-08-19 08:34:25 +05:30
2021-08-28 00:02:52 +05:30
``` bash
2021-09-21 10:16:08 +05:30
pip install opensearch-py
2021-08-28 00:02:52 +05:30
```
2021-08-19 08:34:25 +05:30
2021-08-28 00:02:52 +05:30
Then import it like any other module:
2021-08-19 08:34:25 +05:30
2021-08-28 00:02:52 +05:30
``` python
2021-09-16 14:59:29 +05:30
from opensearchpy import OpenSearch
2021-08-28 00:02:52 +05:30
```
2021-08-19 08:34:25 +05:30
2021-08-28 00:02:52 +05:30
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 ).
2021-08-19 08:34:25 +05:30
2021-08-28 00:02:52 +05:30
## Sample code
2021-08-19 08:34:25 +05:30
2021-08-28 00:02:52 +05:30
``` python
2021-09-16 14:59:29 +05:30
from opensearchpy import OpenSearch
2021-08-28 00:02:52 +05:30
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 ( ' \n Creating 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 ( ' \n Adding 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 ( ' \n Search results: ' )
print ( response )
# Delete the document.
response = client . delete (
index = index_name ,
id = id
)
print ( ' \n Deleting document: ' )
print ( response )
# Delete the index.
response = client . indices . delete (
index = index_name
)
print ( ' \n Deleting index: ' )
print ( response )
```
2021-08-19 08:34:25 +05:30
2021-08-03 19:48:23 +05:30
## Project Resources
2021-04-26 10:12:40 -05:00
2021-08-03 19:48:23 +05:30
* [Project Website ](https://opensearch.org/ )
2021-08-19 10:41:05 +03:00
* [Downloads ](https://opensearch.org/downloads.html )
2021-08-03 19:48:23 +05:30
* [Documentation ](https://opensearch.org/docs/ )
* Need help? Try [Forums ](https://discuss.opendistrocommunity.dev/ )
* [Project Principles ](https://opensearch.org/#principles )
* [Contributing to OpenSearch ](CONTRIBUTING.md )
* [Maintainer Responsibilities ](MAINTAINERS.md )
* [Release Management ](RELEASING.md )
* [Admin Responsibilities ](ADMINS.md )
* [Security ](SECURITY.md )
2021-04-26 10:12:40 -05:00
2021-08-03 19:48:23 +05:30
## Code of Conduct
2021-04-26 10:12:40 -05:00
2021-08-03 19:48:23 +05:30
This project has adopted the [Amazon Open Source Code of Conduct ](CODE_OF_CONDUCT.md ). For more information see the [Code of Conduct FAQ ](https://aws.github.io/code-of-conduct-faq ), or contact [opensource-codeofconduct@amazon.com ](mailto:opensource-codeofconduct@amazon.com ) with any additional questions or comments.
2021-04-26 10:12:40 -05:00
2021-08-03 19:48:23 +05:30
## License
2021-04-26 10:12:40 -05:00
2021-08-03 19:48:23 +05:30
This project is licensed under the [Apache v2.0 License ](LICENSE.txt ).
2021-04-26 10:12:40 -05:00
2021-08-03 19:48:23 +05:30
## Copyright
2021-04-26 10:12:40 -05:00
2021-08-19 08:34:25 +05:30
Copyright OpenSearch Contributors. See [NOTICE ](NOTICE.txt ) for details.