Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Backport] [2.x] Add missed fields to MultisearchBody: ext, rescore and to SearchRequest: ext (#918) #919

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
- Add xy_shape property ([#884](https://github.com/opensearch-project/opensearch-java/pull/885))
- 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 @@ -58,6 +59,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 @@ -222,6 +224,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 @@ -278,6 +282,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 @@ -820,6 +825,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 @@ -1049,6 +1061,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 @@ -1103,7 +1126,8 @@ public Builder toBuilder() {
.timeout(timeout)
.trackScores(trackScores)
.trackTotalHits(trackTotalHits)
.version(version);
.version(version)
.ext(ext);
}

// ---------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -1269,6 +1293,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 @@ -2151,6 +2178,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 @@ -2210,6 +2257,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,10 +1,22 @@
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.model.ModelTestCase;

public class SearchRequestTest extends ModelTestCase {

@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
Loading