Search in sources :

Example 1 with DocumentMapperParser

use of org.elasticsearch.index.mapper.DocumentMapperParser in project elasticsearch by elastic.

the class PercolatorFieldMapperTests method testEmptyName.

public void testEmptyName() throws Exception {
    // after 5.x
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("").field("type", "percolator").endObject().endObject().endObject().endObject().string();
    DocumentMapperParser parser = mapperService.documentMapperParser();
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse("type1", new CompressedXContent(mapping)));
    assertThat(e.getMessage(), containsString("name cannot be empty string"));
    // before 5.x
    Version oldVersion = VersionUtils.randomVersionBetween(getRandom(), Version.V_2_0_0, Version.V_2_3_5);
    Settings oldIndexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, oldVersion).build();
    DocumentMapperParser parser2x = createIndex("test_old", oldIndexSettings).mapperService().documentMapperParser();
    DocumentMapper defaultMapper = parser2x.parse("type1", new CompressedXContent(mapping));
    assertEquals(mapping, defaultMapper.mappingSource().string());
}
Also used : Version(org.elasticsearch.Version) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) DocumentMapperParser(org.elasticsearch.index.mapper.DocumentMapperParser) Settings(org.elasticsearch.common.settings.Settings)

Example 2 with DocumentMapperParser

use of org.elasticsearch.index.mapper.DocumentMapperParser in project elasticsearch by elastic.

the class Murmur3FieldMapperTests method testEmptyName.

public void testEmptyName() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("").field("type", "murmur3").endObject().endObject().endObject().endObject().string();
    IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> parser.parse("type", new CompressedXContent(mapping)));
    assertThat(e.getMessage(), containsString("name cannot be empty string"));
    // before 5.x
    Version oldVersion = VersionUtils.randomVersionBetween(getRandom(), Version.V_2_0_0, Version.V_2_3_5);
    Settings oldIndexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, oldVersion).build();
    IndexService indexService2x = createIndex("test_old", oldIndexSettings);
    Supplier<QueryShardContext> queryShardContext = () -> {
        return indexService2x.newQueryShardContext(0, null, () -> {
            throw new UnsupportedOperationException();
        });
    };
    DocumentMapperParser parser = new DocumentMapperParser(indexService2x.getIndexSettings(), indexService2x.mapperService(), indexService2x.getIndexAnalyzers(), indexService2x.xContentRegistry(), indexService2x.similarityService(), mapperRegistry, queryShardContext);
    DocumentMapper defaultMapper = parser.parse("type", new CompressedXContent(mapping));
    assertEquals(mapping, defaultMapper.mappingSource().string());
}
Also used : Version(org.elasticsearch.Version) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Matchers.containsString(org.hamcrest.Matchers.containsString) DocumentMapperParser(org.elasticsearch.index.mapper.DocumentMapperParser) Settings(org.elasticsearch.common.settings.Settings)

Example 3 with DocumentMapperParser

use of org.elasticsearch.index.mapper.DocumentMapperParser in project elasticsearch by elastic.

the class DocumentMapperMergeTests method testMergeObjectDynamic.

public void testMergeObjectDynamic() throws Exception {
    DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
    String objectMapping = XContentFactory.jsonBuilder().startObject().startObject("type1").endObject().endObject().string();
    DocumentMapper mapper = parser.parse("type1", new CompressedXContent(objectMapping));
    assertNull(mapper.root().dynamic());
    String withDynamicMapping = XContentFactory.jsonBuilder().startObject().startObject("type1").field("dynamic", "false").endObject().endObject().string();
    DocumentMapper withDynamicMapper = parser.parse("type1", new CompressedXContent(withDynamicMapping));
    assertThat(withDynamicMapper.root().dynamic(), equalTo(ObjectMapper.Dynamic.FALSE));
    DocumentMapper merged = mapper.merge(withDynamicMapper.mapping(), false);
    assertThat(merged.root().dynamic(), equalTo(ObjectMapper.Dynamic.FALSE));
}
Also used : DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) DocumentMapperParser(org.elasticsearch.index.mapper.DocumentMapperParser)

Example 4 with DocumentMapperParser

