org.elasticsearch.action.admin.indices.create.CreateIndexRequest Java Examples

The following examples show how to use org.elasticsearch.action.admin.indices.create.CreateIndexRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ElasticsearchRestClientInstrumentationIT.java    From apm-agent-java with Apache License 2.0 8 votes vote down vote up
@BeforeClass
public static void startElasticsearchContainerAndClient() throws IOException {
    container = new ElasticsearchContainer(ELASTICSEARCH_CONTAINER_VERSION);
    container.start();

    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER_NAME, PASSWORD));

    RestClientBuilder builder = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
        .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));

    client = new RestHighLevelClient(builder);

    client.indices().create(new CreateIndexRequest(INDEX), RequestOptions.DEFAULT);
    reporter.reset();
}
 
Example #2
Source File: SetupDao.java    From usergrid with Apache License 2.0 7 votes vote down vote up
public void setup() throws IOException, NoSuchFieldException, IllegalAccessException {
    String key;
    CreateIndexResponse ciResp;

    Reflections reflections = new Reflections("org.apache.usergrid.chop.webapp.dao");
    Set<Class<? extends Dao>> daoClasses = reflections.getSubTypesOf(Dao.class);

    IndicesAdminClient client = elasticSearchClient.getClient().admin().indices();

    for (Class<? extends Dao> daoClass : daoClasses) {

        key = daoClass.getDeclaredField("DAO_INDEX_KEY").get(null).toString();

        if (!client.exists(new IndicesExistsRequest(key)).actionGet().isExists()) {
            ciResp = client.create(new CreateIndexRequest(key)).actionGet();
            if (ciResp.isAcknowledged()) {
                LOG.debug("Index for key {} didn't exist, now created", key);
            } else {
                LOG.debug("Could not create index for key: {}", key);
            }
        } else {
            LOG.debug("Key {} already exists", key);
        }
    }
}
 
Example #3
Source File: CustomFieldMaskedComplexMappingTest.java    From deprecated-security-advanced-modules with Apache License 2.0 7 votes vote down vote up
@Override
protected void populateData(TransportClient tc) {

    try {
        tc.admin().indices().create(new CreateIndexRequest("logs").mapping("_doc", FileHelper.loadFile("dlsfls/masked_field_mapping.json"), XContentType.JSON)).actionGet();


        byte[] data = FileHelper.loadFile("dlsfls/logs_bulk_data.json").getBytes(StandardCharsets.UTF_8);
        BulkRequest br = new BulkRequest().add(data, 0, data.length, XContentType.JSON).setRefreshPolicy(RefreshPolicy.IMMEDIATE);
        if(tc.bulk(br).actionGet().hasFailures()) {
            Assert.fail("bulk import failed");
        }
        Thread.sleep(1000);

    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.toString());
    }

}
 
Example #4
Source File: TwitterUserstreamElasticsearchIT.java    From streams with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public void prepareTest() throws Exception {

  testConfiguration = new StreamsConfigurator<>(TwitterUserstreamElasticsearchConfiguration.class).detectCustomConfiguration();

  testClient = ElasticsearchClientManager.getInstance(testConfiguration.getElasticsearch()).client();

  ClusterHealthRequest clusterHealthRequest = Requests.clusterHealthRequest();
  ClusterHealthResponse clusterHealthResponse = testClient.admin().cluster().health(clusterHealthRequest).actionGet();
  assertNotEquals(clusterHealthResponse.getStatus(), ClusterHealthStatus.RED);

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getElasticsearch().getIndex());
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();

  if(indicesExistsResponse.isExists()) {
    DeleteIndexRequest deleteIndexRequest = Requests.deleteIndexRequest(testConfiguration.getElasticsearch().getIndex());
    DeleteIndexResponse deleteIndexResponse = testClient.admin().indices().delete(deleteIndexRequest).actionGet();
    assertTrue(deleteIndexResponse.isAcknowledged());
  };

  CreateIndexRequest createIndexRequest = Requests.createIndexRequest(testConfiguration.getElasticsearch().getIndex());
  CreateIndexResponse createIndexResponse = testClient.admin().indices().create(createIndexRequest).actionGet();
  assertTrue(createIndexResponse.isAcknowledged());

}
 
