Search in sources :

Example 1 with CreateIndexRequest

use of org.elasticsearch.client.indices.CreateIndexRequest in project drill by apache.

the class ElasticInfoSchemaTest method prepareData.

private static void prepareData() throws IOException {
    restHighLevelClient = new RestHighLevelClient(RestClient.builder(HttpHost.create(HOST)));
    String indexName = "t1";
    indexNames.add(indexName);
    CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
    restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    builder.field("string_field", "a");
    builder.field("int_field", 123);
    builder.endObject();
    IndexRequest indexRequest = new IndexRequest(indexName).source(builder);
    restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
    restHighLevelClient.indices().refresh(new RefreshRequest(indexName), RequestOptions.DEFAULT);
    indexName = "t2";
    indexNames.add(indexName);
    createIndexRequest = new CreateIndexRequest(indexName);
    restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);
    builder = XContentFactory.jsonBuilder();
    builder.startObject();
    builder.field("another_int_field", 321);
    builder.field("another_string_field", "b");
    builder.endObject();
    indexRequest = new IndexRequest(indexName).source(builder);
    restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
    restHighLevelClient.indices().refresh(new RefreshRequest(indexName), RequestOptions.DEFAULT);
}
Also used : RefreshRequest(org.elasticsearch.action.admin.indices.refresh.RefreshRequest) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) IndexRequest(org.elasticsearch.action.index.IndexRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 2 with CreateIndexRequest

use of org.elasticsearch.client.indices.CreateIndexRequest in project graylog2-server by Graylog2.

the class ClientES7 method bulkIndex.

@Override
public void bulkIndex(BulkIndexRequest bulkIndexRequest) {
    final BulkRequest bulkRequest = new BulkRequest();
    bulkRequest.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
    bulkIndexRequest.requests().forEach((indexName, documents) -> documents.forEach(doc -> bulkRequest.add(createIndexRequest(indexName, doc))));
    client.execute((c, requestOptions) -> c.bulk(bulkRequest, requestOptions));
}
Also used : ElasticsearchClient(org.graylog.storage.elasticsearch7.ElasticsearchClient) IndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.index.IndexRequest) Retryer(com.github.rholder.retry.Retryer) RetryerBuilder(com.github.rholder.retry.RetryerBuilder) LoggerFactory(org.slf4j.LoggerFactory) Callable(java.util.concurrent.Callable) WriteRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.support.WriteRequest) CloseIndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.CloseIndexRequest) ClusterHealthStatus(org.graylog.shaded.elasticsearch7.org.elasticsearch.cluster.health.ClusterHealthStatus) PutIndexTemplateRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.PutIndexTemplateRequest) GetIndexTemplatesRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexTemplatesRequest) RefreshRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.refresh.RefreshRequest) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) AliasActions(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest.AliasActions) Response(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Response) WaitStrategies(com.github.rholder.retry.WaitStrategies) GetIndexTemplatesResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexTemplatesResponse) GetIndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.GetIndexRequest) BulkIndexRequest(org.graylog.testing.elasticsearch.BulkIndexRequest) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DeleteIndexTemplateRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) Request(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.Request) Client(org.graylog.testing.elasticsearch.Client) ObjectMapperProvider(org.graylog2.shared.bindings.providers.ObjectMapperProvider) Streams(com.google.common.collect.Streams) ClusterHealthRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) Collectors(java.util.stream.Collectors) DeleteIndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) CreateIndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.CreateIndexRequest) UpdateSettingsRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.settings.put.UpdateSettingsRequest) TimeUnit(java.util.concurrent.TimeUnit) ClusterUpdateSettingsRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.cluster.settings.ClusterUpdateSettingsRequest) Stream(java.util.stream.Stream) StopStrategies(com.github.rholder.retry.StopStrategies) Settings(org.graylog.shaded.elasticsearch7.org.elasticsearch.common.settings.Settings) IndicesAliasesRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.admin.indices.alias.IndicesAliasesRequest) BulkRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest) IndexTemplateMetadata(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.IndexTemplateMetadata) Collections(java.util.Collections) BulkRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.bulk.BulkRequest)

Example 3 with CreateIndexRequest

use of org.elasticsearch.client.indices.CreateIndexRequest in project graylog2-server by Graylog2.

the class IndicesAdapterES7 method create.

@Override
public void create(String index, IndexSettings indexSettings) {
    final Map<String, Object> settings = new HashMap<>();
    settings.put("number_of_shards", indexSettings.shards());
    settings.put("number_of_replicas", indexSettings.replicas());
    final CreateIndexRequest request = new CreateIndexRequest(index).settings(settings);
    client.execute((c, requestOptions) -> c.indices().create(request, requestOptions), "Unable to create index " + index);
}
Also used : HashMap(java.util.HashMap) CreateIndexRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.CreateIndexRequest)

Example 4 with CreateIndexRequest

use of org.elasticsearch.client.indices.CreateIndexRequest 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)

Example 5 with CreateIndexRequest

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

the class EsRequestDetailsTest method should_format_CreateIndexRequest.

@Test
public void should_format_CreateIndexRequest() {
    CreateIndexRequest request = new CreateIndexRequest("index-1");
    assertThat(EsRequestDetails.computeDetailsAsString(request)).isEqualTo("ES create index 'index-1'");
}
Also used : CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) Test(org.junit.Test)

Aggregations

CreateIndexRequest (org.elasticsearch.client.indices.CreateIndexRequest)17 IOException (java.io.IOException)6 DeleteIndexRequest (org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest)6 IndexRequest (org.elasticsearch.action.index.IndexRequest)5 GetIndexRequest (org.elasticsearch.client.indices.GetIndexRequest)5 RefreshRequest (org.elasticsearch.action.admin.indices.refresh.RefreshRequest)4 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)4 CreateIndexResponse (org.elasticsearch.client.indices.CreateIndexResponse)4 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)4 CreateIndexRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.client.indices.CreateIndexRequest)4 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AcknowledgedResponse (org.elasticsearch.action.support.master.AcknowledgedResponse)2 DocumentMapping (com.b2international.index.mapping.DocumentMapping)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Retryer (com.github.rholder.retry.Retryer)1 RetryerBuilder (com.github.rholder.retry.RetryerBuilder)1 StopStrategies (com.github.rholder.retry.StopStrategies)1