@@ -402,45 +402,50 @@ public void testInnerHitsWithObjectFieldThatHasANestedField() throws Exception {
402
402
List <IndexRequestBuilder > requests = new ArrayList <>();
403
403
requests .add (client ().prepareIndex ("articles" , "article" , "1" ).setSource (jsonBuilder ().startObject ()
404
404
.field ("title" , "quick brown fox" )
405
- .startObject ("comments" )
406
- .startArray ("messages" )
407
- .startObject ().field ("message" , "fox eat quick" ).endObject ()
408
- .startObject ().field ("message" , "bear eat quick" ).endObject ()
405
+ .startArray ("comments" )
406
+ .startObject ()
407
+ .startArray ("messages" )
408
+ .startObject ().field ("message" , "fox eat quick" ).endObject ()
409
+ .startObject ().field ("message" , "bear eat quick" ).endObject ()
410
+ .endArray ()
411
+ .endObject ()
412
+ .startObject ()
413
+ .startArray ("messages" )
414
+ .startObject ().field ("message" , "no fox" ).endObject ()
415
+ .endArray ()
416
+ .endObject ()
409
417
.endArray ()
410
- .endObject ()
411
418
.endObject ()));
412
419
indexRandom (true , requests );
413
420
414
- SearchPhaseExecutionException e = expectThrows (
415
- SearchPhaseExecutionException .class ,
416
- () -> client ().prepareSearch ("articles" ).setQuery (nestedQuery ("comments.messages" ,
417
- matchQuery ("comments.messages.message" , "fox" ), ScoreMode .Avg ).innerHit (new InnerHitBuilder ())).get ()
418
- );
421
+ SearchResponse response = client ().prepareSearch ("articles" ).setQuery (nestedQuery ("comments.messages" ,
422
+ matchQuery ("comments.messages.message" , "fox" ), ScoreMode .Avg ).innerHit (new InnerHitBuilder ())).get ();
419
423
assertEquals ("Cannot execute inner hits. One or more parent object fields of nested field [comments.messages] are " +
420
- "not nested. All parent fields need to be nested fields too" , e . shardFailures ()[0 ].getCause ().getMessage ());
424
+ "not nested. All parent fields need to be nested fields too" , response . getShardFailures ()[0 ].getCause ().getMessage ());
421
425
422
- e = expectThrows (
423
- SearchPhaseExecutionException .class ,
424
- () -> client ().prepareSearch ("articles" ).setQuery (nestedQuery ("comments.messages" ,
425
- matchQuery ("comments.messages.message" , "fox" ), ScoreMode .Avg ).innerHit (new InnerHitBuilder ()
426
- .setFetchSourceContext (new FetchSourceContext (true )))).get ()
427
- );
426
+ response = client ().prepareSearch ("articles" ).setQuery (nestedQuery ("comments.messages" ,
427
+ matchQuery ("comments.messages.message" , "fox" ), ScoreMode .Avg ).innerHit (new InnerHitBuilder ()
428
+ .setFetchSourceContext (new FetchSourceContext (true )))).get ();
428
429
assertEquals ("Cannot execute inner hits. One or more parent object fields of nested field [comments.messages] are " +
429
- "not nested. All parent fields need to be nested fields too" , e . shardFailures ()[0 ].getCause ().getMessage ());
430
+ "not nested. All parent fields need to be nested fields too" , response . getShardFailures ()[0 ].getCause ().getMessage ());
430
431
431
- SearchResponse response = client ().prepareSearch ("articles" )
432
+ response = client ().prepareSearch ("articles" )
432
433
.setQuery (nestedQuery ("comments.messages" , matchQuery ("comments.messages.message" , "fox" ), ScoreMode .Avg )
433
434
.innerHit (new InnerHitBuilder ().setFetchSourceContext (new FetchSourceContext (false )))).get ();
434
435
assertNoFailures (response );
435
436
assertHitCount (response , 1 );
436
437
SearchHit hit = response .getHits ().getAt (0 );
437
438
assertThat (hit .getId (), equalTo ("1" ));
438
439
SearchHits messages = hit .getInnerHits ().get ("comments.messages" );
439
- assertThat (messages .getTotalHits (), equalTo (1L ));
440
+ assertThat (messages .getTotalHits (), equalTo (2L ));
440
441
assertThat (messages .getAt (0 ).getId (), equalTo ("1" ));
441
442
assertThat (messages .getAt (0 ).getNestedIdentity ().getField ().string (), equalTo ("comments.messages" ));
442
- assertThat (messages .getAt (0 ).getNestedIdentity ().getOffset (), equalTo (0 ));
443
+ assertThat (messages .getAt (0 ).getNestedIdentity ().getOffset (), equalTo (2 ));
443
444
assertThat (messages .getAt (0 ).getNestedIdentity ().getChild (), nullValue ());
445
+ assertThat (messages .getAt (1 ).getId (), equalTo ("1" ));
446
+ assertThat (messages .getAt (1 ).getNestedIdentity ().getField ().string (), equalTo ("comments.messages" ));
447
+ assertThat (messages .getAt (1 ).getNestedIdentity ().getOffset (), equalTo (0 ));
448
+ assertThat (messages .getAt (1 ).getNestedIdentity ().getChild (), nullValue ());
444
449
445
450
response = client ().prepareSearch ("articles" )
446
451
.setQuery (nestedQuery ("comments.messages" , matchQuery ("comments.messages.message" , "bear" ), ScoreMode .Avg )
0 commit comments