Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
48 changes: 48 additions & 0 deletions _api-reference/grpc-apis/bulk.md
Original file line number Diff line number Diff line change
Expand Up @@ -510,3 +510,51 @@
}
```
{% include copy.html %}

## Python gRPC client example

The following example gives the same request for a Python client side application. The opensearch-protobufs package is available for download with pip.

Check failure on line 516 in _api-reference/grpc-apis/bulk.md

View workflow job for this annotation

GitHub Actions / style-job

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'OpenSearch' instead of 'opensearch'. Raw Output: {"message": "[Vale.Terms] Use 'OpenSearch' instead of 'opensearch'.", "location": {"path": "_api-reference/grpc-apis/bulk.md", "range": {"start": {"line": 516, "column": 88}}}, "severity": "ERROR"}

```
pip install opensearch-protobufs==0.19.0
```

```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)

# Create index operation
index_op = document_pb2.OperationContainer()
index_op.index.CopyFrom(document_pb2.IndexOperation())

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

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

# Send request
response = document_stub.Bulk(request)
channel.close()

# Handle the response
if response.errors:
print("Response items contain errors.")
for item in response.items:
print(item)

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

## Python gRPC client example

The following example gives the same request for a Python client side application. The opensearch-protobufs package is available for download with pip.

Check failure on line 467 in _api-reference/grpc-apis/search.md

View workflow job for this annotation

GitHub Actions / style-job

[vale] reported by reviewdog 🐶 [Vale.Terms] Use 'OpenSearch' instead of 'opensearch'. Raw Output: {"message": "[Vale.Terms] Use 'OpenSearch' instead of 'opensearch'.", "location": {"path": "_api-reference/grpc-apis/search.md", "range": {"start": {"line": 467, "column": 88}}}, "severity": "ERROR"}

```
pip install opensearch-protobufs==0.19.0
```

```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
response = search_stub.Search(request)

# Handle the response
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)

channel.close()
```
Loading