Skip to content

Commit 5e2dbd2

Browse files
l-trottagithub-actions[bot]
authored andcommitted
support searchrequest compatibility for msearch body and script source (#1138)
1 parent 45a074e commit 5e2dbd2

3 files changed

Lines changed: 96 additions & 2 deletions

File tree

java-client/src/main/java/co/elastic/clients/elasticsearch/_types/ScriptSource.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package co.elastic.clients.elasticsearch._types;
2121

22+
import co.elastic.clients.elasticsearch.core.SearchRequest;
2223
import co.elastic.clients.elasticsearch.core.search.SearchRequestBody;
2324
import co.elastic.clients.json.JsonpDeserializable;
2425
import co.elastic.clients.json.JsonpDeserializer;
@@ -163,6 +164,24 @@ public ObjectBuilder<ScriptSource> scriptTemplate(SearchRequestBody v) {
163164
return this;
164165
}
165166

167+
public final ObjectBuilder<ScriptSource> scriptTemplate(SearchRequest value) {
168+
this._kind = Kind.ScriptTemplate;
169+
SearchRequestBody body = SearchRequestBody.of(srb -> srb.aggregations(value.aggregations())
170+
.collapse(value.collapse()).explain(value.explain()).ext(value.ext()).from(value.from())
171+
.highlight(value.highlight()).trackTotalHits(value.trackTotalHits())
172+
.indicesBoost(value.indicesBoost()).docvalueFields(value.docvalueFields()).knn(value.knn())
173+
.rank(value.rank()).minScore(value.minScore()).postFilter(value.postFilter())
174+
.profile(value.profile()).query(value.query()).rescore(value.rescore()).retriever(value.retriever())
175+
.scriptFields(value.scriptFields()).searchAfter(value.searchAfter()).size(value.size())
176+
.slice(value.slice()).sort(value.sort()).source(value.source()).fields(value.fields())
177+
.suggest(value.suggest()).terminateAfter(value.terminateAfter()).timeout(value.timeout())
178+
.trackScores(value.trackScores()).version(value.version())
179+
.seqNoPrimaryTerm(value.seqNoPrimaryTerm()).storedFields(value.storedFields()).pit(value.pit())
180+
.runtimeMappings(value.runtimeMappings()).stats(value.stats()));
181+
this._value = body;
182+
return this;
183+
}
184+
166185
public ObjectBuilder<ScriptSource> scriptTemplate(
167186
Function<SearchRequestBody.Builder, ObjectBuilder<SearchRequestBody>> fn) {
168187
return this.scriptTemplate(fn.apply(new SearchRequestBody.Builder()).build());

java-client/src/main/java/co/elastic/clients/elasticsearch/core/msearch/RequestItem.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package co.elastic.clients.elasticsearch.core.msearch;
2121

22+
import co.elastic.clients.elasticsearch.core.SearchRequest;
2223
import co.elastic.clients.elasticsearch.core.search.SearchRequestBody;
2324
import co.elastic.clients.json.JsonpDeserializable;
2425
import co.elastic.clients.json.JsonpDeserializer;
@@ -163,6 +164,23 @@ public final Builder body(Function<SearchRequestBody.Builder, ObjectBuilder<Sear
163164
return this.body(fn.apply(new SearchRequestBody.Builder()).build());
164165
}
165166

167+
public final Builder body(SearchRequest value) {
168+
SearchRequestBody body = SearchRequestBody.of(srb -> srb.aggregations(value.aggregations())
169+
.collapse(value.collapse()).explain(value.explain()).ext(value.ext()).from(value.from())
170+
.highlight(value.highlight()).trackTotalHits(value.trackTotalHits())
171+
.indicesBoost(value.indicesBoost()).docvalueFields(value.docvalueFields()).knn(value.knn())
172+
.rank(value.rank()).minScore(value.minScore()).postFilter(value.postFilter())
173+
.profile(value.profile()).query(value.query()).rescore(value.rescore()).retriever(value.retriever())
174+
.scriptFields(value.scriptFields()).searchAfter(value.searchAfter()).size(value.size())
175+
.slice(value.slice()).sort(value.sort()).source(value.source()).fields(value.fields())
176+
.suggest(value.suggest()).terminateAfter(value.terminateAfter()).timeout(value.timeout())
177+
.trackScores(value.trackScores()).version(value.version())
178+
.seqNoPrimaryTerm(value.seqNoPrimaryTerm()).storedFields(value.storedFields()).pit(value.pit())
179+
.runtimeMappings(value.runtimeMappings()).stats(value.stats()));
180+
this.body = body;
181+
return this;
182+
}
183+
166184
/**
167185
* Builds a {@link RequestItem}.
168186
*

java-client/src/test/java/co/elastic/clients/elasticsearch/model/OverloadsTest.java

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
import co.elastic.clients.elasticsearch.ElasticsearchClient;
2323
import co.elastic.clients.elasticsearch._types.FieldSort;
2424
import co.elastic.clients.elasticsearch._types.FieldValue;
25+
import co.elastic.clients.elasticsearch._types.ScriptSource;
2526
import co.elastic.clients.elasticsearch._types.aggregations.TopMetrics;
2627
import co.elastic.clients.elasticsearch._types.query_dsl.MatchAllQuery;
28+
import co.elastic.clients.elasticsearch.core.MsearchRequest;
2729
import co.elastic.clients.elasticsearch.core.SearchRequest;
2830
import co.elastic.clients.elasticsearch.core.SearchResponse;
2931
import co.elastic.clients.testkit.ModelTestCase;
@@ -81,13 +83,68 @@ public void arrayOverloads() {
8183
}
8284

8385
@Test
84-
@Disabled("just need to compile")
86+
@Disabled("just needs to compile")
8587
public void voidClassTDocumentOverload() throws IOException {
8688
// no need for a complete instance of the client,
87-
// nor testing anything, just checking this compiles
89+
// nor testing anything, just checking that this compiles
8890
ElasticsearchClient client = ElasticsearchClient.of(e -> e.host("http://localhost:9200"));
8991

9092
SearchResponse<Void> resp = client.search(s -> s,Void.class);
9193
SearchResponse<Void> respDefault = client.search(s -> s);
9294
}
95+
96+
@Test
97+
public void searchRequestBodyOverloads() throws IOException {
98+
99+
// Normal search request
100+
SearchRequest searchRequest = SearchRequest.of(b -> b
101+
.size(10)
102+
.from(10)
103+
.query(q -> q
104+
.matchAll(m -> m)
105+
)
106+
);
107+
108+
// Msearch compatibility
109+
MsearchRequest msearchRequestStandard = MsearchRequest.of(ms -> ms
110+
.searches(s -> s
111+
.header(h -> h.index("index"))
112+
.body(b -> b
113+
.size(10)
114+
.from(10)
115+
.query(q -> q
116+
.matchAll(m -> m)
117+
)
118+
)
119+
)
120+
);
121+
122+
MsearchRequest msearchRequestOverload = MsearchRequest.of(ms -> ms
123+
.searches(s -> s
124+
.header(h -> h.index("index"))
125+
.body(searchRequest)
126+
)
127+
);
128+
129+
// Assert both variants result in the same serialization
130+
assertEquals(msearchRequestStandard.toString(), msearchRequestOverload.toString());
131+
132+
// Script source compatibility
133+
ScriptSource scriptSourceStandard = ScriptSource.of(s -> s
134+
.scriptTemplate(t -> t
135+
.size(10)
136+
.from(10)
137+
.query(q -> q
138+
.matchAll(m -> m)
139+
)
140+
)
141+
);
142+
143+
ScriptSource scriptSourceOverload = ScriptSource.of(s -> s
144+
.scriptTemplate(searchRequest)
145+
);
146+
147+
// Assert both variants result in the same serialization
148+
assertEquals(scriptSourceStandard.toString(), scriptSourceOverload.toString());
149+
}
93150
}

0 commit comments

Comments
 (0)