|
42 | 42 | import co.elastic.clients.util.MapBuilder;
|
43 | 43 | import org.junit.jupiter.api.Test;
|
44 | 44 |
|
| 45 | +import static co.elastic.clients.elasticsearch._types.query_dsl.Query.Kind.MatchAll; |
| 46 | + |
45 | 47 | public class BehaviorsTest extends ModelTestCase {
|
46 | 48 |
|
47 | 49 | /**
|
@@ -249,46 +251,88 @@ public void testObjectShortcutProperty() {
|
249 | 251 | assertEquals("foo", req.query().term().field());
|
250 | 252 | assertEquals("bar", req.query().term().value().stringValue());
|
251 | 253 | }
|
252 |
| - |
| 254 | + |
253 | 255 | @Test
|
254 | 256 | 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" + |
266 | 262 | "}";
|
267 | 263 |
|
268 | 264 | String full =
|
269 | 265 | "{" +
|
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 | + "}"; |
284 | 318 |
|
285 | 319 | FunctionScoreQuery fsq;
|
286 | 320 |
|
| 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 |
287 | 326 | fsq = fromJson(full, FunctionScoreQuery.class);
|
288 | 327 | assertEquals(MultiValueMode.Avg, fsq.functions().get(0).gauss().untyped().multiValueMode());
|
289 | 328 |
|
| 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 |
290 | 334 | fsq = fromJson(shortcut, FunctionScoreQuery.class);
|
291 |
| - assertEquals(MultiValueMode.Avg, fsq.functions().get(0).gauss().untyped().multiValueMode()); |
| 335 | + assertEquals(0, fsq.functions().size()); |
292 | 336 | }
|
293 | 337 |
|
294 | 338 | @Test
|
|
0 commit comments