Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into analyzers-for-extra…
Browse files Browse the repository at this point in the history
…-languages
  • Loading branch information
brentam committed Dec 30, 2023
2 parents f3412c6 + 5239303 commit 4b8abd2
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 28 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Bumps `org.apache.httpcomponents.core5:httpcore5-h2` from 5.2.3 to 5.2.4
- Bumps `org.apache.httpcomponents.client5:httpclient5` from 5.2.1 to 5.2.3
- Bumps `org.apache.httpcomponents.core5:httpcore5` from 5.2.3 to 5.2.4
- Bumps `com.carrotsearch.randomizedtesting:randomizedtesting-runner` from 2.7.1 to 2.8.1
- Bumps `org.hamcrest:hamcrest` from 2.1 to 2.2

This section is for maintaining a changelog for all breaking changes for the client that cannot be released in the 2.x line. All other non-breaking changes should be added to [Unreleased 2.x] section.

Expand All @@ -21,6 +23,7 @@ This section is for maintaining a changelog for all breaking changes for the cli

### Changed
- Migrate client transports to Apache HttpClient / Core 5.x ([#246](https://github.com/opensearch-project/opensearch-java/pull/246))
- Changed SearchAfter of SearchRequest type to FieldValue instead of String ([#769](https://github.com/opensearch-project/opensearch-java/pull/769))

### Deprecated
- Deprecate RestClientTransport ([#536](https://github.com/opensearch-project/opensearch-java/pull/536))
Expand All @@ -31,6 +34,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
- Fix version and build ([#254](https://github.com/opensearch-project/opensearch-java/pull/254))
- Fix PutTemplateRequest field deserialization ([#723](https://github.com/opensearch-project/opensearch-java/pull/723))
- Fix PutIndexTemplateRequest field deserialization ([#765](https://github.com/opensearch-project/opensearch-java/pull/765))
- Fix InnerHits storedFields deserialization/serialization ([#781](https://github.com/opensearch-project/opensearch-java/pull/781)

### Security

Expand Down
20 changes: 20 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# UPGRADING

## [UPGRADING 2.x to 3.0]
### SearchAfter of SearchRequest type
- Changed SearchAfter of SearchRequest type to FieldValue instead of String ([#769](https://github.com/opensearch-project/opensearch-java/pull/769))
- Consider using `FieldValue.of` to make string type values compatible.

Before:
```
.searchAfter("string")
.searchAfter("string1", "string2")
.searchAfter(List.of("String"))
```

After:
```
.searchAfter(FieldValue.of("string"))
.searchAfter(FieldValue.of("string1"), FieldValue.of("string2"))
.searchAfter(List.of(FieldValue.of("String")))
```
4 changes: 2 additions & 2 deletions java-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ dependencies {

// Apache 2.0
compileOnly("org.opensearch.client", "opensearch-rest-client", opensearchVersion)
testImplementation("org.hamcrest:hamcrest:2.1")
testImplementation("com.carrotsearch.randomizedtesting:randomizedtesting-runner:2.7.1") {
testImplementation("org.hamcrest:hamcrest:2.2")
testImplementation("com.carrotsearch.randomizedtesting:randomizedtesting-runner:2.8.1") {
exclude(group = "junit")
}
testImplementation("org.opensearch.client", "opensearch-rest-client", opensearchVersion)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.opensearch.client.json.ObjectDeserializer;
import org.opensearch.client.opensearch._types.ErrorResponse;
import org.opensearch.client.opensearch._types.ExpandWildcard;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch._types.RequestBase;
import org.opensearch.client.opensearch._types.ScriptField;
import org.opensearch.client.opensearch._types.SearchType;
Expand Down Expand Up @@ -184,7 +185,7 @@ public class SearchRequest extends RequestBase implements JsonpSerializable {
@Nullable
private final Time scroll;

private final List<String> searchAfter;
private final List<FieldValue> searchAfter;

@Nullable
private final SearchType searchType;
Expand Down Expand Up @@ -678,7 +679,7 @@ public final Time scroll() {
/**
* API name: {@code search_after}
*/
public final List<String> searchAfter() {
public final List<FieldValue> searchAfter() {
return this.searchAfter;
}

Expand Down Expand Up @@ -966,8 +967,8 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
if (ApiTypeHelper.isDefined(this.searchAfter)) {
generator.writeKey("search_after");
generator.writeStartArray();
for (String item0 : this.searchAfter) {
generator.write(item0);
for (FieldValue item0 : this.searchAfter) {
item0.serialize(generator, mapper);

}
generator.writeEnd();
Expand Down Expand Up @@ -1173,7 +1174,7 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<S
private Time scroll;

@Nullable
private List<String> searchAfter;
private List<FieldValue> searchAfter;

@Nullable
private SearchType searchType;
Expand Down Expand Up @@ -1860,7 +1861,7 @@ public final Builder scroll(Function<Time.Builder, ObjectBuilder<Time>> fn) {
* <p>
* Adds all elements of <code>list</code> to <code>searchAfter</code>.
*/
public final Builder searchAfter(List<String> list) {
public final Builder searchAfter(List<FieldValue> list) {
this.searchAfter = _listAddAll(this.searchAfter, list);
return this;
}
Expand All @@ -1870,7 +1871,7 @@ public final Builder searchAfter(List<String> list) {
* <p>
* Adds one or more values to <code>searchAfter</code>.
*/
public final Builder searchAfter(String value, String... values) {
public final Builder searchAfter(FieldValue value, FieldValue... values) {
this.searchAfter = _listAdd(this.searchAfter, value, values);
return this;
}
Expand Down Expand Up @@ -2142,7 +2143,7 @@ protected static void setupSearchRequestDeserializer(ObjectDeserializer<SearchRe
op.add(Builder::rescore, JsonpDeserializer.arrayDeserializer(Rescore._DESERIALIZER), "rescore");
op.add(Builder::runtimeMappings, JsonpDeserializer.stringMapDeserializer(RuntimeField._DESERIALIZER), "runtime_mappings");
op.add(Builder::scriptFields, JsonpDeserializer.stringMapDeserializer(ScriptField._DESERIALIZER), "script_fields");
op.add(Builder::searchAfter, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()), "search_after");
op.add(Builder::searchAfter, JsonpDeserializer.arrayDeserializer(FieldValue._DESERIALIZER), "search_after");
op.add(Builder::seqNoPrimaryTerm, JsonpDeserializer.booleanDeserializer(), "seq_no_primary_term");
op.add(Builder::size, JsonpDeserializer.integerDeserializer(), "size");
op.add(Builder::slice, SlicedScroll._DESERIALIZER, "slice");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public class InnerHits implements JsonpSerializable {
@Nullable
private final SourceConfig source;

private final List<String> storedField;
private final List<String> storedFields;

@Nullable
private final Boolean trackScores;
Expand All @@ -114,7 +114,7 @@ private InnerHits(Builder builder) {
this.fields = ApiTypeHelper.unmodifiable(builder.fields);
this.sort = ApiTypeHelper.unmodifiable(builder.sort);
this.source = builder.source;
this.storedField = ApiTypeHelper.unmodifiable(builder.storedField);
this.storedFields = ApiTypeHelper.unmodifiable(builder.storedFields);
this.trackScores = builder.trackScores;
this.version = builder.version;

Expand Down Expand Up @@ -225,10 +225,10 @@ public final SourceConfig source() {
}

/**
* API name: {@code stored_field}
* API name: {@code stored_fields}
*/
public final List<String> storedField() {
return this.storedField;
public final List<String> storedFields() {
return this.storedFields;
}

/**
Expand Down Expand Up @@ -344,10 +344,10 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
this.source.serialize(generator, mapper);

}
if (ApiTypeHelper.isDefined(this.storedField)) {
generator.writeKey("stored_field");
if (ApiTypeHelper.isDefined(this.storedFields)) {
generator.writeKey("stored_fields");
generator.writeStartArray();
for (String item0 : this.storedField) {
for (String item0 : this.storedFields) {
generator.write(item0);

}
Expand Down Expand Up @@ -414,7 +414,7 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<I
private SourceConfig source;

@Nullable
private List<String> storedField;
private List<String> storedFields;

@Nullable
private Boolean trackScores;
Expand Down Expand Up @@ -623,22 +623,22 @@ public final Builder source(Function<SourceConfig.Builder, ObjectBuilder<SourceC
}

/**
* API name: {@code stored_field}
* API name: {@code stored_fields}
* <p>
* Adds all elements of <code>list</code> to <code>storedField</code>.
* Adds all elements of <code>list</code> to <code>storedFields</code>.
*/
public final Builder storedField(List<String> list) {
this.storedField = _listAddAll(this.storedField, list);
public final Builder storedFields(List<String> list) {
this.storedFields = _listAddAll(this.storedFields, list);
return this;
}

/**
* API name: {@code stored_field}
* API name: {@code stored_fields}
* <p>
* Adds one or more values to <code>storedField</code>.
* Adds one or more values to <code>storedFields</code>.
*/
public final Builder storedField(String value, String... values) {
this.storedField = _listAdd(this.storedField, value, values);
public final Builder storedFields(String value, String... values) {
this.storedFields = _listAdd(this.storedFields, value, values);
return this;
}

Expand Down Expand Up @@ -696,7 +696,7 @@ protected static void setupInnerHitsDeserializer(ObjectDeserializer<InnerHits.Bu
op.add(Builder::fields, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()), "fields");
op.add(Builder::sort, JsonpDeserializer.arrayDeserializer(SortOptions._DESERIALIZER), "sort");
op.add(Builder::source, SourceConfig._DESERIALIZER, "_source");
op.add(Builder::storedField, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()), "stored_field");
op.add(Builder::storedFields, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()), "stored_fields");
op.add(Builder::trackScores, JsonpDeserializer.booleanDeserializer(), "track_scores");
op.add(Builder::version, JsonpDeserializer.booleanDeserializer(), "version");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.opensearch.client.opensearch.core;

import org.junit.Test;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch.model.ModelTestCase;

public class SearchRequestTest extends ModelTestCase {
@Test
public void afterSearch() {
SearchRequest request = new SearchRequest.Builder().searchAfter(FieldValue.of(1), FieldValue.of("string")).build();

assertEquals("{\"search_after\":[1,\"string\"]}", toJson(request));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.opensearch.client.opensearch.core.search;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import jakarta.json.stream.JsonGenerator;
import jakarta.json.stream.JsonParser;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;
import org.junit.Test;
import org.opensearch.client.json.JsonpMapper;
import org.opensearch.client.json.JsonpSerializable;
import org.opensearch.client.json.jsonb.JsonbJsonpMapper;
import org.opensearch.client.opensearch.core.SearchRequest;

public class InnerHitsTest {
private final JsonpMapper mapper = new JsonbJsonpMapper();
private final String storedSalary = "details.salary";
private final String storedJobId = "details.jobId";

/**
* test if the json field for storedFields is generated with the correct name "stored_fields"
*/
@Test
public void testInnerHitStoredFields() {
InnerHits hits = InnerHits.of((it) -> it.storedFields(List.of("field1", "field2")));
assertTrue(toJson(hits).contains("stored_fields"));
}

/**
* test if the field "stored_fields" is present after deserialization/serialization
* of InnerHits
*/
@Test
public void testInnerHitFromParsed() {
JsonParser parser = mapper.jsonProvider().createParser(new StringReader(innerHitsJson));
InnerHits innerHits = InnerHits._DESERIALIZER.deserialize(parser, mapper);
assertThat(innerHits.storedFields(), containsInAnyOrder(storedJobId, storedSalary));
String actualJson = toJson(innerHits);
assertEquals(innerHitsJson, actualJson);

}

/**
* We test if the field "stored_fields" is present in the InnerHits after deserialization/serialization
* of a SearchRequest
*/
@Test
public void testRequestWithInnerHitFromParsed() {
JsonParser parser = mapper.jsonProvider().createParser(new StringReader(searchRequestJson));
SearchRequest searchRequest = SearchRequest._DESERIALIZER.deserialize(parser, mapper);
InnerHits innerHits = searchRequest.query().bool().must().get(1).nested().innerHits();
assertThat(innerHits.storedFields(), containsInAnyOrder(storedJobId, storedSalary));
String actualJson = toJson(searchRequest);
assertEquals(searchRequestJson, actualJson);
}

private String toJson(JsonpSerializable obj) {
StringWriter stringWriter = new StringWriter();
try (JsonGenerator generator = mapper.jsonProvider().createGenerator(stringWriter)) {
mapper.serialize(obj, generator);
}
return stringWriter.toString();
}

private final String innerHitsJson = String.format("{\"_source\":false,\"stored_fields\":[\"%s\",\"%s\"]}", storedJobId, storedSalary);
private final String searchRequestJson = String.format(
"{\"_source\":false,\"query\":{\"bool\":{\"must\":[{\"match_all\":{}},{\"nested\":{\"inner_hits\":%s,\"path\":\"details\","
+ "\"query\":{\"match_all\":{}}}}]}},\"stored_fields\":[\"title\",\"companyName\"]}",
innerHitsJson
);
}

0 comments on commit 4b8abd2

Please sign in to comment.