Skip to content

Commit c99e926

Browse files
committed
function score deserialization bug fix
1 parent 9bd69ba commit c99e926

File tree

2 files changed

+72
-28
lines changed

2 files changed

+72
-28
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ protected static void setupFunctionScoreQueryDeserializer(ObjectDeserializer<Fun
385385
op.add(Builder::query, Query._DESERIALIZER, "query");
386386
op.add(Builder::scoreMode, FunctionScoreMode._DESERIALIZER, "score_mode");
387387

388-
op.shortcutProperty("functions", true);
388+
op.shortcutProperty("functions");
389389

390390
}
391391

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

Lines changed: 71 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import co.elastic.clients.util.MapBuilder;
4343
import org.junit.jupiter.api.Test;
4444

45+
import static co.elastic.clients.elasticsearch._types.query_dsl.Query.Kind.MatchAll;
46+
4547
public class BehaviorsTest extends ModelTestCase {
4648

4749
/**
@@ -249,46 +251,88 @@ public void testObjectShortcutProperty() {
249251
assertEquals("foo", req.query().term().field());
250252
assertEquals("bar", req.query().term().value().stringValue());
251253
}
252-
254+
253255
@Test
254256
public void testFunctionScoreQuery() {
255-
String shortcut =
256-
"{" +
257-
" \"gauss\": {" +
258-
" \"date\": {" +
259-
" \"origin\": \"2013-09-17\", " +
260-
" \"scale\": \"10d\"," +
261-
" \"offset\": \"5d\", " +
262-
" \"decay\": 0.5" +
263-
" }," +
264-
" \"multi_value_mode\": \"avg\"" +
265-
" }" +
257+
258+
String queryOnly = "{" +
259+
" \"query\": {\n" +
260+
" \"match_all\": {}\n" +
261+
" }\n" +
266262
"}";
267263

268264
String full =
269265
"{" +
270-
" \"functions\": [" +
271-
" {" +
272-
" \"gauss\": {" +
273-
" \"date\": {" +
274-
" \"origin\": \"2013-09-17\"," +
275-
" \"scale\": \"10d\"," +
276-
" \"offset\": \"5d\"," +
277-
" \"decay\": 0.5" +
278-
" }," +
279-
" \"multi_value_mode\": \"avg\"" +
280-
" }" +
281-
" }" +
282-
" ]" +
283-
"}";
266+
" \"functions\": [" +
267+
" {" +
268+
" \"gauss\": {" +
269+
" \"date\": {" +
270+
" \"origin\": \"2013-09-17\"," +
271+
" \"scale\": \"10d\"," +
272+
" \"offset\": \"5d\"," +
273+
" \"decay\": 0.5" +
274+
" }," +
275+
" \"multi_value_mode\": \"avg\"" +
276+
" }" +
277+
" }" +
278+
" ]" +
279+
"}";
280+
281+
String nested =
282+
"{\n" +
283+
" \"functions\": [\n" +
284+
" {\n" +
285+
" \"gauss\": {" +
286+
" \"date\": {" +
287+
" \"origin\": \"2013-09-17\"," +
288+
" \"scale\": \"10d\"," +
289+
" \"offset\": \"5d\"," +
290+
" \"decay\": 0.5" +
291+
" }," +
292+
" \"multi_value_mode\": \"avg\"" +
293+
" }\n" +
294+
" }\n" +
295+
" ],\n" +
296+
" \"query\": {\n" +
297+
" \"function_score\": {\n" +
298+
" \"query\": {\n" +
299+
" \"match_all\": {}\n" +
300+
" }\n" +
301+
" }\n" +
302+
" }\n" +
303+
" }";
304+
305+
306+
String shortcut =
307+
"{" +
308+
" \"gauss\": {" +
309+
" \"date\": {" +
310+
" \"origin\": \"2013-09-17\", " +
311+
" \"scale\": \"10d\"," +
312+
" \"offset\": \"5d\", " +
313+
" \"decay\": 0.5" +
314+
" }," +
315+
" \"multi_value_mode\": \"avg\"" +
316+
" }" +
317+
"}";
284318

285319
FunctionScoreQuery fsq;
286320

321+
// should always work, this is just a function score query with just one "query" field
322+
fsq = fromJson(queryOnly, FunctionScoreQuery.class);
323+
assertEquals(MatchAll, fsq.query()._kind());
324+
325+
// should also work
287326
fsq = fromJson(full, FunctionScoreQuery.class);
288327
assertEquals(MultiValueMode.Avg, fsq.functions().get(0).gauss().untyped().multiValueMode());
289328

329+
// should also work
330+
fsq = fromJson(nested, FunctionScoreQuery.class);
331+
assertEquals(MatchAll, fsq.query().functionScore().query()._kind());
332+
333+
// should not work, shortcut for function score query is currently not supported
290334
fsq = fromJson(shortcut, FunctionScoreQuery.class);
291-
assertEquals(MultiValueMode.Avg, fsq.functions().get(0).gauss().untyped().multiValueMode());
335+
assertEquals(0, fsq.functions().size());
292336
}
293337

294338
@Test

0 commit comments

Comments
 (0)