Search in sources :

Example 1 with MergeReason

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());
}
Also used : CompressedXContent(org.opensearch.common.compress.CompressedXContent) MergeReason(org.opensearch.index.mapper.MapperService.MergeReason) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 2 with MergeReason

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());
}
Also used : CompressedXContent(org.opensearch.common.compress.CompressedXContent) MergeReason(org.opensearch.index.mapper.MapperService.MergeReason) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 3 with MergeReason

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"));
    }
}
Also used : NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) CyclicBarrier(java.util.concurrent.CyclicBarrier) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) CompressedXContent(org.opensearch.common.compress.CompressedXContent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) HashMap(java.util.HashMap) AnalyzerScope(org.opensearch.index.analysis.AnalyzerScope) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) AtomicReference(java.util.concurrent.atomic.AtomicReference) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) MergeReason(org.opensearch.index.mapper.MapperService.MergeReason) Map(java.util.Map) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.nullValue(org.hamcrest.Matchers.nullValue) IndexSettings(org.opensearch.index.IndexSettings) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) MergeReason(org.opensearch.index.mapper.MapperService.MergeReason)

Example 4 with MergeReason

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());
}
Also used : NamedAnalyzer(org.opensearch.index.analysis.NamedAnalyzer) CyclicBarrier(java.util.concurrent.CyclicBarrier) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) CompressedXContent(org.opensearch.common.compress.CompressedXContent) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IOException(java.io.IOException) HashMap(java.util.HashMap) AnalyzerScope(org.opensearch.index.analysis.AnalyzerScope) WhitespaceAnalyzer(org.apache.lucene.analysis.core.WhitespaceAnalyzer) AtomicReference(java.util.concurrent.atomic.AtomicReference) KeywordAnalyzer(org.apache.lucene.analysis.core.KeywordAnalyzer) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) MergeReason(org.opensearch.index.mapper.MapperService.MergeReason) Map(java.util.Map) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.nullValue(org.hamcrest.Matchers.nullValue) IndexSettings(org.opensearch.index.IndexSettings) Collections(java.util.Collections) Matchers.containsString(org.hamcrest.Matchers.containsString) IndexAnalyzers(org.opensearch.index.analysis.IndexAnalyzers) MergeReason(org.opensearch.index.mapper.MapperService.MergeReason)

Example 5 with MergeReason

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());
}
Also used : CompressedXContent(org.opensearch.common.compress.CompressedXContent) MergeReason(org.opensearch.index.mapper.MapperService.MergeReason) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

Matchers.containsString (org.hamcrest.Matchers.containsString)8 CompressedXContent (org.opensearch.common.compress.CompressedXContent)8 MergeReason (org.opensearch.index.mapper.MapperService.MergeReason)8 IOException (java.io.IOException)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)2 WhitespaceAnalyzer (org.apache.lucene.analysis.core.WhitespaceAnalyzer)2 StandardAnalyzer (org.apache.lucene.analysis.standard.StandardAnalyzer)2 IndexableField (org.apache.lucene.index.IndexableField)2 Matchers.equalTo (org.hamcrest.Matchers.equalTo)2 Matchers.notNullValue (org.hamcrest.Matchers.notNullValue)2 Matchers.nullValue (org.hamcrest.Matchers.nullValue)2 IndexSettings (org.opensearch.index.IndexSettings)2 AnalyzerScope (org.opensearch.index.analysis.AnalyzerScope)2 IndexAnalyzers (org.opensearch.index.analysis.IndexAnalyzers)2