From 9ae2986ee7eaf4b826faff2e4fd7e74eb90df3cd Mon Sep 17 00:00:00 2001 From: Andriy Redko Date: Wed, 3 Apr 2024 15:42:38 -0400 Subject: [PATCH] Address code review comments Signed-off-by: Andriy Redko --- guides/generic.md | 35 ++++++++++++++- .../client/opensearch/generic/Bodies.java | 10 +++++ .../client/opensearch/generic/Requests.java | 12 +++++ .../integTest/AbstractGenericClientIT.java | 45 ++++++++++--------- 4 files changed, 79 insertions(+), 23 deletions(-) diff --git a/guides/generic.md b/guides/generic.md index af8a7794d4..f9ee7ff5fa 100644 --- a/guides/generic.md +++ b/guides/generic.md @@ -2,7 +2,8 @@ - [Getting the Client](#get-client) - [Sending Simple Requests](#request-bodyless) - [Sending JSON Requests](#request-json) - - [Sending JSON using POJOs](#request-pojo) + - [Sending JSON Requests using POJOs](#request-pojo) + - [Sending Requests using structured JSON](#request-structured) # Generic Client @@ -66,7 +67,7 @@ The following sample code a simple request with JSON body. } ``` -## Sending JSON using POJOs +## Sending JSON Requests using POJOs Besides providing the ability to deal with raw request and response payloads (bodies), the `OpenSearchGenericClient` could be used mixed with existing OpenSearch typed requests and responses (POJOs), like the following sample code demonstrates. @@ -93,4 +94,34 @@ try (Response response = javaClient().generic() .orElse(null); // ... } +``` + +## Sending Requests using structured JSON +Dealing with strings or POJOs could be daunting sometimes, using structured JSON APIs is a middle ground of both approaches, as per following sample code that uses (`jakarta.json.Json`)[https://jakarta.ee/specifications/jsonp]. + +```java +try (Response response = javaClient().generic() + .execute( + Requests.builder() + .endpoint("/" + index) + .method("PUT") + .json(Json.createObjectBuilder() + .add("settings", Json.createObjectBuilder() + .add("index", Json.createObjectBuilder() + .add("sort.field", "name")) + .add("sort.order", "asc") + ) + .add("mappings",Json.createObjectBuilder() + .add("properties", Json.createObjectBuilder() + .add("name", Json.createObjectBuilder() + .add("type", "keyword")) + .add("doc_values", true) + .add("size", Json.createObjectBuilder() + .add("type", "keyword")) + .add("doc_values", true)) + ) + ) + .build())) { + // ... +} ``` \ No newline at end of file diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/generic/Bodies.java b/java-client/src/main/java/org/opensearch/client/opensearch/generic/Bodies.java index 76ba65cd4d..96dafca2db 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/generic/Bodies.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/generic/Bodies.java @@ -8,6 +8,8 @@ package org.opensearch.client.opensearch.generic; +import jakarta.json.JsonObject; +import jakarta.json.JsonObjectBuilder; import jakarta.json.stream.JsonGenerator; import jakarta.json.stream.JsonParser; import java.io.ByteArrayOutputStream; @@ -42,6 +44,14 @@ public static Body json(C value, JsonpMapper jsonpMapper) throws IOException } } + public static Body json(final JsonObjectBuilder builder) { + return json(builder.build()); + } + + public static Body json(final JsonObject json) { + return json(json.toString()); + } + public static Body json(String str) { return Body.from(str.getBytes(StandardCharsets.UTF_8), APPLICATION_JSON); } diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/generic/Requests.java b/java-client/src/main/java/org/opensearch/client/opensearch/generic/Requests.java index 9d4910dcfc..e9a39871ea 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/generic/Requests.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/generic/Requests.java @@ -1,5 +1,7 @@ package org.opensearch.client.opensearch.generic; +import jakarta.json.JsonObject; +import jakarta.json.JsonObjectBuilder; import java.io.IOException; import java.util.Collection; import java.util.Collections; @@ -63,6 +65,16 @@ public JsonBodyBuilder body(final Body body) { return this; } + public JsonBodyBuilder json(final JsonObjectBuilder builder) { + this.body = Bodies.json(builder); + return this; + } + + public JsonBodyBuilder json(final JsonObject json) { + this.body = Bodies.json(json); + return this; + } + public JsonBodyBuilder json(String str) { this.body = Bodies.json(str); return this; diff --git a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractGenericClientIT.java b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractGenericClientIT.java index 76211659c1..bf3900bfb8 100644 --- a/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractGenericClientIT.java +++ b/java-client/src/test/java11/org/opensearch/client/opensearch/integTest/AbstractGenericClientIT.java @@ -10,6 +10,7 @@ import static org.hamcrest.CoreMatchers.equalTo; +import jakarta.json.Json; import java.io.IOException; import java.util.Arrays; import java.util.stream.Collectors; @@ -125,6 +126,8 @@ private void createIndex(String index) throws IOException { } private void createIndexUntyped(String index) throws IOException { + final JsonpMapper jsonpMapper = javaClient()._transport().jsonpMapper(); + try ( Response response = javaClient().generic() .execute( @@ -132,26 +135,26 @@ private void createIndexUntyped(String index) throws IOException { .endpoint("/" + index) .method("PUT") .json( - "{" - + " \"settings\": {" - + " \"index\": {" - + " \"sort.field\": \"name\"," - + " \"sort.order\": \"asc\"" - + " }" - + " }," - + " \"mappings\": {" - + " \"properties\": {" - + " \"name\": {" - + " \"type\": \"keyword\"," - + " \"doc_values\": true" - + " }," - + " \"size\": {" - + " \"type\": \"keyword\"," - + " \"doc_values\": true" - + " }" - + " }" - + " }" - + "}" + Json.createObjectBuilder() + .add( + "settings", + Json.createObjectBuilder() + .add("index", Json.createObjectBuilder().add("sort.field", "name")) + .add("sort.order", "asc") + ) + .add( + "mappings", + Json.createObjectBuilder() + .add( + "properties", + Json.createObjectBuilder() + .add("name", Json.createObjectBuilder().add("type", "keyword")) + .add("doc_values", true) + + .add("size", Json.createObjectBuilder().add("type", "keyword")) + .add("doc_values", true) + ) + ) ) .build() ) @@ -160,7 +163,7 @@ private void createIndexUntyped(String index) throws IOException { assertThat(response.getBody().isPresent(), equalTo(true)); final CreateIndexResponse r = response.getBody() - .map(b -> Bodies.json(b, CreateIndexResponse._DESERIALIZER, javaClient()._transport().jsonpMapper())) + .map(b -> Bodies.json(b, CreateIndexResponse._DESERIALIZER, jsonpMapper)) .orElse(null); assertThat(r.acknowledged(), equalTo(true)); }