Example #5
Source File: ElasticsearchRestClientInstrumentationIT.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
@BeforeClass
public static void startElasticsearchContainerAndClient() throws IOException {
    // Start the container
    container = new ElasticsearchContainer(ELASTICSEARCH_CONTAINER_VERSION);
    container.start();

    // Create the client
    CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(USER_NAME, PASSWORD));

    RestClientBuilder builder = RestClient.builder(HttpHost.create(container.getHttpHostAddress()))
        .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    client = new RestHighLevelClient(builder);

    client.indices().create(new CreateIndexRequest(INDEX), RequestOptions.DEFAULT);
    reporter.reset();
}
 
Example #6
Source File: ElasticSearchSink.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private void createIndexIfNeeded() throws IOException {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(elasticSearchConfig.getIndexName());
    boolean exists = getClient().indices().exists(request);

    if (!exists) {
        CreateIndexRequest cireq = new CreateIndexRequest(elasticSearchConfig.getIndexName());

        cireq.settings(Settings.builder()
           .put("index.number_of_shards", elasticSearchConfig.getIndexNumberOfShards())
           .put("index.number_of_replicas", elasticSearchConfig.getIndexNumberOfReplicas()));

        CreateIndexResponse ciresp = getClient().indices().create(cireq);
        if (!ciresp.isAcknowledged() || !ciresp.isShardsAcknowledged()) {
            throw new RuntimeException("Unable to create index.");
        }
    }
}
 
Example #7
Source File: IndexCreatorIfNotPresent.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
private CreateIndexRequest createIndexRequest() {
    val docObject = new JsonObject();
    docObject.addProperty("dynamic", "false");
    docObject.add("properties", buildMappingsJson());

    val mapObject = new JsonObject();
    mapObject.add(properties.getDocType(), docObject);

    val request = new CreateIndexRequest(properties.getIndexName());
    request.settings(Settings.builder()
            .put("index.number_of_shards", 5)
            .put("index.number_of_replicas", 3)
    );
    request.mapping(properties.getDocType(), mapObject.toString(), XContentType.JSON);
    return request;
}
 
Example #8
Source File: DetectorIndexCreatorIfNotPresent.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
private CreateIndexRequest createIndexRequest() {
    val docObject = new JsonObject();
    docObject.addProperty("dynamic", "false");
    docObject.add("properties", buildMappingsJson());

    val mapObject = new JsonObject();
    mapObject.add(properties.getDetectorDocType(), docObject);

    val request = new CreateIndexRequest(properties.getDetectorIndexName());
    request.settings(Settings.builder()
            .put("index.number_of_shards", 5)
            .put("index.number_of_replicas", 3)
    );
    request.mapping(properties.getDetectorDocType(), mapObject.toString(), XContentType.JSON);
    return request;
}
 
Example #9
Source File: ElasticsearchConfiguration.java    From datashare with GNU Affero General Public License v3.0 6 votes vote down vote up
public static boolean createIndex(RestHighLevelClient client, String indexName, String indexType) {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(indexName);
    try {
        if (!client.indices().exists(request)) {
            LOGGER.info("index {} does not exist, creating one", indexName);
            CreateIndexRequest createReq = new CreateIndexRequest(indexName);
            createReq.settings(getResourceContent(SETTINGS_RESOURCE_NAME), JSON);
            createReq.mapping(indexType, getResourceContent(MAPPING_RESOURCE_NAME), JSON);
            client.indices().create(createReq);
            return true;
        }
    } catch (IOException e) {
        throw new ConfigurationException(e);
    }
    return false;
}
 
