Search in sources :

Example 1 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class MapperService method internalMerge.

private synchronized Map<String, DocumentMapper> internalMerge(Map<String, CompressedXContent> mappings, MergeReason reason, boolean updateAllTypes) {
    DocumentMapper defaultMapper = null;
    String defaultMappingSource = null;
    if (mappings.containsKey(DEFAULT_MAPPING)) {
        // NOTE: never apply the default here
        try {
            defaultMapper = documentParser.parse(DEFAULT_MAPPING, mappings.get(DEFAULT_MAPPING));
        } catch (Exception e) {
            throw new MapperParsingException("Failed to parse mapping [{}]: {}", e, DEFAULT_MAPPING, e.getMessage());
        }
        try {
            defaultMappingSource = mappings.get(DEFAULT_MAPPING).string();
        } catch (IOException e) {
            throw new ElasticsearchGenerationException("failed to un-compress", e);
        }
    }
    final String defaultMappingSourceOrLastStored;
    if (defaultMappingSource != null) {
        defaultMappingSourceOrLastStored = defaultMappingSource;
    } else {
        defaultMappingSourceOrLastStored = this.defaultMappingSource;
    }
    List<DocumentMapper> documentMappers = new ArrayList<>();
    for (Map.Entry<String, CompressedXContent> entry : mappings.entrySet()) {
        String type = entry.getKey();
        if (type.equals(DEFAULT_MAPPING)) {
            continue;
        }
        final boolean applyDefault = // the default was already applied if we are recovering
        reason != MergeReason.MAPPING_RECOVERY && // only apply the default mapping if we don't have the type yet
        mappers.containsKey(type) == false;
        try {
            DocumentMapper documentMapper = documentParser.parse(type, entry.getValue(), applyDefault ? defaultMappingSourceOrLastStored : null);
            documentMappers.add(documentMapper);
        } catch (Exception e) {
            throw new MapperParsingException("Failed to parse mapping [{}]: {}", e, entry.getKey(), e.getMessage());
        }
    }
    return internalMerge(defaultMapper, defaultMappingSource, documentMappers, reason, updateAllTypes);
}
Also used : ArrayList(java.util.ArrayList) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) IOException(java.io.IOException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) InvalidTypeNameException(org.elasticsearch.indices.InvalidTypeNameException) ElasticsearchGenerationException(org.elasticsearch.ElasticsearchGenerationException) IOException(java.io.IOException) TypeMissingException(org.elasticsearch.indices.TypeMissingException) ElasticsearchGenerationException(org.elasticsearch.ElasticsearchGenerationException)

Example 2 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class MapperService method assertSerialization.

private boolean assertSerialization(DocumentMapper mapper) {
    // capture the source now, it may change due to concurrent parsing
    final CompressedXContent mappingSource = mapper.mappingSource();
    DocumentMapper newMapper = parse(mapper.type(), mappingSource, false);
    if (newMapper.mappingSource().equals(mappingSource) == false) {
        throw new IllegalStateException("DocumentMapper serialization result is different from source. \n--> Source [" + mappingSource + "]\n--> Result [" + newMapper.mappingSource() + "]");
    }
    return true;
}
Also used : CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 3 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class MetaDataMappingServiceTests method testMappingClusterStateUpdateDoesntChangeExistingIndices.

public void testMappingClusterStateUpdateDoesntChangeExistingIndices() throws Exception {
    final IndexService indexService = createIndex("test", client().admin().indices().prepareCreate("test").addMapping("type"));
    final CompressedXContent currentMapping = indexService.mapperService().documentMapper("type").mappingSource();
    final MetaDataMappingService mappingService = getInstanceFromNode(MetaDataMappingService.class);
    final ClusterService clusterService = getInstanceFromNode(ClusterService.class);
    // TODO - it will be nice to get a random mapping generator
    final PutMappingClusterStateUpdateRequest request = new PutMappingClusterStateUpdateRequest().type("type");
    request.source("{ \"properties\" { \"field\": { \"type\": \"string\" }}}");
    mappingService.putMappingExecutor.execute(clusterService.state(), Collections.singletonList(request));
    assertThat(indexService.mapperService().documentMapper("type").mappingSource(), equalTo(currentMapping));
}
Also used : ClusterService(org.elasticsearch.cluster.service.ClusterService) IndexService(org.elasticsearch.index.IndexService) PutMappingClusterStateUpdateRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingClusterStateUpdateRequest) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent)

