Search in sources :

Example 21 with MappingMetaData

use of org.elasticsearch.cluster.metadata.MappingMetaData in project elasticsearch by elastic.

the class GetMappingsResponse method readFrom.

@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    int size = in.readVInt();
    ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetaData>> indexMapBuilder = ImmutableOpenMap.builder();
    for (int i = 0; i < size; i++) {
        String key = in.readString();
        int valueSize = in.readVInt();
        ImmutableOpenMap.Builder<String, MappingMetaData> typeMapBuilder = ImmutableOpenMap.builder();
        for (int j = 0; j < valueSize; j++) {
            typeMapBuilder.put(in.readString(), new MappingMetaData(in));
        }
        indexMapBuilder.put(key, typeMapBuilder.build());
    }
    mappings = indexMapBuilder.build();
}
Also used : ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData)

Example 22 with MappingMetaData

use of org.elasticsearch.cluster.metadata.MappingMetaData in project elasticsearch by elastic.

the class SpecificMasterNodesIT method testCustomDefaultMapping.

/**
     * Tests that putting custom default mapping and then putting a type mapping will have the default mapping merged
     * to the type mapping.
     */
public void testCustomDefaultMapping() throws Exception {
    logger.info("--> start master node / non data");
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), false).put(Node.NODE_MASTER_SETTING.getKey(), true));
    logger.info("--> start data node / non master node");
    internalCluster().startNode(Settings.builder().put(Node.NODE_DATA_SETTING.getKey(), true).put(Node.NODE_MASTER_SETTING.getKey(), false));
    createIndex("test");
    assertAcked(client().admin().indices().preparePutMapping("test").setType("_default_").setSource("timestamp", "type=date"));
    MappingMetaData defaultMapping = client().admin().cluster().prepareState().get().getState().getMetaData().getIndices().get("test").getMappings().get("_default_");
    Map<?, ?> properties = (Map<?, ?>) defaultMapping.getSourceAsMap().get("properties");
    assertThat(properties.get("timestamp"), notNullValue());
    assertAcked(client().admin().indices().preparePutMapping("test").setType("_default_").setSource("timestamp", "type=date"));
    assertAcked(client().admin().indices().preparePutMapping("test").setType("type1").setSource("foo", "enabled=true"));
    MappingMetaData type1Mapping = client().admin().cluster().prepareState().get().getState().getMetaData().getIndices().get("test").getMappings().get("type1");
    properties = (Map<?, ?>) type1Mapping.getSourceAsMap().get("properties");
    assertThat(properties.get("timestamp"), notNullValue());
}
Also used : MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) Map(java.util.Map)

Example 23 with MappingMetaData

use of org.elasticsearch.cluster.metadata.MappingMetaData in project elasticsearch by elastic.

the class StoreRecovery method recoverFromLocalShards.

boolean recoverFromLocalShards(BiConsumer<String, MappingMetaData> mappingUpdateConsumer, final IndexShard indexShard, final List<LocalShardSnapshot> shards) throws IOException {
    if (canRecover(indexShard)) {
        RecoverySource.Type recoveryType = indexShard.recoveryState().getRecoverySource().getType();
        assert recoveryType == RecoverySource.Type.LOCAL_SHARDS : "expected local shards recovery type: " + recoveryType;
        if (shards.isEmpty()) {
            throw new IllegalArgumentException("shards must not be empty");
        }
        Set<Index> indices = shards.stream().map((s) -> s.getIndex()).collect(Collectors.toSet());
        if (indices.size() > 1) {
            throw new IllegalArgumentException("can't add shards from more than one index");
        }
        IndexMetaData indexMetaData = shards.get(0).getIndexMetaData();
        for (ObjectObjectCursor<String, MappingMetaData> mapping : indexMetaData.getMappings()) {
            mappingUpdateConsumer.accept(mapping.key, mapping.value);
        }
        indexShard.mapperService().merge(indexMetaData, MapperService.MergeReason.MAPPING_RECOVERY, true);
        return executeRecovery(indexShard, () -> {
            logger.debug("starting recovery from local shards {}", shards);
            try {
                // don't close this directory!!
                final Directory directory = indexShard.store().directory();
                addIndices(indexShard.recoveryState().getIndex(), directory, shards.stream().map(s -> s.getSnapshotDirectory()).collect(Collectors.toList()).toArray(new Directory[shards.size()]));
                internalRecoverFromStore(indexShard);
                // just trigger a merge to do housekeeping on the
                // copied segments - we will also see them in stats etc.
                indexShard.getEngine().forceMerge(false, -1, false, false, false);
            } catch (IOException ex) {
                throw new IndexShardRecoveryException(indexShard.shardId(), "failed to recover from local shards", ex);
            }
        });
    }
    return false;
}
Also used : NoMergePolicy(org.apache.lucene.index.NoMergePolicy) Arrays(java.util.Arrays) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) SnapshotRecoverySource(org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Index(org.elasticsearch.index.Index) Lucene(org.elasticsearch.common.lucene.Lucene) IndexId(org.elasticsearch.repositories.IndexId) ObjectObjectCursor(com.carrotsearch.hppc.cursors.ObjectObjectCursor) IndexRequest(org.elasticsearch.action.index.IndexRequest) Directory(org.apache.lucene.store.Directory) TimeValue(org.elasticsearch.common.unit.TimeValue) Store(org.elasticsearch.index.store.Store) BiConsumer(java.util.function.BiConsumer) TimeValue.timeValueMillis(org.elasticsearch.common.unit.TimeValue.timeValueMillis) IndexShardRestoreFailedException(org.elasticsearch.index.snapshots.IndexShardRestoreFailedException) IOContext(org.apache.lucene.store.IOContext) EngineException(org.elasticsearch.index.engine.EngineException) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) Repository(org.elasticsearch.repositories.Repository) IndexInput(org.apache.lucene.store.IndexInput) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) SegmentInfos(org.apache.lucene.index.SegmentInfos) ExceptionsHelper(org.elasticsearch.ExceptionsHelper) RecoverySource(org.elasticsearch.cluster.routing.RecoverySource) MapperService(org.elasticsearch.index.mapper.MapperService) IndexWriter(org.apache.lucene.index.IndexWriter) List(java.util.List) Logger(org.apache.logging.log4j.Logger) FilterDirectory(org.apache.lucene.store.FilterDirectory) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) Index(org.elasticsearch.index.Index) IOException(java.io.IOException) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) SnapshotRecoverySource(org.elasticsearch.cluster.routing.RecoverySource.SnapshotRecoverySource) RecoverySource(org.elasticsearch.cluster.routing.RecoverySource) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Directory(org.apache.lucene.store.Directory) FilterDirectory(org.apache.lucene.store.FilterDirectory)