Example #10
Source File: ElasticsearchClientRest.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean createIndex(IndexMetadata indexMetadata, String mapping) {
  CreateIndexRequest request = new CreateIndexRequest(indexMetadata.getName());

  request.settings(Settings.builder()
      .put("index.number_of_shards", properties.getShardsPerIndex())
      .put("index.number_of_replicas", properties.getReplicasPerShard())
  );

  if (properties.isAutoTemplateMapping()) {
    request.mapping(TYPE, mapping, XContentType.JSON);
  }

  try {
    CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
    return createIndexResponse.isAcknowledged();
  } catch (IOException e) {
    log.error("Error creating '{}' index on Elasticsearch.", indexMetadata.getName(), e);
  }

  return false;
}
 
Example #11
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
private void createIndex(String indexName) {
    try {
        elasticSearchClient.admin().indices().prepareGetIndex().addIndices(indexName).execute().actionGet();
    } catch (IndexNotFoundException infe) {
        try {
            CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
            createIndexRequest.settings(Settings.builder()
                    .put("index.number_of_shards", config.getElasticSearchIndexShardCount())
                    .put("index.number_of_replicas", config.getElasticSearchIndexReplicationCount())
            );

            elasticSearchClient.admin()
                    .indices()
                    .create(createIndexRequest)
                    .actionGet();
        } catch (ResourceAlreadyExistsException done) {
            LOGGER.error("Failed to update log index name: {}", indexName, done);
        }
    }
}
 
Example #12
Source File: ElasticsearchPersistWriter.java    From streams with Apache License 2.0 6 votes vote down vote up
/**
 * createIndexIfMissing
 * @param indexName indexName
 */
public void createIndexIfMissing(String indexName) {
  // Synchronize this on a static class level
  if (!this.manager.client()
      .admin()
      .indices()
      .exists(new IndicesExistsRequest(indexName))
      .actionGet()
      .isExists()) {
    // It does not exist... So we are going to need to create the index.
    // we are going to assume that the 'templates' that we have loaded into
    // elasticsearch are sufficient to ensure the index is being created properly.
    CreateIndexResponse response = this.manager.client().admin().indices().create(new CreateIndexRequest(indexName)).actionGet();

    if (response.isAcknowledged()) {
      LOGGER.info("Index Created: {}", indexName);
    } else {
      LOGGER.error("Index {} did not exist. While attempting to create the index from stored ElasticSearch Templates we were unable to get an acknowledgement.", indexName);
      LOGGER.error("Error Message: {}", response.toString());
      throw new RuntimeException("Unable to create index " + indexName);
    }
  }
}
 
Example #13
Source File: IndexServiceImpl.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
@Override
public boolean create(IndexDto indexDto) throws IOException {
    CreateIndexRequest request = new CreateIndexRequest(indexDto.getIndexName());
    request.settings(Settings.builder()
            .put("index.number_of_shards", indexDto.getNumberOfShards())
            .put("index.number_of_replicas", indexDto.getNumberOfReplicas())
    );
    if (StrUtil.isNotEmpty(indexDto.getType()) && StrUtil.isNotEmpty(indexDto.getMappingsSource())) {
        //mappings
        request.mapping(indexDto.getType(), indexDto.getMappingsSource(), XContentType.JSON);
    }
    CreateIndexResponse response = elasticsearchRestTemplate.getClient()
            .indices()
            .create(request, RequestOptions.DEFAULT);
    return response.isAcknowledged();
}
 
Example #14
Source File: UpsertByIdTask.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void createIndexAndExecuteUpsertRequest(final UpsertByIdNode.Item item,
                                                final SettableFuture<TaskResult> futureResult) {
    transportCreateIndexAction.execute(
            new CreateIndexRequest(item.index()).cause("upsert single item"),
            new ActionListener<CreateIndexResponse>() {
        @Override
        public void onResponse(CreateIndexResponse createIndexResponse) {
            executeUpsertRequest(item, futureResult);
        }

        @Override
        public void onFailure(Throwable e) {
            e = ExceptionsHelper.unwrapCause(e);
            if (e instanceof IndexAlreadyExistsException) {
                executeUpsertRequest(item, futureResult);
            } else {
                futureResult.setException(e);
            }

        }
    });
}
 
