23
23
import org .elasticsearch .common .settings .ImmutableSettings ;
24
24
import org .elasticsearch .common .xcontent .XContentBuilder ;
25
25
import org .elasticsearch .common .xcontent .XContentFactory ;
26
- import org .elasticsearch .index .mapper .DocumentMapper ;
27
- import org .elasticsearch .index .mapper .FieldMappers ;
28
- import org .elasticsearch .index .mapper .ParsedDocument ;
29
- import org .elasticsearch .index .mapper .StrictDynamicMappingException ;
26
+ import org .elasticsearch .index .mapper .*;
30
27
import org .elasticsearch .index .IndexService ;
31
28
import org .elasticsearch .test .ElasticsearchSingleNodeTest ;
32
29
import org .junit .Test ;
33
30
34
31
import java .io .IOException ;
32
+ import java .util .LinkedHashMap ;
33
+ import java .util .Map ;
35
34
36
35
import static org .elasticsearch .common .xcontent .XContentFactory .jsonBuilder ;
37
36
import static org .hamcrest .Matchers .equalTo ;
@@ -242,4 +241,41 @@ public boolean apply(java.lang.Object input) {
242
241
getMappingsResponse = client ().admin ().indices ().prepareGetMappings ("test" ).get ();
243
242
assertNotNull (getMappingsResponse .getMappings ().get ("test" ).get ("type" ));
244
243
}
244
+
245
+ @ Test
246
+ public void testFieldsCreatedProperly () throws IOException , InterruptedException {
247
+ XContentBuilder mapping = jsonBuilder ().startObject ().startObject ("doc" )
248
+ .startObject ("properties" )
249
+ .startObject ("z" )
250
+ .field ("type" , "long" )
251
+ .endObject ()
252
+ .endObject ()
253
+ .endObject ().endObject ();
254
+
255
+ IndexService indexService = createIndex ("test" , ImmutableSettings .EMPTY , "doc" , mapping );
256
+
257
+ try {
258
+ client ().prepareIndex ().setIndex ("test" ).setType ("doc" ).setSource (jsonBuilder ().startObject ().field ("a" , "string" ).field ("z" , "string" ).endObject ()).get ();
259
+ fail ();
260
+ } catch (MapperParsingException e ) {
261
+ // this should fail because the field z is of type long
262
+ }
263
+ //type should be in mapping
264
+ GetMappingsResponse getMappingsResponse = client ().admin ().indices ().prepareGetMappings ("test" ).get ();
265
+ assertNotNull (getMappingsResponse .getMappings ().get ("test" ).get ("doc" ));
266
+
267
+ client ().prepareIndex ().setIndex ("test" ).setType ("doc" ).setSource (jsonBuilder ().startObject ().field ("a" , "string" ).field ("z" , 0 ).endObject ()).get ();
268
+ client ().admin ().indices ().prepareRefresh ("test" ).get ();
269
+ assertThat (client ().prepareSearch ("test" ).get ().getHits ().getTotalHits (), equalTo (1l ));
270
+
271
+ DocumentMapper mapper = indexService .mapperService ().documentMapper ("doc" );
272
+ assertNotNull (mapper .mappers ().name ("a" ));
273
+ assertNotNull (mapper .mappers ().name ("z" ));
274
+
275
+ getMappingsResponse = client ().admin ().indices ().prepareGetMappings ("test" ).get ();
276
+ assertNotNull (getMappingsResponse .getMappings ().get ("test" ).get ("doc" ));
277
+ Map <String , Object > mappings = getMappingsResponse .getMappings ().get ("test" ).get ("doc" ).getSourceAsMap ();
278
+ assertNotNull (((LinkedHashMap )mappings .get ("properties" )).get ("a" ));
279
+ assertNotNull (((LinkedHashMap )mappings .get ("properties" )).get ("z" ));
280
+ }
245
281
}
0 commit comments