2023-07-24 12:23:51 -05:00
- [OpenSearch Python Client User Guide ](#opensearch-python-client-user-guide )
2022-04-05 14:06:54 -07:00
- [Setup ](#setup )
2023-07-24 12:23:51 -05:00
- [Basic Features ](#basic-features )
- [Creating a Client ](#creating-a-client )
- [Creating an Index ](#creating-an-index )
- [Adding a Document to an Index ](#adding-a-document-to-an-index )
- [Searching for a Document ](#searching-for-a-document )
- [Deleting a Document ](#deleting-a-document )
- [Deleting an Index ](#deleting-an-index )
- [Advanced Features ](#advanced-features )
- [Plugins ](#plugins )
# OpenSearch Python Client User Guide
2022-04-05 14:06:54 -07:00
## Setup
To add the client to your project, install it using [pip ](https://pip.pypa.io/ ):
``` bash
pip install opensearch-py
```
Then import it like any other module:
``` python
from opensearchpy import OpenSearch
```
2023-07-25 21:04:13 -05:00
For better performance we recommend the async client. See [Asynchronous I/O ](guides/async.md ) for more information.
2023-03-28 10:21:07 -07:00
2023-07-24 12:23:51 -05:00
In general, we recommend using a package manager, such as [poetry ](https://python-poetry.org/docs/ ), for your projects. This is the package manager used for [samples ](samples ).
2022-04-05 14:06:54 -07:00
2023-07-24 12:23:51 -05:00
## Basic Features
2022-04-05 14:06:54 -07:00
2023-07-24 12:23:51 -05:00
In the example below, we create a client, create an index with non-default settings, insert a
document into the index, search for the document, delete the document, and finally delete the index.
2022-04-05 14:06:54 -07:00
2023-07-24 12:23:51 -05:00
You can find working versions of the code below that can be run with a local instance of OpenSearch in [samples ](samples ).
### Creating a Client
2022-08-26 16:45:36 -07:00
2022-04-05 14:06:54 -07:00
``` python
from opensearchpy import OpenSearch
host = ' localhost '
port = 9200
auth = ( ' admin ' , ' admin ' ) # For testing only. Don't store credentials in code.
2022-11-22 11:33:45 -05:00
2022-04-05 14:06:54 -07:00
client = OpenSearch (
hosts = [ { ' host ' : host , ' port ' : port } ] ,
http_auth = auth ,
use_ssl = True ,
2023-07-24 12:23:51 -05:00
verify_certs = False
2022-04-05 14:06:54 -07:00
)
2023-07-24 12:23:51 -05:00
info = client . info ( )
print ( f " Welcome to { info [ ' version ' ] [ ' distribution ' ] } { info [ ' version ' ] [ ' number ' ] } ! " )
2022-08-26 16:45:36 -07:00
```
2023-07-25 21:04:13 -05:00
See [hello.py ](samples/hello/hello.py ) for a working synchronous sample, and [guides/ssl ](guides/ssl.md ) for how to setup SSL certificates.
2023-07-24 12:23:51 -05:00
### Creating an Index
2022-08-26 16:45:36 -07:00
``` python
2023-07-24 12:23:51 -05:00
index_name = ' test-index '
2022-04-05 14:06:54 -07:00
index_body = {
' settings ' : {
' index ' : {
' number_of_shards ' : 4
}
}
}
2023-07-24 12:23:51 -05:00
response = client . indices . create (
index_name ,
body = index_body
)
2022-04-05 14:06:54 -07:00
print ( response )
2022-08-26 16:45:36 -07:00
```
2022-04-05 14:06:54 -07:00
2023-07-24 12:23:51 -05:00
### Adding a Document to an Index
2022-08-26 16:45:36 -07:00
``` python
2022-04-05 14:06:54 -07:00
document = {
' title ' : ' Moneyball ' ,
' director ' : ' Bennett Miller ' ,
' year ' : ' 2011 '
}
2023-07-24 12:23:51 -05:00
2022-04-05 14:06:54 -07:00
id = ' 1 '
response = client . index (
index = index_name ,
body = document ,
id = id ,
refresh = True
)
print ( response )
2022-08-26 16:45:36 -07:00
```
2023-07-24 12:23:51 -05:00
### Searching for a Document
2022-08-26 16:45:36 -07:00
``` python
2022-04-05 14:06:54 -07:00
q = ' miller '
query = {
' size ' : 5 ,
' query ' : {
' multi_match ' : {
' query ' : q ,
' fields ' : [ ' title^2 ' , ' director ' ]
}
}
}
response = client . search (
body = query ,
index = index_name
)
2023-07-24 12:23:51 -05:00
2022-04-05 14:06:54 -07:00
print ( response )
2022-08-26 16:45:36 -07:00
```
2022-04-05 14:06:54 -07:00
2023-07-24 12:23:51 -05:00
### Deleting a Document
2022-08-26 16:45:36 -07:00
``` python
2022-04-05 14:06:54 -07:00
response = client . delete (
index = index_name ,
id = id
)
2023-06-28 22:56:00 +02:00
print ( response )
```
2023-07-24 12:23:51 -05:00
### Deleting an Index
2023-06-28 22:56:00 +02:00
``` python
2023-07-24 12:23:51 -05:00
response = client . indices . delete (
2022-04-05 14:06:54 -07:00
index = index_name
)
print ( response )
2022-12-01 20:22:49 +01:00
```
2023-07-24 12:23:51 -05:00
## Advanced Features
2023-01-02 14:25:48 -08:00
2023-07-25 21:04:13 -05:00
- [Asynchronous I/O ](guides/async.md )
2023-07-24 12:23:51 -05:00
- [Authentication (IAM, SigV4) ](guides/auth.md )
- [Configuring SSL ](guides/ssl.md )
- [Bulk Indexing ](guides/bulk.md )
- [High Level DSL ](guides/dsl.md )
- [Index Lifecycle ](guides/index_lifecycle.md )
- [Search ](guides/search.md )
- [Point in Time ](guides/point_in_time.md )
- [Using a Proxy ](guides/proxy.md )
2024-08-15 10:52:20 -04:00
- [Working with Snapshots ](guides/snapshot.md )
2023-10-11 10:11:35 -07:00
- [Index Templates ](guides/index_template.md )
2023-10-16 08:27:22 -07:00
- [Advanced Index Actions ](guides/advanced_index_actions.md )
2023-10-16 15:32:17 -04:00
- [Making Raw JSON REST Requests ](guides/json.md )
2023-10-12 18:55:31 -04:00
- [Connection Classes ](guides/connection_classes.md )
2023-10-30 20:02:30 -07:00
- [Document Lifecycle ](guides/document_lifecycle.md )
2023-11-22 06:14:12 -08:00
- [Collecting Logs ](guides/log_collection.md )
2023-03-28 10:21:07 -07:00
2023-07-24 12:23:51 -05:00
## Plugins
2023-03-28 10:21:07 -07:00
2023-07-24 12:23:51 -05:00
- [Security ](guides/plugins/security.md )
- [Alerting ](guides/plugins/alerting.md )
2023-07-25 21:04:13 -05:00
- [Index Management ](guides/plugins/index_management.md )
- [k-NN ](guides/plugins/knn.md )