private void updateLogIndexName() { this.logIndexName = this.logIndexPrefix + "_" + SIMPLE_DATE_FORMAT.format(new Date()); try { elasticSearchClient.admin() .indices() .prepareGetIndex() .addIndices(logIndexName) .execute() .actionGet(); } catch (IndexNotFoundException infe) { try { elasticSearchClient.admin() .indices() .prepareCreate(logIndexName) .execute() .actionGet(); } catch (ResourceAlreadyExistsException ilee) { // no-op } catch (Exception e) { logger.error("Failed to update log index name: {}", logIndexName, e); } } }
private void addIndex(String indexName) { try { elasticSearchClient.admin() .indices() .prepareGetIndex() .addIndices(indexName) .execute() .actionGet(); } catch (IndexNotFoundException infe) { try { elasticSearchClient.admin() .indices() .prepareCreate(indexName) .execute() .actionGet(); } catch (ResourceAlreadyExistsException done) { // no-op } } }
public void createIndexIfNotExists(String indexName, final int shards, final int replicas) { // create an index if not existent if (!this.elasticsearchClient.admin().indices().prepareExists(indexName).execute().actionGet().isExists()) { Settings.Builder settings = Settings.builder() .put("number_of_shards", shards) .put("number_of_replicas", replicas); this.elasticsearchClient.admin().indices().prepareCreate(indexName) .setSettings(settings) .setUpdateAllTypes(true) .execute().actionGet(); } else { //LOGGER.debug("Index with name {} already exists", indexName); } }
/** * 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 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"); }
final CreateIndexResponse cir = admin.indices().prepareCreate(indexName) .setSettings(settings) .execute()
settings.put(index.getSettings()); CreateIndexResponse indexResponse = SHARED_NODE.client().admin().indices() .prepareCreate(index.getName()) .setSettings(settings) .get();
esClient.admin().indices().prepareCreate(definition.getIndexName()).get();
public void recreateIndex() throws IOException { deleteIndex(); final Client client = this.getClient(); final InputStream mappings = Thread.currentThread().getContextClassLoader() .getResourceAsStream("mappings.json"); final InputStream index_settings = Thread.currentThread().getContextClassLoader() .getResourceAsStream("index_settings.json"); String mappingsString = IOUtils.toString(mappings); JSONObject mappingsJSON = new JSONObject(mappingsString); // add all langs to the mapping mappingsJSON = addLangsToMapping(mappingsJSON); client.admin().indices().prepareCreate("photon").setSettings(IOUtils.toString(index_settings)).execute() .actionGet(); client.admin().indices().preparePutMapping("photon").setType("place").setSource(mappingsJSON.toString()) .execute().actionGet(); log.info("mapping created: " + mappingsJSON.toString()); }
/** * create a new index. This method must be called to ensure that an elasticsearch index is available and can be used. * @param indexName * @param shards * @param replicas * @throws NoNodeAvailableException in case that no elasticsearch server can be contacted. */ public void createIndexIfNotExists(String indexName, final int shards, final int replicas) throws NoNodeAvailableException { // create an index if not existent if (!this.elasticsearchClient.admin().indices().prepareExists(indexName).execute().actionGet().isExists()) { Settings.Builder settings = Settings.builder() .put("number_of_shards", shards) .put("number_of_replicas", replicas); this.elasticsearchClient.admin().indices().prepareCreate(indexName) .setSettings(settings) .setUpdateAllTypes(true) .execute().actionGet(); } else { //LOGGER.debug("Index with name {} already exists", indexName); } }
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"); } }
.prepareCreate(indexName).execute().actionGet(); try { Thread.sleep(200);
client.admin().indices().prepareCreate(m.index) .addMapping(m.docType, m.mapping).get();
private void createIndex(String indexName, Settings indicesSettings) { if (indicesSettings != null) { client.admin().indices().prepareCreate(indexName).setSettings(indicesSettings).execute().actionGet(); } else { client.admin().indices().prepareCreate(indexName).execute().actionGet(); } }
private void createIndex(BulkRequestBuilder bulkRequest, String indexName, Settings indicesSettings) { try { if (indicesSettings != null) { client.admin().indices().prepareCreate(indexName).setSettings(indicesSettings).execute().actionGet(); } else { client.admin().indices().prepareCreate(indexName).execute().actionGet(); } } catch (ElasticSearchException e) { StormElasticSearchUtils.handleElasticSearchException(getClass(), e); } }
/** * Create a new index on the singleton node with the provided index settings. */ protected IndexService createIndex(String index, Settings settings, String type, XContentBuilder mappings) { CreateIndexRequestBuilder createIndexRequestBuilder = client().admin().indices().prepareCreate(index).setSettings(settings); if (type != null && mappings != null) { createIndexRequestBuilder.addMapping(type, mappings); } return createIndex(index, createIndexRequestBuilder); }
/** * Create a new index on the singleton node with the provided index settings. */ protected IndexService createIndex(String index, Settings settings, String type, Object... mappings) { CreateIndexRequestBuilder createIndexRequestBuilder = client().admin().indices().prepareCreate(index).setSettings(settings); if (type != null && mappings != null) { createIndexRequestBuilder.addMapping(type, mappings); } return createIndex(index, createIndexRequestBuilder); }
private void initNativeClient() { esClient = NodeBuilder.nodeBuilder().client(true).node().client(); boolean indexExists = esClient.admin().indices().prepareExists(indexName).execute().actionGet().isExists(); if (indexExists) { esClient.admin().indices().prepareDelete(indexName).execute().actionGet(); } esClient.admin().indices().prepareCreate(indexName).execute().actionGet(); }
private void createIndexIfNeeded(IndexFactory indexFactory) { IndicesExistsResponse response = client.admin().indices().exists(new IndicesExistsRequest(indexFactory.createIndex())).actionGet(); if (response.isExists()) { return; } client.admin().indices().prepareCreate(indexFactory.createIndex()).execute().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(); }