Example 4 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class CategoryContextMappingTests method testIndexingWithMultipleContexts.

public void testIndexingWithMultipleContexts() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").startArray("contexts").startObject().field("name", "ctx").field("type", "category").endObject().startObject().field("name", "type").field("type", "category").endObject().endArray().endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    MappedFieldType completionFieldType = fieldMapper.fieldType();
    XContentBuilder builder = jsonBuilder().startObject().startArray("completion").startObject().array("input", "suggestion5", "suggestion6", "suggestion7").field("weight", 5).startObject("contexts").array("ctx", "ctx1", "ctx2", "ctx3").array("type", "typr3", "ftg").endObject().endObject().endArray().endObject();
    ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", builder.bytes());
    IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name());
    assertContextSuggestFields(fields, 3);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 5 with CompressedXContent

use of org.elasticsearch.common.compress.CompressedXContent in project elasticsearch by elastic.

the class CategoryContextMappingTests method testIndexingWithNoContexts.

public void testIndexingWithNoContexts() throws Exception {
    String mapping = jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("completion").field("type", "completion").startArray("contexts").startObject().field("name", "ctx").field("type", "category").endObject().endArray().endObject().endObject().endObject().endObject().string();
    DocumentMapper defaultMapper = createIndex("test").mapperService().documentMapperParser().parse("type1", new CompressedXContent(mapping));
    FieldMapper fieldMapper = defaultMapper.mappers().getMapper("completion");
    MappedFieldType completionFieldType = fieldMapper.fieldType();
    ParsedDocument parsedDocument = defaultMapper.parse("test", "type1", "1", jsonBuilder().startObject().startArray("completion").startObject().array("input", "suggestion1", "suggestion2").field("weight", 3).endObject().startObject().array("input", "suggestion3", "suggestion4").field("weight", 4).endObject().startObject().array("input", "suggestion5", "suggestion6", "suggestion7").field("weight", 5).endObject().endArray().endObject().bytes());
    IndexableField[] fields = parsedDocument.rootDoc().getFields(completionFieldType.name());
    assertContextSuggestFields(fields, 7);
}
Also used : IndexableField(org.apache.lucene.index.IndexableField) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) FieldMapper(org.elasticsearch.index.mapper.FieldMapper)

Aggregations

CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)366 Matchers.containsString (org.hamcrest.Matchers.containsString)223 IndexableField (org.apache.lucene.index.IndexableField)75 BytesReference (org.elasticsearch.common.bytes.BytesReference)62 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)51 IndexService (org.elasticsearch.index.IndexService)49 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)46 Settings (org.elasticsearch.common.settings.Settings)25 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)19 BytesRef (org.apache.lucene.util.BytesRef)18 BytesArray (org.elasticsearch.common.bytes.BytesArray)16 MapperParsingException (org.elasticsearch.index.mapper.MapperParsingException)16 IOException (java.io.IOException)15 DocumentMapperParser (org.elasticsearch.index.mapper.DocumentMapperParser)15 Map (java.util.Map)13 Document (org.elasticsearch.index.mapper.ParseContext.Document)13 MapperService (org.elasticsearch.index.mapper.MapperService)12 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)10 IndexTemplateMetadata (org.elasticsearch.cluster.metadata.IndexTemplateMetadata)9 FieldMapper (org.elasticsearch.index.mapper.FieldMapper)9