Search in sources :

Example 11 with DocumentMapper

use of org.elasticsearch.index.mapper.DocumentMapper 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 12 with DocumentMapper

use of org.elasticsearch.index.mapper.DocumentMapper 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 13 with DocumentMapper

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

the class SizeMappingTests method testSizeEnabled.

public void testSizeEnabled() throws Exception {
    IndexService service = createIndex("test", Settings.EMPTY, "type", "_size", "enabled=true");
    DocumentMapper docMapper = service.mapperService().documentMapper("type");
    BytesReference source = XContentFactory.jsonBuilder().startObject().field("field", "value").endObject().bytes();
    ParsedDocument doc = docMapper.parse(SourceToParse.source("test", "type", "1", source, XContentType.JSON));
    boolean stored = false;
    boolean points = false;
    for (IndexableField field : doc.rootDoc().getFields("_size")) {
        stored |= field.fieldType().stored();
        points |= field.fieldType().pointDimensionCount() > 0;
    }
    assertTrue(stored);
    assertTrue(points);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper)

Example 14 with DocumentMapper

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

the class SizeMappingTests method testThatDisablingWorksWhenMerging.

public void testThatDisablingWorksWhenMerging() throws Exception {
    IndexService service = createIndex("test", Settings.EMPTY, "type", "_size", "enabled=true");
    DocumentMapper docMapper = service.mapperService().documentMapper("type");
    assertThat(docMapper.metadataMapper(SizeFieldMapper.class).enabled(), is(true));
    String disabledMapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("_size").field("enabled", false).endObject().endObject().endObject().string();
    docMapper = service.mapperService().merge("type", new CompressedXContent(disabledMapping), MapperService.MergeReason.MAPPING_UPDATE, false);
    assertThat(docMapper.metadataMapper(SizeFieldMapper.class).enabled(), is(false));
}
Also used : IndexService(org.elasticsearch.index.IndexService) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 15 with DocumentMapper

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

the class PreBuiltAnalyzerTests method testThatAnalyzersAreUsedInMapping.

public void testThatAnalyzersAreUsedInMapping() throws IOException {
    int randomInt = randomInt(PreBuiltAnalyzers.values().length - 1);
    PreBuiltAnalyzers randomPreBuiltAnalyzer = PreBuiltAnalyzers.values()[randomInt];
    String analyzerName = randomPreBuiltAnalyzer.name().toLowerCase(Locale.ROOT);
    Version randomVersion = randomVersion(random());
    Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, randomVersion).build();
    NamedAnalyzer namedAnalyzer = new PreBuiltAnalyzerProvider(analyzerName, AnalyzerScope.INDEX, randomPreBuiltAnalyzer.getAnalyzer(randomVersion)).get();
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "text").field("analyzer", analyzerName).endObject().endObject().endObject().endObject().string();
    DocumentMapper docMapper = createIndex("test", indexSettings).mapperService().documentMapperParser().parse("type", new CompressedXContent(mapping));
    FieldMapper fieldMapper = docMapper.mappers().getMapper("field");
    assertThat(fieldMapper.fieldType().searchAnalyzer(), instanceOf(NamedAnalyzer.class));
    NamedAnalyzer fieldMapperNamedAnalyzer = fieldMapper.fieldType().searchAnalyzer();
    assertThat(fieldMapperNamedAnalyzer.analyzer(), is(namedAnalyzer.analyzer()));
}
Also used : Version(org.elasticsearch.Version) VersionUtils.randomVersion(org.elasticsearch.test.VersionUtils.randomVersion) PreBuiltAnalyzers(org.elasticsearch.indices.analysis.PreBuiltAnalyzers) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)96 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)51 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)35 IndexService (org.elasticsearch.index.IndexService)30 IndexableField (org.apache.lucene.index.IndexableField)20 BytesReference (org.elasticsearch.common.bytes.BytesReference)20 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)16 Matchers.containsString (org.hamcrest.Matchers.containsString)15 Settings (org.elasticsearch.common.settings.Settings)14 MapperService (org.elasticsearch.index.mapper.MapperService)14 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)12 DocumentMapperParser (org.elasticsearch.index.mapper.DocumentMapperParser)11 Test (org.junit.Test)11 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)10 Document (org.elasticsearch.index.mapper.ParseContext.Document)10 CrateDummyClusterServiceUnitTest (io.crate.test.integration.CrateDummyClusterServiceUnitTest)9 ArrayList (java.util.ArrayList)9 IOException (java.io.IOException)8 BytesArray (org.elasticsearch.common.bytes.BytesArray)8 Map (java.util.Map)7