2021-08-19 12:56:53 -07:00
---
layout: default
title: Python client
nav_order: 70
---
# Python client
2021-09-30 13:48:32 -07:00
The OpenSearch Python client provides a more natural syntax for interacting with your cluster. Rather than sending HTTP requests to a given URL, you can create an OpenSearch client for your cluster and call the client's built-in functions.
2021-08-19 12:56:53 -07:00
2021-09-21 13:40:38 -07:00
{% comment %}
`opensearch-py` is the lower-level of the two Python clients. If you want a general client for assorted operations, it's a great choice. If you want a higher-level client strictly for indexing and search operations, consider [opensearch-dsl-py ]({{site.url}}{{site.baseurl}}/clients/python-dsl/ ).
{% endcomment %}
2021-08-19 12:56:53 -07:00
## Setup
2021-08-26 11:50:46 -07:00
To add the client to your project, install it using [pip ](https://pip.pypa.io/ ):
2021-08-19 12:56:53 -07:00
``` bash
2021-09-21 13:40:38 -07:00
pip install opensearch-py
2021-08-19 12:56:53 -07:00
```
Then import it like any other module:
``` python
2021-09-21 13:40:38 -07:00
from opensearchpy import OpenSearch
2021-08-19 12:56:53 -07:00
```
2021-08-26 09:15:50 -07:00
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 12:56:53 -07:00
## Sample code
``` python
2021-09-21 13:40:38 -07:00
from opensearchpy import OpenSearch
2021-08-19 12:56:53 -07:00
host = ' localhost '
port = 9200
auth = ( ' admin ' , ' admin ' ) # For testing only. Don't store credentials in code.
2021-08-19 13:23:14 -07:00
ca_certs_path = ' /full/path/to/root-ca.pem ' # Provide a CA bundle if you use intermediate CAs with your root CA.
2021-08-19 12:56:53 -07:00
# 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 } ] ,
2021-08-19 13:23:14 -07:00
http_compress = True , # enables gzip compression for request bodies
2021-08-19 12:56:53 -07:00
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.
2021-09-21 13:40:38 -07:00
index_name = ' python-test-index '
2021-08-19 12:56:53 -07:00
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 (
2021-08-19 13:23:14 -07:00
index = index_name ,
body = document ,
id = id ,
refresh = True
2021-08-19 12:56:53 -07:00
)
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 (
2021-08-19 13:23:14 -07:00
body = query ,
index = index_name
2021-08-19 12:56:53 -07:00
)
print ( ' \n Search results: ' )
print ( response )
# Delete the document.
response = client . delete (
2021-08-19 13:23:14 -07:00
index = index_name ,
id = id
2021-08-19 12:56:53 -07:00
)
print ( ' \n Deleting document: ' )
print ( response )
# Delete the index.
response = client . indices . delete (
2021-08-19 13:23:14 -07:00
index = index_name
2021-08-19 12:56:53 -07:00
)
print ( ' \n Deleting index: ' )
print ( response )
```