Skip to content

Commit

Permalink
Address code review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Andriy Redko <[email protected]>
  • Loading branch information
reta committed Apr 3, 2024
1 parent 965e26f commit 9ae2986
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 23 deletions.
35 changes: 33 additions & 2 deletions guides/generic.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.


Expand All @@ -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())) {
// ...
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,6 +44,14 @@ public static <C> 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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -125,33 +126,35 @@ 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(
Requests.builder()
.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()
)
Expand All @@ -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));
}
Expand Down

0 comments on commit 9ae2986

Please sign in to comment.