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); } }
.setSettings(settings) .execute() .actionGet();
.setSettings(settings) .get(); if (!indexResponse.isAcknowledged()) {
CreateIndexResponse indexResponse = SHARED_NODE.client().admin().indices() .prepareCreate(index.getName()) .setSettings(settings) .get(); if (!indexResponse.isAcknowledged()) {
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); } }
String analyser = "{...}"; CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate("indexName"); createIndexRequestBuilder.setSettings(analyser); createIndexRequestBuilder.execute().actionGet();
public CreateIndexResponse createIndex(final String index, final Settings settings) { return createIndex(index, builder -> builder.setSettings(settings != null ? settings : Settings.Builder.EMPTY_SETTINGS)); }
@Override public void createIndex(String indexName, String jsonSettings) { client.admin().indices().prepareCreate(indexName).setSettings(jsonSettings, XContentType.JSON).get(); }
@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(); }
private void createIndexIfNeeded(Client client, IndexName indexName, XContentBuilder settings) { try { client.admin() .indices() .prepareCreate(indexName.getValue()) .setSettings(settings) .execute() .actionGet(); } catch (IndexAlreadyExistsException exception) { LOGGER.info("Index [{}] already exist", indexName); } }
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); } }
/** * creates an index with the given setting */ public final void createIndex(String name, Settings indexSettings) { assertAcked(prepareCreate(name).setSettings(indexSettings)); }
@Override public void createIndex(String indexName, ObjectNode indexSettings) throws ClientException { checkClient(); esClientProvider.getClient().admin() .indices() .prepareCreate(indexName) .setSettings(indexSettings.toString(), XContentType.JSON) .execute() .actionGet(getQueryTimeout()); }
@Override public void createIndex(String indexName, ObjectNode indexSettings) throws ClientException { checkClient(); esClientProvider.getClient().admin() .indices() .prepareCreate(indexName) .setSettings(indexSettings.toString(), XContentType.JSON) .execute() .actionGet(getQueryTimeout()); }
/** * 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); }
@Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { LOG.debug("Creating the index {} settings", indexName); String source = IOUtils.toString(indexSettings.getInputStream(), "UTF-8"); CreateIndexRequestBuilder createIndexReq = esClient.admin().indices().prepareCreate(indexName); createIndexReq.setSettings(source, contentType); CreateIndexResponse response = createIndexReq.execute().actionGet(); if (!response.isAcknowledged()) { throw new RuntimeException("The index settings has not been acknowledged"); } esClient.admin().indices().refresh(new RefreshRequest(indexName)).actionGet(); LOG.info("Index {} settings created", indexName); return RepeatStatus.FINISHED; }
/** * 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); }