Skip to content

Commit 4d511f2

Browse files
karenyrxnatebowerkolchfa-aws
authored andcommitted
[GRPC] Update gRPC usage instructions and fix java examples (opensearch-project#11199)
* [GRPC] Update gRPC usage instructions and fix java examples to adhere to 0.19.0 protos Signed-off-by: Karen X <karenxyr@gmail.com> * fix links Signed-off-by: Karen X <karenxyr@gmail.com> * update bulk Signed-off-by: Karen X <karenxyr@gmail.com> * update Signed-off-by: Karen X <karenxyr@gmail.com> * Apply suggestions from code review Co-authored-by: kolchfa-aws <105444904+kolchfa-aws@users.noreply.github.com> Signed-off-by: Nathan Bower <nbower@amazon.com> --------- Signed-off-by: Karen X <karenxyr@gmail.com> Signed-off-by: Nathan Bower <nbower@amazon.com> Co-authored-by: Nathan Bower <nbower@amazon.com> Co-authored-by: kolchfa-aws <105444904+kolchfa-aws@users.noreply.github.com> Signed-off-by: etgraylog <evan.tepsic@graylog.com>
1 parent 116162e commit 4d511f2

File tree

5 files changed

+114
-56
lines changed

5 files changed

+114
-56
lines changed

_api-reference/grpc-apis/bulk.md

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ The gRPC Bulk API provides an efficient, binary-encoded alternative to the [HTTP
1313

1414
## Prerequisite
1515

16-
To submit gRPC requests, you must have a set of protobufs on the client side. For ways to obtain the protobufs, see [Using gRPC APIs]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/index/#using-grpc-apis).
16+
To submit gRPC requests, you must have a set of protobufs on the client side. For ways to obtain the protobufs, see [Using gRPC APIs]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/index/#how-to-use-grpc-apis).
1717

1818
## gRPC service and method
1919

2020
gRPC Document APIs reside in the [DocumentService](https://github.com/opensearch-project/opensearch-protobufs/blob/0.19.0/protos/services/document_service.proto#L22).
2121

22-
You can submit bulk requests by invoking the [`Bulk`](https://github.com/opensearch-project/opensearch-protobufs/blob/0.19.0/protos/services/document_service.proto#L24) gRPC method within the `DocumentService`. The method takes a [`BulkRequest`](#bulkrequest-fields) and returns a [`BulkResponse`](#bulkresponsebody-fields).
22+
You can submit bulk requests by invoking the [`Bulk`](https://github.com/opensearch-project/opensearch-protobufs/blob/0.19.0/protos/services/document_service.proto#L24) gRPC method within the `DocumentService`. The method takes a [`BulkRequest`](#bulkrequest-fields) and returns a [`BulkResponse`](#bulkresponse-fields).
2323

2424
## Document format
2525

@@ -318,9 +318,9 @@ The following example shows a bulk request with a `script` operation. It increme
318318

319319
The gRPC Bulk API provides the following response fields.
320320

321-
### BulkResponseBody fields
321+
### BulkResponse fields
322322

323-
The [`BulkResponse`](https://github.com/opensearch-project/opensearch-protobufs/blob/0.19.0/protos/schemas/document.proto#L188) message wraps either a `BulkResponseBody` for successful requests or a `BulkErrorResponse` for failed requests. The `BulkResponseBody` provides a summary and per-item result of a bulk operation and contains the following fields.
323+
The [`BulkResponse`](https://github.com/opensearch-project/opensearch-protobufs/blob/0.19.0/protos/schemas/document.proto#L186) message is returned directly from the `Bulk` gRPC method and provides a summary and per-item result of a bulk operation. It contains the following fields.
324324

325325
| Field | Protobuf type | Description |
326326
| :---- | :---- | :---- |
@@ -475,23 +475,23 @@ public class BulkClient {
475475

476476
// Create an index operation
477477
IndexOperation indexOp = IndexOperation.newBuilder()
478-
.setIndex("my-index")
479-
.setId("1")
478+
.setXIndex("my-index")
479+
.setXId("1")
480480
.build();
481481

482482
BulkRequestBody indexBody = BulkRequestBody.newBuilder()
483-
.setIndex(indexOp)
484-
.setDoc(ByteString.copyFromUtf8("{\"field\": \"value\"}"))
483+
.setOperationContainer(OperationContainer.newBuilder().setIndex(indexOp).build())
484+
.setObject(ByteString.copyFromUtf8("{\"field\": \"value\"}"))
485485
.build();
486486

487487
// Create a delete operation
488488
DeleteOperation deleteOp = DeleteOperation.newBuilder()
489-
.setIndex("my-index")
490-
.setId("2")
489+
.setXIndex("my-index")
490+
.setXId("2")
491491
.build();
492492

493493
BulkRequestBody deleteBody = BulkRequestBody.newBuilder()
494-
.setDelete(deleteOp)
494+
.setOperationContainer(OperationContainer.newBuilder().setDelete(deleteOp).build())
495495
.build();
496496

497497
// Build the bulk request
@@ -502,8 +502,32 @@ public class BulkClient {
502502
.build();
503503

504504
// Execute the bulk request
505-
BulkResponse response = stub.bulk(request);
506-
System.out.println("Bulk errors: " + response.getBulkResponseBody().getErrors());
505+
try {
506+
BulkResponse response = stub.bulk(request);
507+
508+
// Handle the response
509+
System.out.println("Bulk errors: " + response.getErrors());
510+
System.out.println("Bulk took: " + response.getTook() + " ms");
511+
if (response.hasIngestTook()) {
512+
System.out.println("Ingest took: " + response.getIngestTook() + " ms");
513+
}
514+
515+
// Process individual items
516+
for (Item item : response.getItemsList()) {
517+
if (item.hasIndex()) {
518+
System.out.println("Index operation: " + item.getIndex().getStatus());
519+
} else if (item.hasDelete()) {
520+
System.out.println("Delete operation: " + item.getDelete().getStatus());
521+
} else if (item.hasCreate()) {
522+
System.out.println("Create operation: " + item.getCreate().getStatus());
523+
} else if (item.hasUpdate()) {
524+
System.out.println("Update operation: " + item.getUpdate().getStatus());
525+
}
526+
}
527+
} catch (io.grpc.StatusRuntimeException e) {
528+
System.err.println("gRPC request failed with status: " + e.getStatus());
529+
System.err.println("Error message: " + e.getMessage());
530+
}
507531

508532
channel.shutdown();
509533
}

_api-reference/grpc-apis/index.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,30 @@ redirect_from:
1818
Starting with OpenSearch version 3.2, the gRPC [Bulk API]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/bulk/) and [k-NN search queries]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/knn/) are generally available. These use [protobuf version 0.19.0](https://github.com/opensearch-project/opensearch-protobufs/releases/tag/0.19.0). However, expect updates to the protobuf structure as the feature matures in upcoming versions. Other gRPC search functionality remains experimental and not recommended for production use. For updates on the progress of these features or to leave feedback, see the associated [GitHub issue](https://github.com/opensearch-project/OpenSearch/issues/16787).
1919
{: .note}
2020

21-
The OpenSearch gRPC functionality provides an alternative, high-performance transport layer using [gRPC](https://grpc.io/) for communication with OpenSearch. It uses protocol buffers over gRPC for lower overhead and faster serialization. This reduces overhead, speeds up serialization, and improves request-side latency, based on initial benchmarking results.
21+
The OpenSearch gRPC functionality provides an alternative, high-performance transport layer using [gRPC](https://grpc.io/) for communication with OpenSearch. It uses protocol buffers over gRPC for lower overhead and faster serialization. This reduces overhead, speeds up serialization, and improves request-side latency, based on initial benchmarking results. For more information, see [Performance Benefits](#grpc-performance-benefits).
2222

23-
The primary goal of gRPC support is to:
23+
## Supported APIs
2424

25-
* Offer a **binary-encoded** alternative to HTTP/REST-based communication.
26-
* **Improve performance** for bulk workloads and large-scale ingestion scenarios.
27-
* **Enable more efficient client integrations** across languages, like Java, Go, and Python, using native gRPC stubs.
25+
The following gRPC APIs are currently supported:
26+
- [Bulk]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/bulk/) **Generally available 3.2**
27+
- [k-NN]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/knn/) **Generally available 3.2**
28+
- [Search]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/search/) (for select query types)
2829

29-
## Performance benefits
30+
## How to use gRPC APIs
3031

31-
Using gRPC APIs provides several advantages over HTTP APIs:
32+
To use gRPC APIs, follow these steps:
33+
1. Enable gRPC transport by configuring the necessary [gRPC settings](#grpc-settings).
34+
35+
2. To submit gRPC requests, you must have a set of protobufs on the client side. You can obtain the protobufs in the following ways.
36+
37+
| Language | Distribution method | Instructions |
38+
| :------- | :------------------ | :----------- |
39+
| Java | Maven Central repository | Download the `opensearch-protobufs` jar from the [Maven Central repository](https://repo1.maven.org/maven2/org/opensearch/protobufs/0.19.0). |
40+
| Python | PyPI repository | Download the `opensearch-protobufs` package from the [PyPI repository](https://pypi.org/project/opensearch-protobufs/0.19.0). |
41+
| Other languages | GitHub repository (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/). |
3242

33-
- **Reduced latency**: Binary protocol buffers eliminate JSON parsing overhead.
34-
- **Higher throughput**: More efficient network utilization for high-frequency queries.
35-
- **Lower CPU usage**: Reduced serialization and deserialization costs.
36-
- **Type safety**: Protocol buffer schemas provide compile-time validation.
37-
- **Smaller payload sizes**: Binary encoding reduces network traffic.
3843

39-
## Enabling gRPC APIs
44+
## gRPC settings
4045

4146
The `transport-grpc` module is included by default with OpenSearch installations. To enable it, add the following settings to `opensearch.yml`:
4247

@@ -63,7 +68,7 @@ grpc.bind_host: 0.0.0.0
6368
```
6469
{% include copy.html %}
6570
66-
## Advanced gRPC settings
71+
### Advanced gRPC settings
6772
6873
OpenSearch supports the following advanced settings for gRPC communication. These settings can be configured in `opensearch.yml`.
6974

@@ -106,18 +111,16 @@ grpc.netty.max_msg_size: 10mb
106111

107112
These settings are similar to the [HTTP Network settings]({{site.url}}{{site.baseurl}}/install-and-configure/configuring-opensearch/network-settings/#advanced-http-settings) but specifically apply to gRPC communication.
108113

109-
## Using gRPC APIs
110114

111-
To submit gRPC requests, you must have a set of protobufs on the client side. You can obtain the protobufs in the following ways:
115+
## gRPC performance benefits
112116

113-
- **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/).
114-
- **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).
115-
- **Python client-side programs only**: Download the `opensearch-protobufs` package from the [PyPI repository](https://pypi.org/project/opensearch-protobufs/0.19.0/).
116-
117-
## Supported APIs
117+
Using gRPC APIs provides several advantages over HTTP APIs:
118118

119-
The following gRPC APIs are supported:
119+
- **Reduced latency**: Binary protocol buffers eliminate JSON parsing overhead.
120+
- **Higher throughput**: More efficient network utilization for high-frequency queries.
121+
- **Lower CPU usage**: Reduced serialization and deserialization costs.
122+
- **Type safety**: Protocol buffer schemas provide compile-time validation.
123+
- **Smaller payload sizes**: Binary encoding reduces network traffic.
120124

121-
- [Bulk]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/bulk/) **Generally available 3.2**
122-
- [k-NN]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/knn/) **Generally available 3.2**
123-
- [Search]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/search/) (for select query types)
125+
### Additional performance tip
126+
Indexing documents as supported binary formats, such as SMILE, will usually incur lower latency than indexing/searching for them as JSON. Both indexing and searching latency should be reduced.

_api-reference/grpc-apis/knn.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ For information about HTTP-based k-NN queries, see [k-NN query]({{site.url}}{{si
1818

1919
## Prerequisite
2020

21-
To submit gRPC requests, you must have a set of protobufs on the client side. For ways to obtain the protobufs, see [Using gRPC APIs]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/index/#using-grpc-apis).
21+
To submit gRPC requests, you must have a set of protobufs on the client side. For ways to obtain the protobufs, see [Using gRPC APIs]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/index/#how-to-use-grpc-apis).
2222

2323
## gRPC service and method
2424

@@ -105,9 +105,28 @@ public class KnnGrpcClient {
105105
.build();
106106

107107
// Execute the search
108-
SearchResponse response = searchStub.search(request);
109-
110-
System.out.println("Found " + response.getResponseBody().getHits().getHitsCount() + " results");
108+
try {
109+
SearchResponse response = searchStub.search(request);
110+
111+
// Handle the response
112+
System.out.println("Search took: " + response.getTook() + " ms");
113+
114+
HitsMetadata hits = response.getHits();
115+
if (hits.hasTotal()) {
116+
System.out.println("Found " + hits.getTotal().getTotalHits().getValue() + " results");
117+
}
118+
119+
// Process k-NN results with similarity scores
120+
for (HitsMetadataHitsInner hit : hits.getHitsList()) {
121+
System.out.println("Document ID: " + hit.getXId());
122+
if (hit.hasXScore()) {
123+
System.out.println("Similarity score: " + hit.getXScore().getDouble());
124+
}
125+
}
126+
} catch (io.grpc.StatusRuntimeException e) {
127+
System.err.println("gRPC k-NN search request failed with status: " + e.getStatus());
128+
System.err.println("Error message: " + e.getMessage());
129+
}
111130

112131
channel.shutdown();
113132
}
@@ -140,4 +159,4 @@ For complex filtering requirements, consider using the HTTP k-NN API, simplifyin
140159
- Learn more about [vector search in OpenSearch]({{site.url}}{{site.baseurl}}/search-plugins/knn/index/).
141160
- Explore [k-NN index settings]({{site.url}}{{site.baseurl}}/search-plugins/knn/knn-index/).
142161
- Review [performance tuning for k-NN]({{site.url}}{{site.baseurl}}/search-plugins/knn/performance-tuning/).
143-
- Read about [gRPC configuration]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/index/#enabling-grpc-apis).
162+
- Read about [gRPC configuration]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/index/#grpc-settings).

_api-reference/grpc-apis/search.md

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The gRPC Search API provides a performant, binary interface for running [queries
1717

1818
## Prerequisite
1919

20-
To submit gRPC requests, you must have a set of protobufs on the client side. For ways to obtain the protobufs, see [Using gRPC APIs]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/index/#using-grpc-apis).
20+
To submit gRPC requests, you must have a set of protobufs on the client side. For ways to obtain the protobufs, see [Using gRPC APIs]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/index/#how-to-use-grpc-apis).
2121

2222
## gRPC service and method
2323

@@ -443,17 +443,29 @@ public class SearchClient {
443443
.setRequestBody(requestBody)
444444
.build();
445445

446-
SearchResponse response = stub.search(request);
447-
448-
// Handle the response
449-
if (response.hasResponseBody()) {
450-
ResponseBody responseBody = response.getResponseBody();
451-
HitsMetadata hits = responseBody.getHits();
452-
System.out.println("Found hits: " + hits.getTotal().getTotalHits().getValue());
453-
} else if (response.hasError4XxResponse()) {
454-
System.out.println("4xx Error: " + response.getError4XxResponse().getError());
455-
} else if (response.hasError5XxResponse()) {
456-
System.out.println("5xx Error: " + response.getError5XxResponse().getMessage());
446+
try {
447+
SearchResponse response = stub.search(request);
448+
449+
// Handle the response
450+
System.out.println("Search took: " + response.getTook() + " ms");
451+
System.out.println("Timed out: " + response.getTimedOut());
452+
453+
HitsMetadata hits = response.getHits();
454+
if (hits.hasTotal()) {
455+
System.out.println("Total hits: " + hits.getTotal().getTotalHits().getValue());
456+
}
457+
458+
// Process individual hits
459+
for (HitsMetadataHitsInner hit : hits.getHitsList()) {
460+
System.out.println("Hit ID: " + hit.getXId());
461+
System.out.println("Hit Index: " + hit.getXIndex());
462+
if (hit.hasXScore()) {
463+
System.out.println("Score: " + hit.getXScore().getDouble());
464+
}
465+
}
466+
} catch (io.grpc.StatusRuntimeException e) {
467+
System.err.println("gRPC search request failed with status: " + e.getStatus());
468+
System.err.println("Error message: " + e.getMessage());
457469
}
458470

459471
channel.shutdown();

_security/configuration/tls.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ You should receive the following response:
315315
gRPC supports encryption in transit only. Trust stores and certificates configured as root CAs in PEM format are used only for the purpose of TLS client authorization. Role-based access is not available for gRPC endpoints.
316316
{: .warning}
317317

318-
You can configure TLS on the optional gRPC transport in `opensearch.yml`. For more information about using the gRPC plugin, see [Enabling gRPC APIs]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/index/#enabling-grpc-apis).
318+
You can configure TLS on the optional gRPC transport in `opensearch.yml`. For more information about using the gRPC plugin, see [Enabling gRPC APIs]({{site.url}}{{site.baseurl}}/api-reference/grpc-apis/index/#grpc-settings).
319319

320320
### PEM key settings (X.509 PEM certificates and PKCS #8 keys)
321321

0 commit comments

Comments
 (0)