@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); } }
@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); } }
/** * Create an index * @param index index name * @param ignoreErrors don't fail if the index already exists * @param indexSettings index settings if any * @throws IOException In case of error */ public void createIndex(String index, boolean ignoreErrors, String indexSettings) throws IOException { logger.debug("create index [{}]", index); logger.trace("index settings: [{}]", indexSettings); CreateIndexRequest cir = new CreateIndexRequest(index); if (!isNullOrEmpty(indexSettings)) { cir.source(indexSettings, XContentType.JSON); } try { client.indices().create(cir, RequestOptions.DEFAULT); } catch (ElasticsearchStatusException e) { if (e.getMessage().contains("resource_already_exists_exception") && !ignoreErrors) { throw new RuntimeException("index already exists"); } } }
/** * Create an index * @param index index name * @param ignoreErrors don't fail if the index already exists * @param indexSettings index settings if any * @throws IOException In case of error */ public void createIndex(String index, boolean ignoreErrors, String indexSettings) throws IOException { logger.debug("create index [{}]", index); logger.trace("index settings: [{}]", indexSettings); CreateIndexRequest cir = new CreateIndexRequest(index); if (!isNullOrEmpty(indexSettings)) { cir.source(indexSettings, XContentType.JSON); } try { client.indices().create(cir, RequestOptions.DEFAULT); } catch (ElasticsearchStatusException e) { if (e.getMessage().contains("resource_already_exists_exception") && !ignoreErrors) { throw new RuntimeException("index already exists"); } } }
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; } }
/** * Create an index, using the mappings read from the mappings file. * * @param index the name of the index to create. * @return {@code true} if the index was successfully created. * @throws IOException if there are problems reading the mappings, or * making the index creation request. */ public boolean createIndex(String index) throws IOException { // Build the request CreateIndexRequest request = new CreateIndexRequest(index) .source(readConfig(), XContentType.JSON); CreateIndexResponse response = client.indices().create(request); return response.isAcknowledged(); }
public boolean createIndex(String indexName, String type) throws IOException { CreateIndexRequest request = new CreateIndexRequest("twitter"); request.settings(Settings.builder() .put("index.number_of_shards", 3) .put("index.number_of_replicas", 2) ); request.mapping("tweet", " {\n" + " \"tweet\": {\n" + " \"properties\": {\n" + " \"message\": {\n" + " \"type\": \"text\"\n" + " }\n" + " }\n" + " }\n" + " }", XContentType.JSON); request.alias( new Alias("twitter_alias") ); request.timeout(TimeValue.timeValueMinutes(2)); request.timeout("2m"); request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); request.masterNodeTimeout("1m"); request.waitForActiveShards(2); request.waitForActiveShards(ActiveShardCount.DEFAULT); CreateIndexResponse createIndexResponse = client.indices().create(request); return createIndexResponse.isAcknowledged(); }