Example #15
Source File: TestUtils.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
public static void ensureIndex(ElasticsearchConnection connection, final String table) {
    IndicesExistsRequest indicesExistsRequest = new IndicesExistsRequest().indices(table);
    IndicesExistsResponse indicesExistsResponse = connection.getClient()
            .admin()
            .indices()
            .exists(indicesExistsRequest)
            .actionGet();

    if(!indicesExistsResponse.isExists()) {
        Settings indexSettings = Settings.builder()
                .put("number_of_replicas", 0)
                .build();
        CreateIndexRequest createRequest = new CreateIndexRequest(table).settings(indexSettings);
        connection.getClient()
                .admin()
                .indices()
                .create(createRequest)
                .actionGet();
    }
}
 
Example #16
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
private void addIndex(String indexName) {
    try {
        elasticSearchClient.admin()
                .indices()
                .prepareGetIndex()
                .addIndices(indexName)
                .execute()
                .actionGet();
    } catch (IndexNotFoundException infe) {
        try {

            CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
            createIndexRequest.settings(Settings.builder()
                    .put("index.number_of_shards", config.getElasticSearchIndexShardCount())
                    .put("index.number_of_replicas", config.getElasticSearchIndexReplicationCount())
            );

            elasticSearchClient.admin()
                    .indices()
                    .create(createIndexRequest)
                    .actionGet();
        } catch (ResourceAlreadyExistsException done) {
            // no-op
        }
    }
}
 
Example #17
Source File: FlsFieldsWcTest.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
protected void populateData(TransportClient tc) {

        tc.admin().indices().create(new CreateIndexRequest("deals")
        .mapping("deals", "timestamp","type=date","@timestamp","type=date")).actionGet();

        try {
            String doc = FileHelper.loadFile("dlsfls/doc1.json");

            for (int i = 0; i < 10; i++) {
                final String moddoc = doc.replace("<name>", "cust" + i).replace("<employees>", "" + i).replace("<date>", "1970-01-02");
                tc.index(new IndexRequest("deals").type("deals").id("0" + i).setRefreshPolicy(RefreshPolicy.IMMEDIATE).source(moddoc, XContentType.JSON)).actionGet();
            }

        } catch (IOException e) {
            Assert.fail(e.toString());
        }

    }
 
Example #18
Source File: FlsFieldsTest.java    From deprecated-security-advanced-modules with Apache License 2.0 6 votes vote down vote up
protected void populateData(TransportClient tc) {

        tc.admin().indices().create(new CreateIndexRequest("deals")
        .mapping("deals", "timestamp","type=date","@timestamp","type=date")).actionGet();

        try {
            String doc = FileHelper.loadFile("dlsfls/doc1.json");

            for (int i = 0; i < 10; i++) {
                final String moddoc = doc.replace("<name>", "cust" + i).replace("<employees>", "" + i).replace("<date>", "1970-01-02");
                tc.index(new IndexRequest("deals").type("deals").id("0" + i).setRefreshPolicy(RefreshPolicy.IMMEDIATE).source(moddoc, XContentType.JSON)).actionGet();
            }

        } catch (IOException e) {
            Assert.fail(e.toString());
        }

    }
 
Example #19
Source File: TransportDeleteAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected void doExecute(final Task task, final DeleteRequest request, final ActionListener<DeleteResponse> listener) {
    ClusterState state = clusterService.state();
    if (autoCreateIndex.shouldAutoCreate(request.index(), state)) {
        createIndexAction.execute(task, new CreateIndexRequest(request).index(request.index()).cause("auto(delete api)")
            .masterNodeTimeout(request.timeout()), new ActionListener<CreateIndexResponse>() {
            @Override
            public void onResponse(CreateIndexResponse result) {
                innerExecute(task, request, listener);
            }

            @Override
            public void onFailure(Throwable e) {
                if (ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException) {
                    // we have the index, do it
                    innerExecute(task, request, listener);
                } else {
                    listener.onFailure(e);
                }
            }
        });
    } else {
        innerExecute(task, request, listener);
    }
}
 
