Skip to content

Commit

Permalink
Add missed fields to MultisearchBody: ext, rescore and to SearchReque…
Browse files Browse the repository at this point in the history
…st: ext (#918)

Signed-off-by: Andriy Redko <[email protected]>
  • Loading branch information
reta authored Apr 4, 2024
1 parent 0bf1810 commit 766b6ec
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ This section is for maintaining a changelog for all breaking changes for the cli
### Added
- Add missed fields to MultisearchBody: seqNoPrimaryTerm, storedFields, explain, fields, indicesBoost ([#914](https://github.com/opensearch-project/opensearch-java/pull/914))
- Add missed fields to MultisearchBody: collapse, version, timeout ([#916](https://github.com/opensearch-project/opensearch-java/pull/916)
- Add missed fields to MultisearchBody: ext, rescore and to SearchRequest: ext ([#918](https://github.com/opensearch-project/opensearch-java/pull/918)

### Dependencies
- Bumps `io.github.classgraph:classgraph` from 4.8.161 to 4.8.165
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.function.Function;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
Expand All @@ -59,6 +60,7 @@
import org.opensearch.client.opensearch._types.query_dsl.FieldAndFormat;
import org.opensearch.client.opensearch._types.query_dsl.Operator;
import org.opensearch.client.opensearch._types.query_dsl.Query;
import org.opensearch.client.opensearch.core.SearchTemplateRequest.Builder;
import org.opensearch.client.opensearch.core.search.FieldCollapse;
import org.opensearch.client.opensearch.core.search.Highlight;
import org.opensearch.client.opensearch.core.search.Pit;
Expand Down Expand Up @@ -223,6 +225,8 @@ public class SearchRequest extends RequestBase implements JsonpSerializable {
@Nullable
private final Boolean version;

private final Map<String, JsonData> ext;

// ---------------------------------------------------------------------------------------------

private SearchRequest(Builder builder) {
Expand Down Expand Up @@ -279,6 +283,7 @@ private SearchRequest(Builder builder) {
this.trackScores = builder.trackScores;
this.trackTotalHits = builder.trackTotalHits;
this.version = builder.version;
this.ext = ApiTypeHelper.unmodifiable(builder.ext);

}

Expand Down Expand Up @@ -821,6 +826,13 @@ public final Boolean version() {
return this.version;
}

/**
* API name: {@code ext}
*/
public final Map<String, JsonData> ext() {
return this.ext;
}

/**
* Serialize this object to JSON.
*/
Expand Down Expand Up @@ -1050,6 +1062,17 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {

}

if (ApiTypeHelper.isDefined(this.ext)) {
generator.writeKey("ext");
generator.writeStartObject();
for (Map.Entry<String, JsonData> item0 : this.ext.entrySet()) {
generator.writeKey(item0.getKey());
item0.getValue().serialize(generator, mapper);

}
generator.writeEnd();
}

}

public Builder toBuilder() {
Expand Down Expand Up @@ -1104,7 +1127,8 @@ public Builder toBuilder() {
.timeout(timeout)
.trackScores(trackScores)
.trackTotalHits(trackTotalHits)
.version(version);
.version(version)
.ext(ext);
}

// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1270,6 +1294,9 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<S
@Nullable
private Boolean version;

@Nullable
private Map<String, JsonData> ext;

/**
* Indicates which source fields are returned for matching documents. These
* fields are returned in the hits._source property of the search response.
Expand Down Expand Up @@ -2152,6 +2179,26 @@ public final Builder version(@Nullable Boolean value) {
return this;
}

/**
* API name: {@code ext}
* <p>
* Adds all entries of <code>map</code> to <code>ext</code>.
*/
public final Builder ext(Map<String, JsonData> map) {
this.ext = _mapPutAll(this.ext, map);
return this;
}

/**
* API name: {@code ext}
* <p>
* Adds an entry to <code>ext</code>.
*/
public final Builder ext(String key, JsonData value) {
this.ext = _mapPut(this.ext, key, value);
return this;
}

/**
* Builds a {@link SearchRequest}.
*
Expand Down Expand Up @@ -2211,6 +2258,7 @@ protected static void setupSearchRequestDeserializer(ObjectDeserializer<SearchRe
op.add(Builder::trackScores, JsonpDeserializer.booleanDeserializer(), "track_scores");
op.add(Builder::trackTotalHits, TrackHits._DESERIALIZER, "track_total_hits");
op.add(Builder::version, JsonpDeserializer.booleanDeserializer(), "version");
op.add(Builder::ext, JsonpDeserializer.stringMapDeserializer(JsonData._DESERIALIZER), "ext");

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.Map;
import java.util.function.Function;
import javax.annotation.Nullable;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.json.JsonpDeserializable;
import org.opensearch.client.json.JsonpDeserializer;
import org.opensearch.client.json.JsonpMapper;
Expand All @@ -51,6 +52,7 @@
import org.opensearch.client.opensearch.core.SearchRequest.Builder;
import org.opensearch.client.opensearch.core.search.FieldCollapse;
import org.opensearch.client.opensearch.core.search.Highlight;
import org.opensearch.client.opensearch.core.search.Rescore;
import org.opensearch.client.opensearch.core.search.SourceConfig;
import org.opensearch.client.opensearch.core.search.Suggester;
import org.opensearch.client.opensearch.core.search.TrackHits;
Expand Down Expand Up @@ -121,6 +123,10 @@ public class MultisearchBody implements JsonpSerializable {
@Nullable
private final String timeout;

private final List<Rescore> rescore;

private final Map<String, JsonData> ext;

// ---------------------------------------------------------------------------------------------

private MultisearchBody(Builder builder) {
Expand All @@ -147,6 +153,8 @@ private MultisearchBody(Builder builder) {
this.collapse = builder.collapse;
this.version = builder.version;
this.timeout = builder.timeout;
this.rescore = ApiTypeHelper.unmodifiable(builder.rescore);
this.ext = ApiTypeHelper.unmodifiable(builder.ext);
}

public static MultisearchBody of(Function<Builder, ObjectBuilder<MultisearchBody>> fn) {
Expand Down Expand Up @@ -341,6 +349,20 @@ public final String timeout() {
return this.timeout;
}

/**
* API name: {@code rescore}
*/
public final List<Rescore> rescore() {
return this.rescore;
}

/**
* API name: {@code ext}
*/
public final Map<String, JsonData> ext() {
return this.ext;
}

/**
* Serialize this object to JSON.
*/
Expand Down Expand Up @@ -511,6 +533,27 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
generator.writeKey("timeout");
generator.write(this.timeout);
}

if (ApiTypeHelper.isDefined(this.rescore)) {
generator.writeKey("rescore");
generator.writeStartArray();
for (Rescore item0 : this.rescore) {
item0.serialize(generator, mapper);

}
generator.writeEnd();
}

if (ApiTypeHelper.isDefined(this.ext)) {
generator.writeKey("ext");
generator.writeStartObject();
for (Map.Entry<String, JsonData> item0 : this.ext.entrySet()) {
generator.writeKey(item0.getKey());
item0.getValue().serialize(generator, mapper);

}
generator.writeEnd();
}
}

// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -585,6 +628,12 @@ public static class Builder extends ObjectBuilderBase implements ObjectBuilder<M
@Nullable
private String timeout;

@Nullable
private List<Rescore> rescore;

@Nullable
private Map<String, JsonData> ext;

/**
* API name: {@code aggregations}
* <p>
Expand Down Expand Up @@ -965,6 +1014,55 @@ public final Builder timeout(@Nullable String value) {
return this;
}

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

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

/**
* API name: {@code rescore}
* <p>
* Adds a value to <code>rescore</code> using a builder lambda.
*/
public final Builder rescore(Function<Rescore.Builder, ObjectBuilder<Rescore>> fn) {
return rescore(fn.apply(new Rescore.Builder()).build());
}

/**
* API name: {@code ext}
* <p>
* Adds all entries of <code>map</code> to <code>ext</code>.
*/
public final Builder ext(Map<String, JsonData> map) {
this.ext = _mapPutAll(this.ext, map);
return this;
}

/**
* API name: {@code ext}
* <p>
* Adds an entry to <code>ext</code>.
*/
public final Builder ext(String key, JsonData value) {
this.ext = _mapPut(this.ext, key, value);
return this;
}

/**
* Builds a {@link MultisearchBody}.
*
Expand Down Expand Up @@ -1016,6 +1114,8 @@ protected static void setupMultisearchBodyDeserializer(ObjectDeserializer<Multis
op.add(Builder::collapse, FieldCollapse._DESERIALIZER, "collapse");
op.add(Builder::version, JsonpDeserializer.booleanDeserializer(), "version");
op.add(Builder::timeout, JsonpDeserializer.stringDeserializer(), "timeout");
op.add(Builder::rescore, JsonpDeserializer.arrayDeserializer(Rescore._DESERIALIZER), "rescore");
op.add(Builder::ext, JsonpDeserializer.stringMapDeserializer(JsonData._DESERIALIZER), "ext");
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.opensearch.client.opensearch.core;

import java.util.Collections;
import org.junit.Test;
import org.opensearch.client.json.JsonData;
import org.opensearch.client.opensearch._types.FieldValue;
import org.opensearch.client.opensearch.model.ModelTestCase;

Expand All @@ -12,6 +14,16 @@ public void afterSearch() {
assertEquals("{\"search_after\":[1,\"string\"]}", toJson(request));
}

@Test
public void ext() {
SearchRequest request = new SearchRequest.Builder().ext(
"similarity",
JsonData.of(Collections.singletonMap("fields", Collections.singletonList("name")))
).build();

assertEquals("{\"ext\":{\"similarity\":{\"fields\":[\"name\"]}}}", toJson(request));
}

@Test
public void toBuilder() {
SearchRequest origin = new SearchRequest.Builder().index("index").build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;
import org.junit.Test;
import org.opensearch.Version;
Expand All @@ -18,6 +19,7 @@
import org.opensearch.client.opensearch._types.mapping.Property;
import org.opensearch.client.opensearch._types.query_dsl.MatchQuery;
import org.opensearch.client.opensearch._types.query_dsl.Query;
import org.opensearch.client.opensearch._types.query_dsl.QueryBuilders;
import org.opensearch.client.opensearch._types.query_dsl.TermQuery;
import org.opensearch.client.opensearch.core.SearchRequest;
import org.opensearch.client.opensearch.core.SearchResponse;
Expand Down Expand Up @@ -120,6 +122,22 @@ public void shouldReturnSearchResultsWithoutStoredFields() throws Exception {
assertNull(response.hits().hits().get(1).id());
}

@Test
public void shouldReturnSearchResultsWithExt() throws Exception {
final String index = "search_request";
createIndex(index);

final SearchRequest request = SearchRequest.of(
r -> r.index(index)
.sort(s -> s.field(f -> f.field("name").order(SortOrder.Asc)))
.query(b -> b.matchAll(QueryBuilders.matchAll().build()))
.ext(Map.of())
);

final SearchResponse<ShopItem> response = javaClient().search(request, ShopItem.class);
assertEquals(response.hits().hits().size(), 8);
}

private void createTestDocuments(String index) throws IOException {
javaClient().create(_1 -> _1.index(index).id("1").document(createItem("hummer", "huge", "yes", 2)));
javaClient().create(_1 -> _1.index(index).id("2").document(createItem("jammer", "huge", "yes", 1)));
Expand Down

0 comments on commit 766b6ec

Please sign in to comment.