use of org.opensearch.index.mapper.MapperService.MergeReason in project OpenSearch by opensearch-project.
the class ObjectMapperTests method testMerge.
public void testMerge() throws IOException {
MergeReason reason = randomFrom(MergeReason.values());
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("foo").field("type", "keyword").endObject().endObject().endObject().endObject());
MapperService mapperService = createIndex("test").mapperService();
DocumentMapper mapper = mapperService.merge("type", new CompressedXContent(mapping), reason);
assertNull(mapper.root().dynamic());
String update = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").field("dynamic", "strict").endObject().endObject());
mapper = mapperService.merge("type", new CompressedXContent(update), reason);
assertEquals(Dynamic.STRICT, mapper.root().dynamic());
}
use of org.opensearch.index.mapper.MapperService.MergeReason in project OpenSearch by opensearch-project.
the class RootObjectMapperTests method testDateDetection.
public void testDateDetection() throws Exception {
MergeReason reason = randomFrom(MergeReason.MAPPING_UPDATE, MergeReason.INDEX_TEMPLATE);
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").field("date_detection", true).endObject().endObject());
MapperService mapperService = createIndex("test").mapperService();
DocumentMapper mapper = mapperService.merge("type", new CompressedXContent(mapping), reason);
assertEquals(mapping, mapper.mappingSource().toString());
// update with a different explicit value
String mapping2 = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").field("date_detection", false).endObject().endObject());
mapper = mapperService.merge("type", new CompressedXContent(mapping2), reason);
assertEquals(mapping2, mapper.mappingSource().toString());
// update with an implicit value: no change
String mapping3 = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject());
mapper = mapperService.merge("type", new CompressedXContent(mapping3), reason);
assertEquals(mapping2, mapper.mappingSource().toString());
}
use of org.opensearch.index.mapper.MapperService.MergeReason in project OpenSearch by opensearch-project.
the class DocumentMapperTests method testMergeObjectAndNested.
public void testMergeObjectAndNested() throws Exception {
DocumentMapper objectMapper = createDocumentMapper(mapping(b -> b.startObject("obj").field("type", "object").endObject()));
DocumentMapper nestedMapper = createDocumentMapper(mapping(b -> b.startObject("obj").field("type", "nested").endObject()));
MergeReason reason = randomFrom(MergeReason.MAPPING_UPDATE, MergeReason.INDEX_TEMPLATE);
{
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> objectMapper.merge(nestedMapper.mapping(), reason));
assertThat(e.getMessage(), containsString("cannot change object mapping from non-nested to nested"));
}
{
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> nestedMapper.merge(objectMapper.mapping(), reason));
assertThat(e.getMessage(), containsString("cannot change object mapping from nested to non-nested"));
}
}
use of org.opensearch.index.mapper.MapperService.MergeReason in project OpenSearch by opensearch-project.
the class DocumentMapperTests method testAddFields.
public void testAddFields() throws Exception {
DocumentMapper stage1 = createDocumentMapper(mapping(b -> b.startObject("name").field("type", "text").endObject()));
DocumentMapper stage2 = createDocumentMapper(mapping(b -> {
b.startObject("name").field("type", "text").endObject();
b.startObject("age").field("type", "integer").endObject();
b.startObject("obj1");
{
b.startObject("properties");
{
b.startObject("prop1").field("type", "integer").endObject();
}
b.endObject();
}
b.endObject();
}));
MergeReason reason = randomFrom(MergeReason.MAPPING_UPDATE, MergeReason.INDEX_TEMPLATE);
DocumentMapper merged = stage1.merge(stage2.mapping(), reason);
// stage1 mapping should not have been modified
assertThat(stage1.mappers().getMapper("age"), nullValue());
assertThat(stage1.mappers().getMapper("obj1.prop1"), nullValue());
// but merged should
assertThat(merged.mappers().getMapper("age"), notNullValue());
assertThat(merged.mappers().getMapper("obj1.prop1"), notNullValue());
}
use of org.opensearch.index.mapper.MapperService.MergeReason in project OpenSearch by opensearch-project.
the class RootObjectMapperTests method testNumericDetection.
public void testNumericDetection() throws Exception {
MergeReason reason = randomFrom(MergeReason.MAPPING_UPDATE, MergeReason.INDEX_TEMPLATE);
String mapping = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").field("numeric_detection", false).endObject().endObject());
MapperService mapperService = createIndex("test").mapperService();
DocumentMapper mapper = mapperService.merge("type", new CompressedXContent(mapping), reason);
assertEquals(mapping, mapper.mappingSource().toString());
// update with a different explicit value
String mapping2 = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").field("numeric_detection", true).endObject().endObject());
mapper = mapperService.merge("type", new CompressedXContent(mapping2), reason);
assertEquals(mapping2, mapper.mappingSource().toString());
// update with an implicit value: no change
String mapping3 = Strings.toString(XContentFactory.jsonBuilder().startObject().startObject("type").endObject().endObject());
mapper = mapperService.merge("type", new CompressedXContent(mapping3), reason);
assertEquals(mapping2, mapper.mappingSource().toString());
}
Aggregations