use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.
the class DynamicMappingTests method testIntroduceTwoFields.
public void testIntroduceTwoFields() throws Exception {
IndexService indexService = createIndex("test");
DocumentMapperParser parser = indexService.mapperService().documentMapperParser();
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
DocumentMapper mapper = parser.parse("type", new CompressedXContent(mapping));
assertEquals(mapping, serialize(mapper));
Mapper update = parse(mapper, parser, XContentFactory.jsonBuilder().startObject().field("foo", "bar").field("bar", "baz").endObject());
assertNotNull(update);
// original mapping not modified
assertEquals(mapping, serialize(mapper));
// but we have an update
assertEquals(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("bar").field("type", "text").startObject("fields").startObject("keyword").field("type", "keyword").field("ignore_above", 256).endObject().endObject().endObject().startObject("foo").field("type", "text").startObject("fields").startObject("keyword").field("type", "keyword").field("ignore_above", 256).endObject().endObject().endObject().endObject().endObject().endObject().string(), serialize(update));
}
use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.
the class DynamicMappingTests method testDateDetectionInheritsFormat.
public void testDateDetectionInheritsFormat() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startArray("dynamic_date_formats").value("yyyy-MM-dd").endArray().startArray("dynamic_templates").startObject().startObject("dates").field("match_mapping_type", "date").field("match", "*2").startObject("mapping").endObject().endObject().endObject().startObject().startObject("dates").field("match_mapping_type", "date").field("match", "*3").startObject("mapping").field("format", "yyyy-MM-dd||epoch_millis").endObject().endObject().endObject().endArray().endObject().endObject().string();
IndexService index = createIndex("test");
client().admin().indices().preparePutMapping("test").setType("type").setSource(mapping, XContentType.JSON).get();
DocumentMapper defaultMapper = index.mapperService().documentMapper("type");
ParsedDocument doc = defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("date1", "2016-11-20").field("date2", "2016-11-20").field("date3", "2016-11-20").endObject().bytes());
assertNotNull(doc.dynamicMappingsUpdate());
assertAcked(client().admin().indices().preparePutMapping("test").setType("type").setSource(doc.dynamicMappingsUpdate().toString(), XContentType.JSON).get());
defaultMapper = index.mapperService().documentMapper("type");
DateFieldMapper dateMapper1 = (DateFieldMapper) defaultMapper.mappers().smartNameFieldMapper("date1");
DateFieldMapper dateMapper2 = (DateFieldMapper) defaultMapper.mappers().smartNameFieldMapper("date2");
DateFieldMapper dateMapper3 = (DateFieldMapper) defaultMapper.mappers().smartNameFieldMapper("date3");
// inherited from dynamic date format
assertEquals("yyyy-MM-dd", dateMapper1.fieldType().dateTimeFormatter().format());
// inherited from dynamic date format since the mapping in the template did not specify a format
assertEquals("yyyy-MM-dd", dateMapper2.fieldType().dateTimeFormatter().format());
// not inherited from the dynamic date format since the template defined an explicit format
assertEquals("yyyy-MM-dd||epoch_millis", dateMapper3.fieldType().dateTimeFormatter().format());
}
use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.
the class DynamicMappingTests method testNumericDetectionDefault.
public void testNumericDetectionDefault() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject().string();
IndexService index = createIndex("test");
client().admin().indices().preparePutMapping("test").setType("type").setSource(mapping, XContentType.JSON).get();
DocumentMapper defaultMapper = index.mapperService().documentMapper("type");
ParsedDocument doc = defaultMapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("s_long", "100").field("s_double", "100.0").endObject().bytes());
assertNotNull(doc.dynamicMappingsUpdate());
assertAcked(client().admin().indices().preparePutMapping("test").setType("type").setSource(doc.dynamicMappingsUpdate().toString(), XContentType.JSON).get());
defaultMapper = index.mapperService().documentMapper("type");
FieldMapper mapper = defaultMapper.mappers().smartNameFieldMapper("s_long");
assertThat(mapper, instanceOf(TextFieldMapper.class));
mapper = defaultMapper.mappers().smartNameFieldMapper("s_double");
assertThat(mapper, instanceOf(TextFieldMapper.class));
}
use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.
the class TokenCountFieldMapperTests method testEmptyName.
public void testEmptyName() throws IOException {
IndexService indexService = createIndex("test");
DocumentMapperParser parser = indexService.mapperService().documentMapperParser();
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("").field("type", "token_count").field("analyzer", "standard").endObject().endObject().endObject().endObject().string();
// Empty name not allowed in index created after 5.0
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse("type", new CompressedXContent(mapping)));
assertThat(e.getMessage(), containsString("name cannot be empty string"));
}
use of org.elasticsearch.index.IndexService in project elasticsearch by elastic.
the class UpdateMappingTests method testNoConflictWhileMergingAndMappingChanged.
private void testNoConflictWhileMergingAndMappingChanged(XContentBuilder mapping, XContentBuilder mappingUpdate, XContentBuilder expectedMapping) throws IOException {
IndexService indexService = createIndex("test", Settings.builder().build(), "type", mapping);
// simulate like in MetaDataMappingService#putMapping
indexService.mapperService().merge("type", new CompressedXContent(mappingUpdate.bytes()), MapperService.MergeReason.MAPPING_UPDATE, false);
// make sure mappings applied
CompressedXContent mappingAfterUpdate = indexService.mapperService().documentMapper("type").mappingSource();
assertThat(mappingAfterUpdate.toString(), equalTo(expectedMapping.string()));
}
Aggregations