use of org.elasticsearch.index.mapper.DocumentMapperParser in project elasticsearch by elastic.

the class DocumentMapperMergeTests method testMergeSearchAnalyzer.

public void testMergeSearchAnalyzer() throws Exception {
    DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
    String mapping1 = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "text").field("analyzer", "standard").field("search_analyzer", "whitespace").endObject().endObject().endObject().endObject().string();
    String mapping2 = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "text").field("analyzer", "standard").field("search_analyzer", "keyword").endObject().endObject().endObject().endObject().string();
    DocumentMapper existing = parser.parse("type", new CompressedXContent(mapping1));
    DocumentMapper changed = parser.parse("type", new CompressedXContent(mapping2));
    assertThat(((NamedAnalyzer) existing.mappers().getMapper("field").fieldType().searchAnalyzer()).name(), equalTo("whitespace"));
    DocumentMapper merged = existing.merge(changed.mapping(), false);
    assertThat(((NamedAnalyzer) merged.mappers().getMapper("field").fieldType().searchAnalyzer()).name(), equalTo("keyword"));
}
Also used : DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) DocumentMapperParser(org.elasticsearch.index.mapper.DocumentMapperParser)

Example 5 with DocumentMapperParser

use of org.elasticsearch.index.mapper.DocumentMapperParser in project elasticsearch by elastic.

the class DocumentMapperMergeTests method test1Merge.

public void test1Merge() throws Exception {
    String stage1Mapping = XContentFactory.jsonBuilder().startObject().startObject("person").startObject("properties").startObject("name").field("type", "text").endObject().endObject().endObject().endObject().string();
    DocumentMapperParser parser = createIndex("test").mapperService().documentMapperParser();
    DocumentMapper stage1 = parser.parse("person", new CompressedXContent(stage1Mapping));
    String stage2Mapping = XContentFactory.jsonBuilder().startObject().startObject("person").startObject("properties").startObject("name").field("type", "text").endObject().startObject("age").field("type", "integer").endObject().startObject("obj1").startObject("properties").startObject("prop1").field("type", "integer").endObject().endObject().endObject().endObject().endObject().endObject().string();
    DocumentMapper stage2 = parser.parse("person", new CompressedXContent(stage2Mapping));
    DocumentMapper merged = stage1.merge(stage2.mapping(), false);
    // stage1 mapping should not have been modified
    assertThat(stage1.mappers().smartNameFieldMapper("age"), nullValue());
    assertThat(stage1.mappers().smartNameFieldMapper("obj1.prop1"), nullValue());
    // but merged should
    assertThat(merged.mappers().smartNameFieldMapper("age"), notNullValue());
    assertThat(merged.mappers().smartNameFieldMapper("obj1.prop1"), notNullValue());
}
Also used : DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString) DocumentMapperParser(org.elasticsearch.index.mapper.DocumentMapperParser)

Aggregations

DocumentMapperParser (org.elasticsearch.index.mapper.DocumentMapperParser)17 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)15 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)11 Matchers.containsString (org.hamcrest.Matchers.containsString)7 Settings (org.elasticsearch.common.settings.Settings)4 IndexService (org.elasticsearch.index.IndexService)4 MapperParsingException (org.elasticsearch.index.mapper.MapperParsingException)4 Version (org.elasticsearch.Version)2 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)2 Before (org.junit.Before)2 KeywordAnalyzer (org.apache.lucene.analysis.core.KeywordAnalyzer)1 ImmutableSettings (org.elasticsearch.common.settings.ImmutableSettings)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 Index (org.elasticsearch.index.Index)1 AnalysisService (org.elasticsearch.index.analysis.AnalysisService)1 AnalyzerProviderFactory (org.elasticsearch.index.analysis.AnalyzerProviderFactory)1 PreBuiltAnalyzerProviderFactory (org.elasticsearch.index.analysis.PreBuiltAnalyzerProviderFactory)1 PostingsFormatService (org.elasticsearch.index.codec.postingsformat.PostingsFormatService)1 MapperService (org.elasticsearch.index.mapper.MapperService)1 OpenNlpMapper (org.elasticsearch.index.mapper.opennlp.OpenNlpMapper)1