Example #20
Source File: BlobAdminClient.java    From crate with Apache License 2.0 5 votes vote down vote up
public CompletableFuture<Long> createBlobTable(String tableName, Settings indexSettings) {
    Settings.Builder builder = Settings.builder();
    builder.put(indexSettings);
    builder.put(SETTING_INDEX_BLOBS_ENABLED.getKey(), true);

    FutureActionListener<CreateIndexResponse, Long> listener = new FutureActionListener<>(r -> 1L);
    CreateIndexRequest createIndexRequest = new CreateIndexRequest(fullIndexName(tableName), builder.build());
    createIndexAction.execute(createIndexRequest, listener);
    return listener;
}
 
Example #21
Source File: InitializerCommand.java    From foxtrot with Apache License 2.0 5 votes vote down vote up
private void createMetaIndex(final ElasticsearchConnection connection, final String indexName, int replicaCount) {
    try {
        logger.info("'{}' creation started", indexName);
        Settings settings = Settings.builder()
                .put("number_of_shards", 1)
                .put("number_of_replicas", replicaCount)
                .build();
        CreateIndexRequest createIndexRequest = new CreateIndexRequest().index(indexName)
                .settings(settings);
        CreateIndexResponse response = connection.getClient()
                .admin()
                .indices()
                .create(createIndexRequest)
                .actionGet();
        logger.info("'{}' creation acknowledged: {}", indexName, response.isAcknowledged());
        if(!response.isAcknowledged()) {
            logger.error("Index {} could not be created.", indexName);
        }
    } catch (Exception e) {
        if(null != e.getCause()) {
            logger.error("Index {} could not be created: {}", indexName, e.getCause()
                    .getLocalizedMessage());
        } else {
            logger.error("Index {} could not be created: {}", indexName, e.getLocalizedMessage());
        }
    }
}
 
Example #22
Source File: ElasticSearchUtils.java    From bdt with Apache License 2.0 5 votes vote down vote up
public boolean createSingleIndex(String indexName, Settings.Builder settings) {
    CreateIndexRequest indexRequest = new CreateIndexRequest(indexName).settings(settings);
    try {
        this.client.indices().create(indexRequest, RequestOptions.DEFAULT);
    } catch (IOException e) {
        throw new ElasticsearchException("Error creating index: " + indexName);

    }
    return indexExists(indexName);
}
 
Example #23
Source File: BaseElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * creates a new index, does not check if the exist exists
 */
protected void createIndex() {
    try {
        CreateIndexResponse createResponse = client.admin().indices().create(new CreateIndexRequest(indexName)
                .settings(indexSettingsMerged).mapping(indexedDocumentType, mappingMerged)).actionGet();
        if (!createResponse.isAcknowledged()) {
            getLog().error("Index wasn't created for index builder [" + getName() + "], can't rebuild");
        }
    } catch (IndexAlreadyExistsException e) {
        getLog().warn("Index already created for index builder [" + getName() + "]");
    }
}
 
Example #24
Source File: ElasticSearchClient.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public boolean createIndex(String indexName) throws IOException {
    indexName = formatIndexName(indexName);

    CreateIndexRequest request = new CreateIndexRequest(indexName);
    CreateIndexResponse response = client.indices().create(request);
    log.debug("create {} index finished, isAcknowledged: {}", indexName, response.isAcknowledged());
    return response.isAcknowledged();
}
 
Example #25
Source File: ElasticSearcher.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void tryCreateIndex() {
    if (checkIndexExist()) {
        return;
    }

    CreateIndexRequest request = new CreateIndexRequest(index);
    try {
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        if (LogKit.isDebugEnabled()) {
            LogKit.debug(response.toString());
        }
    } catch (Exception e) {
        LOG.error(e.toString(), e);
    }
}
 
