Files
Daniel (dB.) Doubrovkine e68b9e762d Added client-level REST helpers. (#544)
* Added client-level REST helpers.

Signed-off-by: dblock <dblock@amazon.com>

* Move functions into an .http namespace.

Signed-off-by: dblock <dblock@amazon.com>

* Poetry update in samples.

Signed-off-by: dblock <dblock@amazon.com>

* Fix: typo.

Signed-off-by: dblock <dblock@amazon.com>

* Clarified what to use in which older versions.

Signed-off-by: dblock <dblock@amazon.com>

---------

Signed-off-by: dblock <dblock@amazon.com>
2023-11-13 12:52:13 -08:00

2.0 KiB

Making Raw JSON REST Requests

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 for a complete working sample.

GET

The following example returns the server version information via GET /.

info = client.get("/")
print(f"Welcome to {info["version"]["distribution"]} {info["version"]["number"]}!")

Note that the client will parse the response as JSON when appropriate.

These methods are also available in the asynchronous client.

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.

info = client.transport.perform_request("GET", "/")
print(f"Welcome to {info["version"]["distribution"]} {info["version"]["number"]}!")

PUT

The following example creates an index.

index_body = {
  "settings": {
    "index": {
      "number_of_shards": 4
    }
  }
}

client.http.put("/movies", body=index_body)

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.

POST

The following example searches for a document.

q = "miller"

query = {
  "size": 5,
  "query": {
    "multi_match": {
      "query": q,
      "fields": ["title^2", "director"]
    }
  }
}

client.http.post("/movies/_search", body = query)

DELETE

The following example deletes an index.

client.http.delete("/movies")