Search in sources :

Example 1 with MapperParsingException

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

the class TransportShardBulkAction method executeIndexRequestOnReplica.

/**
     * Execute the given {@link IndexRequest} on a replica shard, throwing a
     * {@link RetryOnReplicaException} if the operation needs to be re-tried.
     */
public static Engine.IndexResult executeIndexRequestOnReplica(DocWriteResponse primaryResponse, IndexRequest request, IndexShard replica) throws IOException {
    final ShardId shardId = replica.shardId();
    SourceToParse sourceToParse = SourceToParse.source(SourceToParse.Origin.REPLICA, shardId.getIndexName(), request.type(), request.id(), request.source(), request.getContentType()).routing(request.routing()).parent(request.parent());
    final Engine.Index operation;
    final long version = primaryResponse.getVersion();
    final VersionType versionType = request.versionType().versionTypeForReplicationAndRecovery();
    assert versionType.validateVersionForWrites(version);
    final long seqNo = primaryResponse.getSeqNo();
    try {
        operation = replica.prepareIndexOnReplica(sourceToParse, seqNo, version, versionType, request.getAutoGeneratedTimestamp(), request.isRetry());
    } catch (MapperParsingException e) {
        return new Engine.IndexResult(e, version, seqNo);
    }
    Mapping update = operation.parsedDoc().dynamicMappingsUpdate();
    if (update != null) {
        throw new RetryOnReplicaException(shardId, "Mappings are not available on the replica yet, triggered update: " + update);
    }
    return replica.index(operation);
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) SourceToParse(org.elasticsearch.index.mapper.SourceToParse) Mapping(org.elasticsearch.index.mapper.Mapping) Engine(org.elasticsearch.index.engine.Engine) VersionType(org.elasticsearch.index.VersionType)

Example 2 with MapperParsingException

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

the class MetaDataIndexTemplateService method validateAndAddTemplate.

private static void validateAndAddTemplate(final PutRequest request, IndexTemplateMetaData.Builder templateBuilder, IndicesService indicesService, NamedXContentRegistry xContentRegistry) throws Exception {
    Index createdIndex = null;
    final String temporaryIndexName = UUIDs.randomBase64UUID();
    try {
        // use the provided values, otherwise just pick valid dummy values
        int dummyPartitionSize = IndexMetaData.INDEX_ROUTING_PARTITION_SIZE_SETTING.get(request.settings);
        int dummyShards = request.settings.getAsInt(IndexMetaData.SETTING_NUMBER_OF_SHARDS, dummyPartitionSize == 1 ? 1 : dummyPartitionSize + 1);
        //create index service for parsing and validating "mappings"
        Settings dummySettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(request.settings).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, dummyShards).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID()).build();
        final IndexMetaData tmpIndexMetadata = IndexMetaData.builder(temporaryIndexName).settings(dummySettings).build();
        IndexService dummyIndexService = indicesService.createIndex(tmpIndexMetadata, Collections.emptyList(), shardId -> {
        });
        createdIndex = dummyIndexService.index();
        templateBuilder.order(request.order);
        templateBuilder.version(request.version);
        templateBuilder.patterns(request.indexPatterns);
        templateBuilder.settings(request.settings);
        Map<String, Map<String, Object>> mappingsForValidation = new HashMap<>();
        for (Map.Entry<String, String> entry : request.mappings.entrySet()) {
            try {
                templateBuilder.putMapping(entry.getKey(), entry.getValue());
            } catch (Exception e) {
                throw new MapperParsingException("Failed to parse mapping [{}]: {}", e, entry.getKey(), e.getMessage());
            }
            mappingsForValidation.put(entry.getKey(), MapperService.parseMapping(xContentRegistry, entry.getValue()));
        }
        dummyIndexService.mapperService().merge(mappingsForValidation, MergeReason.MAPPING_UPDATE, false);
    } finally {
        if (createdIndex != null) {
            indicesService.removeIndex(createdIndex, NO_LONGER_ASSIGNED, " created for parsing template mapping");
        }
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) IndexService(org.elasticsearch.index.IndexService) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) HashMap(java.util.HashMap) Map(java.util.Map) Settings(org.elasticsearch.common.settings.Settings) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) ValidationException(org.elasticsearch.common.ValidationException) MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) InvalidIndexTemplateException(org.elasticsearch.indices.InvalidIndexTemplateException) IndexTemplateMissingException(org.elasticsearch.indices.IndexTemplateMissingException)

