@Override public boolean createIndex(String indexName) { Assert.notNull(indexName, "No index defined for Query"); try { return client.indices().create(Requests.createIndexRequest(indexName)).isAcknowledged(); } catch (Exception e) { throw new ElasticsearchException("Failed to create index " + indexName, e); } }
/** * If ES already contains this instance's target index, then do nothing. * Otherwise, create the index, then wait {@link #CREATE_SLEEP}. * <p> * The {@code client} field must point to a live, connected client. * The {@code indexName} field must be non-null and point to the name * of the index to check for existence or create. * * @param config the config for this ElasticSearchIndex * @throws java.lang.IllegalArgumentException if the index could not be created */ private void checkForOrCreateIndex(Configuration config) { Preconditions.checkState(null != client); //Create index if it does not already exist IndicesExistsResponse response = client.admin().indices().exists(new IndicesExistsRequest(indexName)).actionGet(); if (!response.isExists()) { ImmutableSettings.Builder settings = ImmutableSettings.settingsBuilder(); ElasticSearchSetup.applySettingsFromTitanConf(settings, config, ES_CREATE_EXTRAS_NS); CreateIndexResponse create = client.admin().indices().prepareCreate(indexName) .setSettings(settings.build()).execute().actionGet(); try { final long sleep = config.get(CREATE_SLEEP); log.debug("Sleeping {} ms after {} index creation returned from actionGet()", sleep, indexName); Thread.sleep(sleep); } catch (InterruptedException e) { throw new TitanException("Interrupted while waiting for index to settle in", e); } if (!create.isAcknowledged()) throw new IllegalArgumentException("Could not create index: " + indexName); } }
@Override public boolean createIndex(String indexName, Object settings) { CreateIndexRequest request = new CreateIndexRequest(indexName); if (settings instanceof String) { request.settings(String.valueOf(settings), Requests.INDEX_CONTENT_TYPE); } else if (settings instanceof Map) { request.settings((Map) settings); } else if (settings instanceof XContentBuilder) { request.settings((XContentBuilder) settings); } try { return client.indices().create(request).isAcknowledged(); } catch (IOException e) { throw new ElasticsearchException("Error for creating index: " + request.toString(), e); } }
.actionGet().isAcknowledged();
@Override public void recreateIndex(String indexName) throws IOException { DeleteIndexRequestBuilder dirBuilder = transportClient.admin().indices().prepareDelete(indexName); try { DeleteIndexResponse diResponse = dirBuilder.execute().actionGet(); } catch (IndexNotFoundException ie) { System.out.println("Index not found... that's ok"); } CreateIndexRequestBuilder cirBuilder = transportClient.admin().indices().prepareCreate(indexName); CreateIndexResponse ciResponse = cirBuilder.execute().actionGet(); Assert.assertTrue(ciResponse.isAcknowledged(), "Create index succeeeded"); }
logger.info("Created new Index Name [{}] ACK=[{}]", indexName, cir.isAcknowledged()); } catch (IndexAlreadyExistsException e) { logger.info("Index Name [{}] already exists", indexName);
.setSettings(settings) .get(); if (!indexResponse.isAcknowledged()) { throw new IllegalStateException("Failed to create index " + index.getName());
.setSettings(settings) .get(); if (!indexResponse.isAcknowledged()) { throw new IllegalStateException("Failed to create index " + index.getName());
public boolean createIndex(String type, XContentBuilder mappingBuilder) { try { if (this.instance() == null) return false; CreateIndexRequest request = new CreateIndexRequest(name); request.settings(Settings.builder() .put("index.number_of_shards", 1) .put("index.number_of_shards", 5)); if (mappingBuilder != null) request.mapping(type, mappingBuilder); CreateIndexResponse response = this.client.indices().create(request, RequestOptions.DEFAULT); return response.isAcknowledged(); } catch (IOException e) { log.error(e.getMessage()); return false; } }
Assert.assertTrue(cir.isAcknowledged());
Assert.assertTrue(cir.isAcknowledged());
public void createIndexWithMapping(String indexName, String mappingType, String mappingSource) throws IOException { CreateIndexResponse cir = client.admin().indices().prepareCreate(indexName) .addMapping(mappingType, mappingSource) .get(); if (!cir.isAcknowledged()) { throw new IOException("Create index was not acknowledged"); } }
"Interrupted while waiting for index to settle in", e); if (!create.isAcknowledged()) { throw new IllegalArgumentException("Could not create index: " + indexName);
/** * Assert that an index creation was fully acknowledged, meaning that both the index creation cluster * state update was successful and that the requisite number of shard copies were started before returning. */ public static void assertAcked(CreateIndexResponse response) { assertThat(response.getClass().getSimpleName() + " failed - not acked", response.isAcknowledged(), equalTo(true)); assertVersionSerializable(response); assertTrue(response.getClass().getSimpleName() + " failed - index creation acked but not all shards were started", response.isShardsAcknowledged()); }
@Override protected XContentBuilder toXContent(CreateIndexRequest request, CreateIndexResponse response, XContentBuilder builder) throws IOException { return builder.startObject() .field(Fields.OK, true) .field(Fields.ACKNOWLEDGED, response.isAcknowledged()) .endObject(); } }
private synchronized void createIndex() { CreateIndexResponse response = client.admin().indices().create(new CreateIndexRequest(index)).actionGet(); if (!response.isAcknowledged()) { throw new RuntimeException("cannot create index " + index); } client.admin().cluster().prepareHealth().setWaitForYellowStatus().execute().actionGet(); }
@Override public boolean createIndex(String indexName) { Assert.notNull(indexName, "No index defined for Query"); return getIndicesAdminClient().create(Requests.createIndexRequest(indexName)).actionGet().isAcknowledged(); }
public CreateIndexResponse createIndex(final String index, final BuilderCallback<CreateIndexRequestBuilder> builder) { final CreateIndexResponse actionGet = builder .apply(client().admin().indices().prepareCreate(index)) .execute().actionGet(); if (!actionGet.isAcknowledged()) { onFailure("Failed to create " + index + ".", actionGet); } return actionGet; }
@Override public boolean createIndex(String indexName, Object settings) { CreateIndexRequestBuilder createIndexRequestBuilder = getIndicesAdminClient().prepareCreate(indexName); if (settings instanceof String) { createIndexRequestBuilder.setSettings(String.valueOf(settings), XContentType.JSON); } else if (settings instanceof Map) { createIndexRequestBuilder.setSettings((Map) settings); } else if (settings instanceof XContentBuilder) { createIndexRequestBuilder.setSettings((XContentBuilder) settings); } return createIndexRequestBuilder.execute().actionGet().isAcknowledged(); }
public static void createIndexELServer( String index, String jsonMappingsFile ) throws Exception { // Obtain data to configure & populate the server. String mappingsContent = getFileAsString(jsonMappingsFile); CreateIndexRequest indexRequest = new CreateIndexRequestBuilder( client, CreateIndexAction.INSTANCE ) .setIndex( index) .setSource( mappingsContent ) .request(); CreateIndexResponse indexResponse = client.admin().indices().create( indexRequest ).actionGet(); if ( !indexResponse.isAcknowledged() ) { throw new RuntimeException( "Error creating index [" + index + "]" ); } }