Search in sources :

Example 6 with GetIndexRequest

use of org.elasticsearch.client.indices.GetIndexRequest in project sonarqube by SonarSource.

the class IndexCreator method start.

@Override
public void start() {
    // create the "metadata" index first
    IndexType.IndexMainType metadataMainType = TYPE_METADATA;
    if (!client.indexExists(new GetIndexRequest(metadataMainType.getIndex().getName()))) {
        IndexDefinition.IndexDefinitionContext context = new IndexDefinition.IndexDefinitionContext();
        metadataIndexDefinition.define(context);
        NewIndex index = context.getIndices().values().iterator().next();
        createIndex(index.build(), false);
    } else {
        ensureWritable(metadataMainType);
    }
    checkDbCompatibility(definitions.getIndices().values());
    // create indices that do not exist or that have a new definition (different mapping, cluster enabled, ...)
    definitions.getIndices().values().stream().filter(i -> !i.getMainType().equals(metadataMainType)).forEach(index -> {
        boolean exists = client.indexExists(new GetIndexRequest(index.getMainType().getIndex().getName()));
        if (!exists) {
            createIndex(index, true);
        } else if (hasDefinitionChange(index)) {
            updateIndex(index);
        } else {
            ensureWritable(index.getMainType());
        }
    });
}
Also used : Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) BuiltIndex(org.sonar.server.es.newindex.BuiltIndex) UpdateSettingsRequest(org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest) Loggers(org.sonar.api.utils.log.Loggers) Settings(org.elasticsearch.common.settings.Settings) Configuration(org.sonar.api.config.Configuration) MetadataIndex(org.sonar.server.es.metadata.MetadataIndex) MetadataIndexDefinition(org.sonar.server.es.metadata.MetadataIndexDefinition) DESCRIPTOR(org.sonar.server.es.metadata.MetadataIndexDefinition.DESCRIPTOR) Logger(org.sonar.api.utils.log.Logger) TYPE_METADATA(org.sonar.server.es.metadata.MetadataIndexDefinition.TYPE_METADATA) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) Startable(org.sonar.api.Startable) Collection(java.util.Collection) EsDbCompatibility(org.sonar.server.es.metadata.EsDbCompatibility) Set(java.util.Set) AcknowledgedResponse(org.elasticsearch.action.support.master.AcknowledgedResponse) Collectors(java.util.stream.Collectors) List(java.util.List) ProcessProperties(org.sonar.process.ProcessProperties) NewIndex(org.sonar.server.es.newindex.NewIndex) PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) ServerSide(org.sonar.api.server.ServerSide) CreateIndexResponse(org.elasticsearch.client.indices.CreateIndexResponse) MigrationEsClient(org.sonar.server.platform.db.migration.es.MigrationEsClient) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) GetSettingsRequest(org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) MetadataIndexDefinition(org.sonar.server.es.metadata.MetadataIndexDefinition) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) NewIndex(org.sonar.server.es.newindex.NewIndex)

Example 7 with GetIndexRequest

use of org.elasticsearch.client.indices.GetIndexRequest in project chili-core by codingchili.

the class ElasticMap method createIndexIfNotExists.

private Future<Void> createIndexIfNotExists() {
    Promise<Void> promise = Promise.promise();
    context.blocking((done) -> {
        IndicesClient indices = client.indices();
        try {
            var exists = indices.exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
            if (!exists) {
                var request = new CreateIndexRequest(index);
                configureMapping(request);
                configureSettings(request);
                indices.create(request, RequestOptions.DEFAULT);
            }
            done.complete();
        } catch (Throwable e) {
            done.fail(e);
        }
    }, promise);
    return promise.future();
}
Also used : GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest)

Example 8 with GetIndexRequest

use of org.elasticsearch.client.indices.GetIndexRequest in project datashare by ICIJ.

the class ElasticsearchConfiguration method createIndex.

public static boolean createIndex(RestHighLevelClient client, String indexName) {
    GetIndexRequest request = new GetIndexRequest(indexName);
    try {
        if (!client.indices().exists(request, RequestOptions.DEFAULT)) {
            LOGGER.info("index {} does not exist, creating one", indexName);
            CreateIndexRequest createReq = new CreateIndexRequest(indexName);
            createReq.settings(getResourceContent(SETTINGS_RESOURCE_NAME), JSON);
            createReq.mapping(getResourceContent(MAPPING_RESOURCE_NAME), JSON);
            client.indices().create(createReq, RequestOptions.DEFAULT);
            return true;
        }
    } catch (IOException e) {
        throw new ConfigurationException(e);
    }
    return false;
}
Also used : GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest)

Example 9 with GetIndexRequest

use of org.elasticsearch.client.indices.GetIndexRequest in project datashare by ICIJ.

the class ElasticsearchRule method before.

@Override
protected void before() throws Throwable {
    GetIndexRequest request = new GetIndexRequest(indexName);
    if (!client.indices().exists(request, RequestOptions.DEFAULT)) {
        CreateIndexRequest createReq = new CreateIndexRequest(indexName);
        byte[] settings = toByteArray(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream(SETTINGS_RESOURCE_NAME)));
        createReq.settings(new String(settings), JSON);
        byte[] mapping = toByteArray(Objects.requireNonNull(getClass().getClassLoader().getResourceAsStream(MAPPING_RESOURCE_NAME)));
        createReq.mapping(new String(mapping), JSON);
        client.indices().create(createReq, RequestOptions.DEFAULT);
    }
}
Also used : GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest)

Aggregations

GetIndexRequest (org.elasticsearch.client.indices.GetIndexRequest)9 CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)5 IOException (java.io.IOException)4 Map (java.util.Map)3 GoraException (org.apache.gora.util.GoraException)3 Arrays (java.util.Arrays)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)2 PutMappingRequest (org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest)2 GetIndexResponse (org.elasticsearch.client.indices.GetIndexResponse)2 Loggers (org.sonar.api.utils.log.Loggers)2 MigrationEsClient (org.sonar.server.platform.db.migration.es.MigrationEsClient)2 Maps (com.google.common.collect.Maps)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Stream (java.util.stream.Stream)1