Search in sources :

Example 1 with GetIndexRequest

use of org.graylog.shaded.elasticsearch7.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 2 with GetIndexRequest

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

the class EsRequestDetailsTest method should_format_GetIndexRequest.

@Test
public void should_format_GetIndexRequest() {
    GetIndexRequest request = new GetIndexRequest("index-1", "index-2");
    assertThat(EsRequestDetails.computeDetailsAsString(request)).isEqualTo("ES indices exists request on indices 'index-1,index-2'");
}
Also used : GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) Test(org.junit.Test)

Example 3 with GetIndexRequest

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

the class MigrationEsClientImpl method addMappingToExistingIndex.

@Override
public void addMappingToExistingIndex(String index, String type, String mappingName, String mappingType, Map<String, String> options) {
    String[] indices = client.getIndex(new GetIndexRequest(index)).getIndices();
    if (indices != null && indices.length == 1) {
        Loggers.get(getClass()).info("Add mapping [{}] to Elasticsearch index [{}]", mappingName, index);
        String mappingOptions = Stream.concat(Stream.of(Maps.immutableEntry("type", mappingType)), options.entrySet().stream()).map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(","));
        client.putMapping(new PutMappingRequest(index).type(type).source(mappingName, mappingOptions));
        updatedIndices.add(index);
    } else {
        throw new IllegalStateException("Expected only one index to be found, actual [" + String.join(",", indices) + "]");
    }
}
Also used : Arrays(java.util.Arrays) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) Set(java.util.Set) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) Loggers(org.sonar.api.utils.log.Loggers) Stream(java.util.stream.Stream) PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) Map(java.util.Map) MigrationEsClient(org.sonar.server.platform.db.migration.es.MigrationEsClient) MoreCollectors(org.sonar.core.util.stream.MoreCollectors) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) PutMappingRequest(org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest)

Example 4 with GetIndexRequest

use of org.graylog.shaded.elasticsearch7.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 5 with GetIndexRequest

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexRequest in project gora by apache.

the class ElasticsearchStore method createSchema.

@Override
public void createSchema() throws GoraException {
    CreateIndexRequest request = new CreateIndexRequest(elasticsearchMapping.getIndexName());
    Map<String, Object> properties = new HashMap<>();
    for (Map.Entry<String, Field> entry : elasticsearchMapping.getFields().entrySet()) {
        Map<String, Object> fieldType = new HashMap<>();
        fieldType.put("type", entry.getValue().getDataType().getType().name().toLowerCase(Locale.ROOT));
        if (entry.getValue().getDataType().getType() == Field.DataType.SCALED_FLOAT) {
            fieldType.put("scaling_factor", entry.getValue().getDataType().getScalingFactor());
        }
        properties.put(entry.getKey(), fieldType);
    }
    // Special field for range query
    properties.put("gora_id", new HashMap<String, Object>() {

        {
            put("type", "keyword");
        }
    });
    Map<String, Object> mapping = new HashMap<>();
    mapping.put("properties", properties);
    request.mapping(mapping);
    try {
        if (!client.indices().exists(new GetIndexRequest(elasticsearchMapping.getIndexName()), RequestOptions.DEFAULT)) {
            client.indices().create(request, RequestOptions.DEFAULT);
        }
    } catch (IOException ex) {
        throw new GoraException(ex);
    }
}
Also used : Field(org.apache.gora.elasticsearch.mapping.Field) GoraException(org.apache.gora.util.GoraException) HashMap(java.util.HashMap) GetIndexRequest(org.elasticsearch.client.indices.GetIndexRequest) IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) Map(java.util.Map) HashMap(java.util.HashMap)

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