@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 deleteIndex(String indexName) { Assert.notNull(indexName, "No index defined for delete operation"); if (indexExists(indexName)) { DeleteIndexRequest request = new DeleteIndexRequest(indexName); try { return client.indices().delete(request).isAcknowledged(); } catch (IOException e) { throw new ElasticsearchException("Error while deleting index request: " + request.toString(), e); } } return false; }
@Override public boolean indexExists(String indexName) { GetIndexRequest request = new GetIndexRequest(); request.indices(indexName); try { return client.indices().exists(request); } catch (IOException e) { throw new ElasticsearchException("Error while for indexExists request: " + request.toString(), e); } }
@Override public void refresh(String indexName) { Assert.notNull(indexName, "No index defined for refresh()"); try { // TODO: Do something with the response. client.indices().refresh(refreshRequest(indexName)); } catch (IOException e) { throw new ElasticsearchException("failed to refresh index: " + indexName, e); } }
/** * Asynchronously force merge one or more indices using the Force Merge API. * See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html"> * Force Merge API on elastic.co</a> * @param forceMergeRequest the request * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param listener the listener to be notified upon request completion * @deprecated use {@link #forcemergeAsync(ForceMergeRequest, RequestOptions, ActionListener)} instead */ @Deprecated public void forceMergeAsync(ForceMergeRequest forceMergeRequest, RequestOptions options, ActionListener<ForceMergeResponse> listener) { forcemergeAsync(forceMergeRequest, options, listener); }
/** * Force merge one or more indices using the Force Merge API. * See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html"> * Force Merge API on elastic.co</a> * @param forceMergeRequest the request * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response * @deprecated use {@link #forcemerge(ForceMergeRequest, RequestOptions)} instead */ @Deprecated public ForceMergeResponse forceMerge(ForceMergeRequest forceMergeRequest, RequestOptions options) throws IOException { return forcemerge(forceMergeRequest, options); }
/** * Refresh an index * @param index index name * @throws IOException In case of error */ public void refresh(String index) throws IOException { logger.debug("refresh index [{}]", index); RefreshRequest request = new RefreshRequest(); if (!isNullOrEmpty(index)) { request.indices(index); } RefreshResponse refresh = client.indices().refresh(request, RequestOptions.DEFAULT); logger.trace("refresh response: {}", refresh); }
/** * Asynchronously force merge one or more indices using the Force Merge API. * See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html"> * Force Merge API on elastic.co</a> * @param forceMergeRequest the request * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @param listener the listener to be notified upon request completion * @deprecated use {@link #forcemergeAsync(ForceMergeRequest, RequestOptions, ActionListener)} instead */ @Deprecated public void forceMergeAsync(ForceMergeRequest forceMergeRequest, RequestOptions options, ActionListener<ForceMergeResponse> listener) { forcemergeAsync(forceMergeRequest, options, listener); }
/** * Force merge one or more indices using the Force Merge API. * See <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-forcemerge.html"> * Force Merge API on elastic.co</a> * @param forceMergeRequest the request * @param options the request options (e.g. headers), use {@link RequestOptions#DEFAULT} if nothing needs to be customized * @return the response * @throws IOException in case there is a problem sending the request or parsing back the response * @deprecated use {@link #forcemerge(ForceMergeRequest, RequestOptions)} instead */ @Deprecated public ForceMergeResponse forceMerge(ForceMergeRequest forceMergeRequest, RequestOptions options) throws IOException { return forcemerge(forceMergeRequest, options); }
@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); } }
/** * Check if an index exists * @param index index name * @return true if the index exists, false otherwise * @throws IOException In case of error */ public boolean isExistingIndex(String index) throws IOException { logger.debug("is existing index [{}]", index); GetIndexRequest gir = new GetIndexRequest(); gir.indices(index); return client.indices().exists(gir, RequestOptions.DEFAULT); }
@Override public void deleteIndex(String index) throws IOException { client.indices().delete(new DeleteIndexRequest(index), RequestOptions.DEFAULT); }
/** * Refresh an index * @param index index name * @throws IOException In case of error */ public void refresh(String index) throws IOException { logger.debug("refresh index [{}]", index); RefreshRequest request = new RefreshRequest(); if (!isNullOrEmpty(index)) { request.indices(index); } RefreshResponse refresh = client.indices().refresh(request, RequestOptions.DEFAULT); logger.trace("refresh response: {}", refresh); }
@Override() public void forcemergeAsync(ForceMergeRequest forceMergeRequest, RequestOptions options, Handler<AsyncResult<ForceMergeResponse>> handler) { Context context = vertx.getOrCreateContext(); delegate.forcemergeAsync(forceMergeRequest, options, new ActionListener<ForceMergeResponse>() { @Override public void onResponse(ForceMergeResponse value) { context.runOnContext(v -> handler.handle(Future.succeededFuture(value))); } @Override public void onFailure(Exception e) { context.runOnContext(v -> handler.handle(Future.failedFuture(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"); } } }
/** * Check if an index exists * @param index index name * @return true if the index exists, false otherwise * @throws IOException In case of error */ public boolean isExistingIndex(String index) throws IOException { logger.debug("is existing index [{}]", index); GetIndexRequest gir = new GetIndexRequest(); gir.indices(index); return client.indices().exists(gir, RequestOptions.DEFAULT); }
@Override public void deleteIndex(String index) throws IOException { client.indices().delete(new DeleteIndexRequest(index), RequestOptions.DEFAULT); }
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; } }
public boolean existIndex() { try { if (this.instance() == null) return false; GetIndexRequest request = new GetIndexRequest(); request.indices(name); request.local(false); request.humanReadable(true); return client.indices().exists(request, RequestOptions.DEFAULT); } catch (IOException e) { log.error(e.getMessage()); return false; } }
public boolean deleteIndex() { try { if (this.instance() == null) return false; DeleteIndexRequest request = new DeleteIndexRequest(name); request.indicesOptions(IndicesOptions.LENIENT_EXPAND_OPEN); AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT); return response.isAcknowledged(); } catch (IOException e) { log.error(e.getMessage()); return false; } }