1818 */
1919package org .elasticsearch .search .source ;
2020
21+ import org .apache .lucene .search .join .ScoreMode ;
2122import org .elasticsearch .ExceptionsHelper ;
2223import org .elasticsearch .action .search .SearchPhaseExecutionException ;
2324import org .elasticsearch .action .search .SearchResponse ;
25+ import org .elasticsearch .index .query .InnerHitBuilder ;
26+ import org .elasticsearch .index .query .NestedQueryBuilder ;
27+ import org .elasticsearch .index .query .TermQueryBuilder ;
2428import org .elasticsearch .search .SearchContextException ;
29+ import org .elasticsearch .search .SearchHits ;
30+ import org .elasticsearch .search .fetch .subphase .FetchSourceContext ;
2531import org .elasticsearch .test .ESIntegTestCase ;
2632
33+ import java .util .Collections ;
34+
2735import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
2836import static org .hamcrest .Matchers .equalTo ;
2937import static org .hamcrest .Matchers .nullValue ;
@@ -33,7 +41,7 @@ public void testSimple() {
3341 assertAcked (prepareCreate ("test" ));
3442 ensureGreen ();
3543
36- client ().prepareIndex ("test" , "type1 " , "1" ).setSource ("field" , "value" ).execute ().actionGet ();
44+ client ().prepareIndex ("test" , "_doc " , "1" ).setSource ("field" , "value" ).execute ().actionGet ();
3745 refresh ();
3846
3947 SearchResponse response = client ()
@@ -42,23 +50,53 @@ public void testSimple() {
4250 .setFetchSource (false )
4351 .get ();
4452 assertThat (response .getHits ().getAt (0 ).getId (), nullValue ());
45- assertThat (response .getHits ().getAt (0 ).getType (), nullValue ( ));
53+ assertThat (response .getHits ().getAt (0 ).getType (), equalTo ( "_doc" ));
4654 assertThat (response .getHits ().getAt (0 ).getSourceAsString (), nullValue ());
4755
4856 response = client ()
4957 .prepareSearch ("test" )
5058 .storedFields ("_none_" )
5159 .get ();
5260 assertThat (response .getHits ().getAt (0 ).getId (), nullValue ());
53- assertThat (response .getHits ().getAt (0 ).getType (), nullValue ());
61+ assertThat (response .getHits ().getAt (0 ).getType (), equalTo ("_doc" ));
62+ assertThat (response .getHits ().getAt (0 ).getSourceAsString (), nullValue ());
63+ }
64+
65+ public void testInnerHits () {
66+ assertAcked (prepareCreate ("test" ).addMapping ("_doc" , "nested" , "type=nested" ));
67+ ensureGreen ();
68+ client ().prepareIndex ("test" , "_doc" , "1" )
69+ .setSource ("field" , "value" , "nested" , Collections .singletonMap ("title" , "foo" )).execute ().actionGet ();
70+ refresh ();
71+
72+ SearchResponse response = client ()
73+ .prepareSearch ("test" )
74+ .storedFields ("_none_" )
75+ .setFetchSource (false )
76+ .setQuery (
77+ new NestedQueryBuilder ("nested" , new TermQueryBuilder ("nested.title" , "foo" ), ScoreMode .Total )
78+ .innerHit (new InnerHitBuilder ()
79+ .setStoredFieldNames (Collections .singletonList ("_none_" ))
80+ .setFetchSourceContext (new FetchSourceContext (false )))
81+ )
82+ .get ();
83+ assertThat (response .getHits ().totalHits , equalTo (1L ));
84+ assertThat (response .getHits ().getAt (0 ).getId (), nullValue ());
85+ assertThat (response .getHits ().getAt (0 ).getType (), equalTo ("_doc" ));
5486 assertThat (response .getHits ().getAt (0 ).getSourceAsString (), nullValue ());
87+ assertThat (response .getHits ().getAt (0 ).getInnerHits ().size (), equalTo (1 ));
88+ SearchHits hits = response .getHits ().getAt (0 ).getInnerHits ().get ("nested" );
89+ assertThat (hits .totalHits , equalTo (1L ));
90+ assertThat (hits .getAt (0 ).getId (), nullValue ());
91+ assertThat (hits .getAt (0 ).getType (), equalTo ("_doc" ));
92+ assertThat (hits .getAt (0 ).getSourceAsString (), nullValue ());
5593 }
5694
5795 public void testWithRouting () {
5896 assertAcked (prepareCreate ("test" ));
5997 ensureGreen ();
6098
61- client ().prepareIndex ("test" , "type1 " , "1" ).setSource ("field" , "value" ).setRouting ("toto" ).execute ().actionGet ();
99+ client ().prepareIndex ("test" , "_doc " , "1" ).setSource ("field" , "value" ).setRouting ("toto" ).execute ().actionGet ();
62100 refresh ();
63101
64102 SearchResponse response = client ()
@@ -67,7 +105,7 @@ public void testWithRouting() {
67105 .setFetchSource (false )
68106 .get ();
69107 assertThat (response .getHits ().getAt (0 ).getId (), nullValue ());
70- assertThat (response .getHits ().getAt (0 ).getType (), nullValue ( ));
108+ assertThat (response .getHits ().getAt (0 ).getType (), equalTo ( "_doc" ));
71109 assertThat (response .getHits ().getAt (0 ).field ("_routing" ), nullValue ());
72110 assertThat (response .getHits ().getAt (0 ).getSourceAsString (), nullValue ());
73111
@@ -76,7 +114,7 @@ public void testWithRouting() {
76114 .storedFields ("_none_" )
77115 .get ();
78116 assertThat (response .getHits ().getAt (0 ).getId (), nullValue ());
79- assertThat (response .getHits ().getAt (0 ).getType (), nullValue ( ));
117+ assertThat (response .getHits ().getAt (0 ).getType (), equalTo ( "_doc" ));
80118 assertThat (response .getHits ().getAt (0 ).getSourceAsString (), nullValue ());
81119 }
82120
0 commit comments