@@ -222,4 +222,60 @@ public void realtimeGetWithCompress() throws Exception {
222222 assertThat (getResponse .exists (), equalTo (true ));
223223 assertThat (getResponse .sourceAsMap ().get ("field" ).toString (), equalTo (fieldValue ));
224224 }
225+
226+ @ Test
227+ public void getFieldsWithDifferentTypes () throws Exception {
228+ client .admin ().indices ().prepareDelete ().execute ().actionGet ();
229+
230+ client .admin ().indices ().prepareCreate ("test" ).setSettings (ImmutableSettings .settingsBuilder ().put ("index.refresh_interval" , -1 ))
231+ .addMapping ("type1" , jsonBuilder ().startObject ().startObject ("type" ).startObject ("_source" ).field ("enabled" , true ).endObject ().endObject ().endObject ())
232+ .addMapping ("type2" , jsonBuilder ().startObject ().startObject ("type" )
233+ .startObject ("_source" ).field ("enabled" , false ).endObject ()
234+ .startObject ("properties" )
235+ .startObject ("str" ).field ("type" , "string" ).field ("store" , "yes" ).endObject ()
236+ .startObject ("int" ).field ("type" , "integer" ).field ("store" , "yes" ).endObject ()
237+ .startObject ("date" ).field ("type" , "date" ).field ("store" , "yes" ).endObject ()
238+ .endObject ()
239+ .endObject ().endObject ())
240+ .execute ().actionGet ();
241+
242+ ClusterHealthResponse clusterHealth = client .admin ().cluster ().health (clusterHealthRequest ().waitForGreenStatus ()).actionGet ();
243+ assertThat (clusterHealth .timedOut (), equalTo (false ));
244+ assertThat (clusterHealth .status (), equalTo (ClusterHealthStatus .GREEN ));
245+
246+ client .prepareIndex ("test" , "type1" , "1" ).setSource ("str" , "test" , "int" , 42 , "date" , "2012-11-13T15:26:14.000Z" ).execute ().actionGet ();
247+ client .prepareIndex ("test" , "type2" , "1" ).setSource ("str" , "test" , "int" , 42 , "date" , "2012-11-13T15:26:14.000Z" ).execute ().actionGet ();
248+
249+ // realtime get with stored source
250+ logger .info ("--> realtime get (from source)" );
251+ GetResponse getResponse = client .prepareGet ("test" , "type1" , "1" ).setFields ("str" , "int" , "date" ).execute ().actionGet ();
252+ assertThat (getResponse .exists (), equalTo (true ));
253+ assertThat ((String ) getResponse .field ("str" ).getValue (), equalTo ("test" ));
254+ assertThat ((Integer ) getResponse .field ("int" ).getValue (), equalTo (42 ));
255+ assertThat ((String ) getResponse .field ("date" ).getValue (), equalTo ("2012-11-13T15:26:14.000Z" ));
256+
257+ logger .info ("--> realtime get (from stored fields)" );
258+ getResponse = client .prepareGet ("test" , "type2" , "1" ).setFields ("str" , "int" , "date" ).execute ().actionGet ();
259+ assertThat (getResponse .exists (), equalTo (true ));
260+ assertThat ((String ) getResponse .field ("str" ).getValue (), equalTo ("test" ));
261+ assertThat ((Integer ) getResponse .field ("int" ).getValue (), equalTo (42 ));
262+ assertThat ((String ) getResponse .field ("date" ).getValue (), equalTo ("2012-11-13T15:26:14.000Z" ));
263+
264+ logger .info ("--> flush the index, so we load it from it" );
265+ client .admin ().indices ().prepareFlush ().execute ().actionGet ();
266+
267+ logger .info ("--> non realtime get (from source)" );
268+ getResponse = client .prepareGet ("test" , "type1" , "1" ).setFields ("str" , "int" , "date" ).execute ().actionGet ();
269+ assertThat (getResponse .exists (), equalTo (true ));
270+ assertThat ((String ) getResponse .field ("str" ).getValue (), equalTo ("test" ));
271+ assertThat ((Integer ) getResponse .field ("int" ).getValue (), equalTo (42 ));
272+ assertThat ((String ) getResponse .field ("date" ).getValue (), equalTo ("2012-11-13T15:26:14.000Z" ));
273+
274+ logger .info ("--> non realtime get (from stored fields)" );
275+ getResponse = client .prepareGet ("test" , "type2" , "1" ).setFields ("str" , "int" , "date" ).execute ().actionGet ();
276+ assertThat (getResponse .exists (), equalTo (true ));
277+ assertThat ((String ) getResponse .field ("str" ).getValue (), equalTo ("test" ));
278+ assertThat ((Integer ) getResponse .field ("int" ).getValue (), equalTo (42 ));
279+ assertThat ((String ) getResponse .field ("date" ).getValue (), equalTo ("2012-11-13T15:26:14.000Z" ));
280+ }
225281}
0 commit comments