Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions _api-reference/grpc-apis/bulk.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,58 @@ public class BulkClient {
}
```
{% include copy.html %}

## Python gRPC client example

The following example shows how to send the same request using a Python client application.

First, install the `opensearch-protobufs` package using `pip`:

```bash
pip install opensearch-protobufs==0.19.0
```
{% include copy.html %}

Use the following code to send the request:

```python
import grpc

from opensearch.protobufs.schemas import document_pb2
from opensearch.protobufs.schemas import common_pb2
from opensearch.protobufs.services.document_service_pb2_grpc import DocumentServiceStub

channel = grpc.insecure_channel(
target="localhost:9400",
)

document_stub = DocumentServiceStub(channel)

# Add documents to a request body
requestBody = document_pb2.BulkRequestBody(
operation_container=document_pb2.OperationContainer(index=document_pb2.IndexOperation())
)
requestBody.object = "{\"field\": \"value\"}".encode('utf-8')

# Append to a bulk request
request = document_pb2.BulkRequest()
request.index = "my-index"
request.request_body.append(requestBody)

# Send request and handle response
try:
response = document_stub.Bulk(request=request)
if response.items:
print("Received {} response items".format(len(response.items)))
print(response.items)
except grpc.RpcError as e:
if e.code() == StatusCode.UNAVAILABLE:
print("Failed to reach server: {}".format(e))
elif e.code() == StatusCode.PERMISSION_DENIED:
print("Permission denied: {}".format(e))
elif e.code() == StatusCode.INVALID_ARGUMENT:
print("Invalid argument: {}".format(e))
finally:
channel.close()
```
{% include copy.html %}
2 changes: 1 addition & 1 deletion _api-reference/grpc-apis/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ To submit gRPC requests, you must have a set of protobufs on the client side. Yo

- **Raw protobufs**: Download the raw protobuf schema from the [OpenSearch Protobufs GitHub repository (v0.19.0)](https://github.com/opensearch-project/opensearch-protobufs/releases/tag/0.19.0). You can then generate client-side code using the protocol buffer compilers for the [supported languages](https://grpc.io/docs/languages/).
- **Java client-side programs only**: Download the `opensearch-protobufs` jar from the [Maven Central repository](https://repo1.maven.org/maven2/org/opensearch/protobufs/0.19.0).
- **Python client-side programs only**: Download the `opensearch-protobufs` package from the [PyPI repository](https://pypi.org/project/opensearch-protobufs/).
- **Python client-side programs only**: Download the `opensearch-protobufs` package from the [PyPI repository](https://pypi.org/project/opensearch-protobufs/0.19.0/).

## Supported APIs

Expand Down
62 changes: 62 additions & 0 deletions _api-reference/grpc-apis/search.md
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,65 @@ public class SearchClient {
}
```
{% include copy.html %}

## Python gRPC client example

The following example shows how to send the same request using a Python client application.

First, install the `opensearch-protobufs` package using `pip`:

```bash
pip install opensearch-protobufs==0.19.0
```
{% include copy.html %}

Use the following code to send the request:

```python
import grpc

from opensearch.protobufs.schemas import search_pb2
from opensearch.protobufs.schemas import common_pb2
from opensearch.protobufs.services.search_service_pb2_grpc import SearchServiceStub

channel = grpc.insecure_channel(
target="localhost:9400",
)

search_stub = SearchServiceStub(channel)

# Create a term query
term_query = common_pb2.TermQuery(
field="field",
value=common_pb2.FieldValue(string="value")
)
query_container = common_pb2.QueryContainer(term=term_query)

# Create a search request
request = search_pb2.SearchRequest(
request_body=search_pb2.SearchRequestBody(query=query_container),
index=["my-index"],
size=5
)

# Send request and handle response
try:
response = search_stub.Search(request=request)
if response.hits:
print("Found {} hits".format(response.hits.total))
print(response.hits)
elif response.timed_out or response.terminated_early:
print("Request timed out or terminated early")
elif response.x_shards.failed:
print("Some shards failed to execute the search")
print(response.x_shards.failures)
except grpc.RpcError as e:
if e.code() == StatusCode.UNAVAILABLE:
print("Failed to reach server: {}".format(e))
elif e.code() == StatusCode.PERMISSION_DENIED:
print("Permission denied: {}".format(e))
elif e.code() == StatusCode.INVALID_ARGUMENT:
print("Invalid argument: {}".format(e))
finally:
channel.close()
```
Loading