Skip to content

Commit dd85dd3

Browse files
Support include_names_queries_score (#982) (#983)
* prep * regen on generator changes + unit test * restore include_names_queries_score * checkstyle * array can only expect string in this case Co-authored-by: Laura Trotta <153528055+l-trotta@users.noreply.github.com>
1 parent 60c6312 commit dd85dd3

File tree

6 files changed

+239
-14
lines changed

6 files changed

+239
-14
lines changed

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

+42
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,9 @@ public class MsearchRequest extends RequestBase implements NdJsonpSerializable,
108108
@Nullable
109109
private final Boolean ignoreUnavailable;
110110

111+
@Nullable
112+
private final Boolean includeNamedQueriesScore;
113+
111114
private final List<String> index;
112115

113116
@Nullable
@@ -136,6 +139,7 @@ private MsearchRequest(Builder builder) {
136139
this.expandWildcards = ApiTypeHelper.unmodifiable(builder.expandWildcards);
137140
this.ignoreThrottled = builder.ignoreThrottled;
138141
this.ignoreUnavailable = builder.ignoreUnavailable;
142+
this.includeNamedQueriesScore = builder.includeNamedQueriesScore;
139143
this.index = ApiTypeHelper.unmodifiable(builder.index);
140144
this.maxConcurrentSearches = builder.maxConcurrentSearches;
141145
this.maxConcurrentShardRequests = builder.maxConcurrentShardRequests;
@@ -210,6 +214,22 @@ public final Boolean ignoreUnavailable() {
210214
return this.ignoreUnavailable;
211215
}
212216

217+
/**
218+
* Indicates whether hit.matched_queries should be rendered as a map that
219+
* includes the name of the matched query associated with its score (true) or as
220+
* an array containing the name of the matched queries (false) This
221+
* functionality reruns each named query on every hit in a search response.
222+
* Typically, this adds a small overhead to a request. However, using
223+
* computationally expensive named queries on a large number of hits may add
224+
* significant overhead.
225+
* <p>
226+
* API name: {@code include_named_queries_score}
227+
*/
228+
@Nullable
229+
public final Boolean includeNamedQueriesScore() {
230+
return this.includeNamedQueriesScore;
231+
}
232+
213233
/**
214234
* Comma-separated list of data streams, indices, and index aliases to search.
215235
* <p>
@@ -320,6 +340,9 @@ public static class Builder extends RequestBase.AbstractBuilder<Builder> impleme
320340
@Nullable
321341
private Boolean ignoreUnavailable;
322342

343+
@Nullable
344+
private Boolean includeNamedQueriesScore;
345+
323346
@Nullable
324347
private List<String> index;
325348

@@ -413,6 +436,22 @@ public final Builder ignoreUnavailable(@Nullable Boolean value) {
413436
return this;
414437
}
415438

439+
/**
440+
* Indicates whether hit.matched_queries should be rendered as a map that
441+
* includes the name of the matched query associated with its score (true) or as
442+
* an array containing the name of the matched queries (false) This
443+
* functionality reruns each named query on every hit in a search response.
444+
* Typically, this adds a small overhead to a request. However, using
445+
* computationally expensive named queries on a large number of hits may add
446+
* significant overhead.
447+
* <p>
448+
* API name: {@code include_named_queries_score}
449+
*/
450+
public final Builder includeNamedQueriesScore(@Nullable Boolean value) {
451+
this.includeNamedQueriesScore = value;
452+
return this;
453+
}
454+
416455
/**
417456
* Comma-separated list of data streams, indices, and index aliases to search.
418457
* <p>
@@ -613,6 +652,9 @@ public MsearchRequest build() {
613652
params.put("expand_wildcards",
614653
request.expandWildcards.stream().map(v -> v.jsonValue()).collect(Collectors.joining(",")));
615654
}
655+
if (request.includeNamedQueriesScore != null) {
656+
params.put("include_named_queries_score", String.valueOf(request.includeNamedQueriesScore));
657+
}
616658
if (request.searchType != null) {
617659
params.put("search_type", request.searchType.jsonValue());
618660
}

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

+42
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ public class SearchRequest extends RequestBase implements JsonpSerializable {
184184
@Nullable
185185
private final Boolean ignoreUnavailable;
186186

187+
@Nullable
188+
private final Boolean includeNamedQueriesScore;
189+
187190
private final List<String> index;
188191

189192
private final List<NamedValue<Double>> indicesBoost;
@@ -304,6 +307,7 @@ private SearchRequest(Builder builder) {
304307
this.highlight = builder.highlight;
305308
this.ignoreThrottled = builder.ignoreThrottled;
306309
this.ignoreUnavailable = builder.ignoreUnavailable;
310+
this.includeNamedQueriesScore = builder.includeNamedQueriesScore;
307311
this.index = ApiTypeHelper.unmodifiable(builder.index);
308312
this.indicesBoost = ApiTypeHelper.unmodifiable(builder.indicesBoost);
309313
this.knn = ApiTypeHelper.unmodifiable(builder.knn);
@@ -598,6 +602,22 @@ public final Boolean ignoreUnavailable() {
598602
return this.ignoreUnavailable;
599603
}
600604

605+
/**
606+
* If <code>true</code>, the response includes the score contribution from any
607+
* named queries.
608+
* <p>
609+
* This functionality reruns each named query on every hit in a search response.
610+
* Typically, this adds a small overhead to a request. However, using
611+
* computationally expensive named queries on a large number of hits may add
612+
* significant overhead.
613+
* <p>
614+
* API name: {@code include_named_queries_score}
615+
*/
616+
@Nullable
617+
public final Boolean includeNamedQueriesScore() {
618+
return this.includeNamedQueriesScore;
619+
}
620+
601621
/**
602622
* A comma-separated list of data streams, indices, and aliases to search. It
603623
* supports wildcards (<code>*</code>). To search all data streams and indices,
@@ -1363,6 +1383,9 @@ public static class Builder extends RequestBase.AbstractBuilder<Builder> impleme
13631383
@Nullable
13641384
private Boolean ignoreUnavailable;
13651385

1386+
@Nullable
1387+
private Boolean includeNamedQueriesScore;
1388+
13661389
@Nullable
13671390
private List<String> index;
13681391

@@ -1883,6 +1906,22 @@ public final Builder ignoreUnavailable(@Nullable Boolean value) {
18831906
return this;
18841907
}
18851908

1909+
/**
1910+
* If <code>true</code>, the response includes the score contribution from any
1911+
* named queries.
1912+
* <p>
1913+
* This functionality reruns each named query on every hit in a search response.
1914+
* Typically, this adds a small overhead to a request. However, using
1915+
* computationally expensive named queries on a large number of hits may add
1916+
* significant overhead.
1917+
* <p>
1918+
* API name: {@code include_named_queries_score}
1919+
*/
1920+
public final Builder includeNamedQueriesScore(@Nullable Boolean value) {
1921+
this.includeNamedQueriesScore = value;
1922+
return this;
1923+
}
1924+
18861925
/**
18871926
* A comma-separated list of data streams, indices, and aliases to search. It
18881927
* supports wildcards (<code>*</code>). To search all data streams and indices,
@@ -2909,6 +2948,9 @@ protected static void setupSearchRequestDeserializer(ObjectDeserializer<SearchRe
29092948
if (request.forceSyntheticSource != null) {
29102949
params.put("force_synthetic_source", String.valueOf(request.forceSyntheticSource));
29112950
}
2951+
if (request.includeNamedQueriesScore != null) {
2952+
params.put("include_named_queries_score", String.valueOf(request.includeNamedQueriesScore));
2953+
}
29122954
if (request.lenient != null) {
29132955
params.put("lenient", String.valueOf(request.lenient));
29142956
}

java-client/src/main/java/co/elastic/clients/elasticsearch/core/search/Hit.java

+24-14
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public class Hit<TDocument> implements JsonpSerializable {
8787

8888
private final Map<String, InnerHitsResult> innerHits;
8989

90-
private final List<String> matchedQueries;
90+
private final Map<String, Double> matchedQueries;
9191

9292
@Nullable
9393
private final NestedIdentity nested;
@@ -212,7 +212,7 @@ public final Map<String, InnerHitsResult> innerHits() {
212212
/**
213213
* API name: {@code matched_queries}
214214
*/
215-
public final List<String> matchedQueries() {
215+
public final Map<String, Double> matchedQueries() {
216216
return this.matchedQueries;
217217
}
218218

@@ -380,12 +380,21 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) {
380380
}
381381
if (ApiTypeHelper.isDefined(this.matchedQueries)) {
382382
generator.writeKey("matched_queries");
383-
generator.writeStartArray();
384-
for (String item0 : this.matchedQueries) {
385-
generator.write(item0);
383+
if (this.matchedQueries.values().stream().allMatch(Objects::isNull)) {
384+
generator.writeStartArray();
385+
for (String item0 : this.matchedQueries.keySet()) {
386+
generator.write(item0);
387+
}
388+
generator.writeEnd();
389+
} else {
390+
generator.writeStartObject();
391+
for (Map.Entry<String, Double> item0 : this.matchedQueries.entrySet()) {
392+
generator.writeKey(item0.getKey());
393+
generator.write(item0.getValue());
386394

395+
}
396+
generator.writeEnd();
387397
}
388-
generator.writeEnd();
389398

390399
}
391400
if (this.nested != null) {
@@ -509,7 +518,7 @@ public static class Builder<TDocument> extends WithJsonObjectBuilderBase<Builder
509518
private Map<String, InnerHitsResult> innerHits;
510519

511520
@Nullable
512-
private List<String> matchedQueries;
521+
private Map<String, Double> matchedQueries;
513522

514523
@Nullable
515524
private NestedIdentity nested;
@@ -662,20 +671,20 @@ public final Builder<TDocument> innerHits(String key,
662671
/**
663672
* API name: {@code matched_queries}
664673
* <p>
665-
* Adds all elements of <code>list</code> to <code>matchedQueries</code>.
674+
* Adds all entries of <code>map</code> to <code>matchedQueries</code>.
666675
*/
667-
public final Builder<TDocument> matchedQueries(List<String> list) {
668-
this.matchedQueries = _listAddAll(this.matchedQueries, list);
676+
public final Builder<TDocument> matchedQueries(Map<String, Double> map) {
677+
this.matchedQueries = _mapPutAll(this.matchedQueries, map);
669678
return this;
670679
}
671680

672681
/**
673682
* API name: {@code matched_queries}
674683
* <p>
675-
* Adds one or more values to <code>matchedQueries</code>.
684+
* Adds an entry to <code>matchedQueries</code>.
676685
*/
677-
public final Builder<TDocument> matchedQueries(String value, String... values) {
678-
this.matchedQueries = _listAdd(this.matchedQueries, value, values);
686+
public final Builder<TDocument> matchedQueries(String key, Double value) {
687+
this.matchedQueries = _mapPut(this.matchedQueries, key, value);
679688
return this;
680689
}
681690

@@ -937,7 +946,8 @@ protected static <TDocument> void setupHitDeserializer(ObjectDeserializer<Hit.Bu
937946
JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer())), "highlight");
938947
op.add(Builder::innerHits, JsonpDeserializer.stringMapDeserializer(InnerHitsResult._DESERIALIZER),
939948
"inner_hits");
940-
op.add(Builder::matchedQueries, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()),
949+
op.add(Builder::matchedQueries,
950+
JsonpDeserializer.stringArrayMapUnionDeserializer(JsonpDeserializer.doubleDeserializer()),
941951
"matched_queries");
942952
op.add(Builder::nested, NestedIdentity._DESERIALIZER, "_nested");
943953
op.add(Builder::ignored, JsonpDeserializer.arrayDeserializer(JsonpDeserializer.stringDeserializer()),

java-client/src/main/java/co/elastic/clients/json/JsonpDeserializer.java

+4
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ static <T> JsonpDeserializer<Map<String, T>> stringMapDeserializer(JsonpDeserial
221221
return new JsonpDeserializerBase.StringMapDeserializer<T>(itemDeserializer);
222222
}
223223

224+
static <T> JsonpDeserializer<Map<String, T>> stringArrayMapUnionDeserializer(JsonpDeserializer<T> itemDeserializer) {
225+
return new JsonpDeserializerBase.StringArrayMapUnionDeserializer<T>(itemDeserializer);
226+
}
227+
224228
static <K extends JsonEnum, V> JsonpDeserializer<Map<K, V>> enumMapDeserializer(
225229
JsonpDeserializer<K> keyDeserializer, JsonpDeserializer<V> valueDeserializer
226230
) {

java-client/src/main/java/co/elastic/clients/json/JsonpDeserializerBase.java

+37
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,43 @@ public Map<String, T> deserialize(JsonParser parser, JsonpMapper mapper, Event e
356356
}
357357
}
358358

359+
// Used for fields such as matched_queries, which can be either an array or a dictionary, where the array
360+
// value type is the same as the dictionary key type
361+
static class StringArrayMapUnionDeserializer<T> extends JsonpDeserializerBase<Map<String, T>> {
362+
private final JsonpDeserializer<T> itemDeserializer;
363+
364+
protected StringArrayMapUnionDeserializer(JsonpDeserializer<T> itemDeserializer) {
365+
super(EnumSet.of(Event.START_OBJECT,Event.START_ARRAY));
366+
this.itemDeserializer = itemDeserializer;
367+
}
368+
369+
@Override
370+
public Map<String, T> deserialize(JsonParser parser, JsonpMapper mapper, Event event) {
371+
Map<String, T> result = new HashMap<>();
372+
String key = null;
373+
try {
374+
// Array case, deserializing it into a map with null values
375+
if (event == Event.START_ARRAY) {
376+
while ((event = parser.next()) != Event.END_ARRAY) {
377+
JsonpUtils.expectEvent(parser, Event.VALUE_STRING, event);
378+
result.put(parser.getString(),null);
379+
}
380+
} else {
381+
// Dictionary case
382+
while ((event = parser.next()) != Event.END_OBJECT) {
383+
JsonpUtils.expectEvent(parser, Event.KEY_NAME, event);
384+
key = parser.getString();
385+
T value = itemDeserializer.deserialize(parser, mapper);
386+
result.put(key, value);
387+
}
388+
}
389+
} catch (Exception e) {
390+
throw JsonpMappingException.from(e, null, key, parser);
391+
}
392+
return result;
393+
}
394+
}
395+
359396
static class EnumMapDeserializer<K, V> extends JsonpDeserializerBase<Map<K, V>> {
360397
private final JsonpDeserializer<K> keyDeserializer;
361398
private final JsonpDeserializer<V> valueDeserializer;

0 commit comments

Comments
 (0)