Example #26
Source File: ElasticSearcher.java    From jpress with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void tryCreateIndex() {
    if (checkIndexExist()) {
        return;
    }

    CreateIndexRequest request = new CreateIndexRequest(index);
    try {
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        if (LogKit.isDebugEnabled()) {
            LogKit.debug(response.toString());
        }
    } catch (Exception e) {
        LOG.error(e.toString(), e);
    }
}
 
Example #27
Source File: RandomCreateIndexGenerator.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a random {@link CreateIndexRequest}. Randomizes the index name, the aliases,
 * mappings and settings associated with the index.
 */
public static CreateIndexRequest randomCreateIndexRequest() throws IOException {
    String index = randomAlphaOfLength(5);
    CreateIndexRequest request = new CreateIndexRequest(index);
    randomAliases(request);
    if (randomBoolean()) {
        String type = randomAlphaOfLength(5);
        request.mapping(type, randomMapping(type));
    }
    if (randomBoolean()) {
        request.settings(randomIndexSettings());
    }
    return request;
}
 
Example #28
Source File: TransportIndexAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void doExecute(final Task task, final IndexRequest request, final ActionListener<IndexResponse> listener) {
    // if we don't have a master, we don't have metadata, that's fine, let it find a master using create index API
    ClusterState state = clusterService.state();
    if (autoCreateIndex.shouldAutoCreate(request.index(), state)) {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(request);
        createIndexRequest.index(request.index());
        createIndexRequest.mapping(request.type());
        createIndexRequest.cause("auto(index api)");
        createIndexRequest.masterNodeTimeout(request.timeout());
        createIndexAction.execute(task, createIndexRequest, new ActionListener<CreateIndexResponse>() {
            @Override
            public void onResponse(CreateIndexResponse result) {
                innerExecute(task, request, listener);
            }

            @Override
            public void onFailure(Throwable e) {
                if (ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException) {
                    // we have the index, do it
                    try {
                        innerExecute(task, request, listener);
                    } catch (Throwable e1) {
                        listener.onFailure(e1);
                    }
                } else {
                    listener.onFailure(e);
                }
            }
        });
    } else {
        innerExecute(task, request, listener);
    }
}
 
Example #29
Source File: TransportUpdateAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void doExecute(final UpdateRequest request, final ActionListener<UpdateResponse> listener) {
    // if we don't have a master, we don't have metadata, that's fine, let it find a master using create index API
    if (autoCreateIndex.shouldAutoCreate(request.index(), clusterService.state())) {
        createIndexAction.execute(new CreateIndexRequest(request).index(request.index()).cause("auto(update api)").masterNodeTimeout(request.timeout()), new ActionListener<CreateIndexResponse>() {
            @Override
            public void onResponse(CreateIndexResponse result) {
                innerExecute(request, listener);
            }

            @Override
            public void onFailure(Throwable e) {
                if (ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException) {
                    // we have the index, do it
                    try {
                        innerExecute(request, listener);
                    } catch (Throwable e1) {
                        listener.onFailure(e1);
                    }
                } else {
                    listener.onFailure(e);
                }
            }
        });
    } else {
        innerExecute(request, listener);
    }
}
 
Example #30
Source File: RestCreateIndexAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"unchecked"})
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    CreateIndexRequest createIndexRequest = new CreateIndexRequest(request.param("index"));
    if (request.hasContent()) {
        createIndexRequest.source(request.content());
    }
    createIndexRequest.updateAllTypes(request.paramAsBoolean("update_all_types", false));
    createIndexRequest.timeout(request.paramAsTime("timeout", createIndexRequest.timeout()));
    createIndexRequest.masterNodeTimeout(request.paramAsTime("master_timeout", createIndexRequest.masterNodeTimeout()));
    client.admin().indices().create(createIndexRequest, new AcknowledgedRestListener<CreateIndexResponse>(channel));
}