Example 3 with MapperParsingException

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

the class PercolatorFieldMapperTests method testRangeQueryWithNowRangeIsForbidden.

public void testRangeQueryWithNowRangeIsForbidden() throws Exception {
    addQueryMapping();
    MapperParsingException e = expectThrows(MapperParsingException.class, () -> {
        mapperService.documentMapper(typeName).parse("test", typeName, "1", jsonBuilder().startObject().field(fieldName, rangeQuery("date_field").from("2016-01-01||/D").to("now")).endObject().bytes());
    });
    assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
    e = expectThrows(MapperParsingException.class, () -> {
        mapperService.documentMapper(typeName).parse("test", typeName, "1", jsonBuilder().startObject().field(fieldName, rangeQuery("date_field").from("2016-01-01||/D").to("now/D")).endObject().bytes());
    });
    assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
    e = expectThrows(MapperParsingException.class, () -> {
        mapperService.documentMapper(typeName).parse("test", typeName, "1", jsonBuilder().startObject().field(fieldName, rangeQuery("date_field").from("now-1d").to("now")).endObject().bytes());
    });
    assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
    e = expectThrows(MapperParsingException.class, () -> {
        mapperService.documentMapper(typeName).parse("test", typeName, "1", jsonBuilder().startObject().field(fieldName, rangeQuery("date_field").from("now")).endObject().bytes());
    });
    assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
    e = expectThrows(MapperParsingException.class, () -> {
        mapperService.documentMapper(typeName).parse("test", typeName, "1", jsonBuilder().startObject().field(fieldName, rangeQuery("date_field").to("now")).endObject().bytes());
    });
    assertThat(e.getCause(), instanceOf(IllegalArgumentException.class));
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException)

Example 4 with MapperParsingException

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

the class Murmur3FieldMapperTests method testDocValuesSettingNotAllowed.

public void testDocValuesSettingNotAllowed() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "murmur3").field("doc_values", false).endObject().endObject().endObject().endObject().string();
    try {
        parser.parse("type", new CompressedXContent(mapping));
        fail("expected a mapper parsing exception");
    } catch (MapperParsingException e) {
        assertTrue(e.getMessage().contains("Setting [doc_values] cannot be modified"));
    }
    // even setting to the default is not allowed, the setting is invalid
    mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "murmur3").field("doc_values", true).endObject().endObject().endObject().endObject().string();
    try {
        parser.parse("type", new CompressedXContent(mapping));
        fail("expected a mapper parsing exception");
    } catch (MapperParsingException e) {
        assertTrue(e.getMessage().contains("Setting [doc_values] cannot be modified"));
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Example 5 with MapperParsingException

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

the class Murmur3FieldMapperTests method testIndexSettingNotAllowed.

public void testIndexSettingNotAllowed() throws Exception {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "murmur3").field("index", "not_analyzed").endObject().endObject().endObject().endObject().string();
    try {
        parser.parse("type", new CompressedXContent(mapping));
        fail("expected a mapper parsing exception");
    } catch (MapperParsingException e) {
        assertTrue(e.getMessage().contains("Setting [index] cannot be modified"));
    }
    // even setting to the default is not allowed, the setting is invalid
    mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field").field("type", "murmur3").field("index", "no").endObject().endObject().endObject().endObject().string();
    try {
        parser.parse("type", new CompressedXContent(mapping));
        fail("expected a mapper parsing exception");
    } catch (MapperParsingException e) {
        assertTrue(e.getMessage().contains("Setting [index] cannot be modified"));
    }
}
Also used : MapperParsingException(org.elasticsearch.index.mapper.MapperParsingException) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.containsString(org.hamcrest.Matchers.containsString)

Aggregations

MapperParsingException (org.elasticsearch.index.mapper.MapperParsingException)35 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)16 Matchers.containsString (org.hamcrest.Matchers.containsString)11 IndexService (org.elasticsearch.index.IndexService)7 Settings (org.elasticsearch.common.settings.Settings)5 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)5 Index (org.elasticsearch.index.Index)4 MapperService (org.elasticsearch.index.mapper.MapperService)4 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 DocumentMapperParser (org.elasticsearch.index.mapper.DocumentMapperParser)3 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 ClusterBlockException (org.elasticsearch.cluster.block.ClusterBlockException)2 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)2 ValidationException (org.elasticsearch.common.ValidationException)2 IndexScopedSettings (org.elasticsearch.common.settings.IndexScopedSettings)2 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)2