@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); } }
@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 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 removeAlias(AliasQuery query) { Assert.notNull(query.getIndexName(), "No index defined for Alias"); Assert.notNull(query.getAliasName(), "No alias defined"); IndicesAliasesRequest request = new IndicesAliasesRequest(); AliasActions aliasAction = new AliasActions(AliasActions.Type.REMOVE); request.addAliasAction(aliasAction); try { return client.indices().updateAliases(request).isAcknowledged(); } catch (IOException e) { throw new ElasticsearchException("failed to update aliases with request: " + request, 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); } }
@Override public boolean putMapping(String indexName, String type, Object mapping) { Assert.notNull(indexName, "No index defined for putMapping()"); Assert.notNull(type, "No type defined for putMapping()"); PutMappingRequest request = new PutMappingRequest(indexName).type(type); if (mapping instanceof String) { request.source(String.valueOf(mapping), XContentType.JSON); } else if (mapping instanceof Map) { request.source((Map) mapping); } else if (mapping instanceof XContentBuilder) { request.source((XContentBuilder) mapping); } try { return client.indices().putMapping(request).isAcknowledged(); } catch (IOException e) { throw new ElasticsearchException("Failed to put mapping for " + indexName, e); } }
@Override public Boolean addAlias(AliasQuery query) { Assert.notNull(query.getIndexName(), "No index defined for Alias"); Assert.notNull(query.getAliasName(), "No alias defined"); final IndicesAliasesRequest.AliasActions aliasAction = IndicesAliasesRequest.AliasActions.add() .alias(query.getAliasName()).index(query.getIndexName()); if (query.getFilterBuilder() != null) { aliasAction.filter(query.getFilterBuilder()); } else if (query.getFilter() != null) { aliasAction.filter(query.getFilter()); } else if (hasText(query.getRouting())) { aliasAction.routing(query.getRouting()); } else if (hasText(query.getSearchRouting())) { aliasAction.searchRouting(query.getSearchRouting()); } else if (hasText(query.getIndexRouting())) { aliasAction.indexRouting(query.getIndexRouting()); } IndicesAliasesRequest request = new IndicesAliasesRequest(); request.addAliasAction(aliasAction); try { return client.indices().updateAliases(request).isAcknowledged(); } catch (IOException e) { throw new ElasticsearchException("failed to update aliases with request: " + request, e); } }
@Override public void deleteIndex(String index) throws IOException { client.indices().delete(new DeleteIndexRequest(index), RequestOptions.DEFAULT); }
@Override public void deleteIndex(String index) throws IOException { client.indices().delete(new DeleteIndexRequest(index), RequestOptions.DEFAULT); }
/** * 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); }
/** * 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); }
/** * 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); }
/** * 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); }
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; } }
/** * 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 * @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 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 existsIndex(String indexName, String type) throws IOException { try { GetIndexRequest request = new GetIndexRequest(); request.indices(indexName); return client.indices().exists(request); } catch (ElasticsearchException exception) { if (exception.status() == RestStatus.NOT_FOUND) { } } return false; }