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.
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"]}!")
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.
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)
The following example deletes an index.
client.http.delete("/movies")