Example 24 with MappingMetaData

use of org.elasticsearch.cluster.metadata.MappingMetaData in project elasticsearch by elastic.

the class GetIndexIT method assertMappings.

private void assertMappings(GetIndexResponse response, String indexName) {
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = response.mappings();
    assertThat(mappings, notNullValue());
    assertThat(mappings.size(), equalTo(1));
    ImmutableOpenMap<String, MappingMetaData> indexMappings = mappings.get(indexName);
    assertThat(indexMappings, notNullValue());
    assertThat(indexMappings.size(), anyOf(equalTo(1), equalTo(2)));
    if (indexMappings.size() == 2) {
        MappingMetaData mapping = indexMappings.get("_default_");
        assertThat(mapping, notNullValue());
    }
    MappingMetaData mapping = indexMappings.get("type1");
    assertThat(mapping, notNullValue());
    assertThat(mapping.type(), equalTo("type1"));
}
Also used : ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData)

Example 25 with MappingMetaData

use of org.elasticsearch.cluster.metadata.MappingMetaData in project sonarqube by SonarSource.

the class IndexCreatorTest method create_index.

@Test
public void create_index() throws Exception {
    assertThat(mappings()).isEmpty();
    IndexDefinitions registry = new IndexDefinitions(new IndexDefinition[] { new FakeIndexDefinition() }, new MapSettings());
    registry.start();
    IndexCreator creator = new IndexCreator(es.client(), registry);
    creator.start();
    // check that index is created with related mapping
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = mappings();
    MappingMetaData mapping = mappings.get("fakes").get("fake");
    assertThat(mapping.type()).isEqualTo("fake");
    assertThat(mapping.getSourceAsMap()).isNotEmpty();
    assertThat(countMappingFields(mapping)).isEqualTo(2);
    assertThat(field(mapping, "updatedAt").get("type")).isEqualTo("date");
    assertThat(setting("fakes", "index.sonar_hash")).isNotEmpty();
    // of course do not delete indices on stop
    creator.stop();
    assertThat(mappings()).isNotEmpty();
}
Also used : MapSettings(org.sonar.api.config.MapSettings) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) Test(org.junit.Test)

Aggregations

MappingMetaData (org.elasticsearch.cluster.metadata.MappingMetaData)45 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)15 Map (java.util.Map)14 GetMappingsResponse (org.elasticsearch.action.admin.indices.mapping.get.GetMappingsResponse)11 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)10 Settings (org.elasticsearch.common.settings.Settings)9 Test (org.junit.Test)9 IOException (java.io.IOException)8 IndexTemplateMetaData (org.elasticsearch.cluster.metadata.IndexTemplateMetaData)7 PartitionName (io.crate.metadata.PartitionName)6 HashMap (java.util.HashMap)6 BytesRef (org.apache.lucene.util.BytesRef)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 List (java.util.List)4 Set (java.util.Set)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)4 ObjectObjectCursor (com.carrotsearch.hppc.cursors.ObjectObjectCursor)3 ArrayList (java.util.ArrayList)3 GetIndexTemplatesResponse (org.elasticsearch.action.admin.indices.template.get.GetIndexTemplatesResponse)3