2023-10-16 15:32:17 -04:00
- [Making Raw JSON REST Requests ](#making-raw-json-rest-requests )
- [GET ](#get )
- [PUT ](#put )
- [POST ](#post )
- [DELETE ](#delete )
# Making Raw JSON REST Requests
2023-11-13 15:52:13 -05:00
The OpenSearch client implements many high-level REST DSLs that invoke OpenSearch APIs. However you may find yourself in a situation that requires you to invoke an API that is not supported by the client. Use `client.http.get` , `head` , `put` , `post` , and `delete` to do so. See [samples/json ](../samples/json ) for a complete working sample.
2023-10-16 15:32:17 -04:00
## GET
The following example returns the server version information via `GET /` .
```python
2023-11-13 15:52:13 -05:00
info = client . get ( "/" )
print ( f "Welcome to { info [ "version" ][ "distribution" ] } { info [ "version" ][ "number" ] } !" )
2023-10-16 15:32:17 -04:00
```
Note that the client will parse the response as JSON when appropriate.
2023-11-13 15:52:13 -05:00
These methods are also available in the asynchronous client.
```python
info = await client . http . get ( "/" )
print ( f "Welcome to { info [ "version" ][ "distribution" ] } { info [ "version" ][ "number" ] } !" )
```
Use `perform_request` in older versions (<= 2.3.x), and `client.http.get` and others in newer ones.
```python
info = client . transport . perform_request ( "GET" , "/" )
print ( f "Welcome to { info [ "version" ][ "distribution" ] } { info [ "version" ][ "number" ] } !" )
```
2023-10-16 15:32:17 -04:00
## PUT
The following example creates an index.
```python
index_body = {
2023-11-13 15:52:13 -05:00
"settings" : {
"index" : {
"number_of_shards" : 4
2023-10-16 15:32:17 -04:00
}
}
}
2023-11-13 15:52:13 -05:00
client . http . put ( "/movies" , body = index_body )
2023-10-16 15:32:17 -04:00
```
2023-11-13 15:52:13 -05:00
Note that the client will raise errors automatically. For example, if the index already exists, an `opensearchpy.exceptions.RequestError: RequestError(400, "resource_already_exists_exception",` will be thrown.
2023-10-16 15:32:17 -04:00
## POST
The following example searches for a document.
```python
2023-11-13 15:52:13 -05:00
q = "miller"
2023-10-16 15:32:17 -04:00
query = {
2023-11-13 15:52:13 -05:00
"size" : 5 ,
"query" : {
"multi_match" : {
"query" : q ,
"fields" : [ "title^2" , "director" ]
2023-10-16 15:32:17 -04:00
}
}
}
2023-11-13 15:52:13 -05:00
client . http . post ( "/movies/_search" , body = query )
2023-10-16 15:32:17 -04:00
```
## DELETE
The following example deletes an index.
```python
2023-11-13 15:52:13 -05:00
client . http . delete ( "/movies" )
2023-10-16 15:32:17 -04:00
```