/** * Fully removes a type from an index (removes data) * @param index index name * @param type type * @throws IOException In case of error */ public void deleteByQuery(String index, String type) throws IOException { logger.debug("deleteByQuery [{}]/[{}]", index, type); String deleteByQuery = "{\n" + " \"query\": {\n" + " \"match_all\": {}\n" + " }\n" + "}"; Request request = new Request("POST", "/" + index + "/" + type + "/_delete_by_query"); request.setJsonEntity(deleteByQuery); Response restResponse = client.getLowLevelClient().performRequest(request); Map<String, Object> response = asMap(restResponse); logger.debug("reindex response: {}", response); }
/** * Reindex data from one index/type to another index * @param sourceIndex source index name * @param sourceType source type name * @param targetIndex target index name * @return The number of documents that have been reindexed * @throws IOException In case of error */ public int reindex(String sourceIndex, String sourceType, String targetIndex) throws IOException { logger.debug("reindex [{}]/[{}] -> [{}]/[doc]", sourceIndex, sourceType, targetIndex); String reindexQuery = "{ \"source\": {\n" + " \"index\": \"" + sourceIndex + "\",\n" + " \"type\": \"" + sourceType + "\"\n" + " },\n" + " \"dest\": {\n" + " \"index\": \"" + targetIndex + "\",\n" + " \"type\": \"doc\"\n" + " }\n" + "}\n"; logger.trace("{}", reindexQuery); Request request = new Request("POST", "/_reindex"); request.setJsonEntity(reindexQuery); Response restResponse = client.getLowLevelClient().performRequest(request); Map<String, Object> response = asMap(restResponse); logger.debug("reindex response: {}", response); return (int) response.get("total"); }
/** * Reindex data from one index/type to another index * @param sourceIndex source index name * @param sourceType source type name * @param targetIndex target index name * @return The number of documents that have been reindexed * @throws IOException In case of error */ public int reindex(String sourceIndex, String sourceType, String targetIndex) throws IOException { logger.debug("reindex [{}]/[{}] -> [{}]/[doc]", sourceIndex, sourceType, targetIndex); String reindexQuery = "{ \"source\": {\n" + " \"index\": \"" + sourceIndex + "\",\n" + " \"type\": \"" + sourceType + "\"\n" + " },\n" + " \"dest\": {\n" + " \"index\": \"" + targetIndex + "\",\n" + " \"type\": \"doc\"\n" + " }\n" + "}\n"; logger.trace("{}", reindexQuery); Request request = new Request("POST", "/_reindex"); request.setJsonEntity(reindexQuery); Response restResponse = client.getLowLevelClient().performRequest(request); Map<String, Object> response = asMap(restResponse); logger.debug("reindex response: {}", response); return (int) response.get("total"); }
/** * Fully removes a type from an index (removes data) * @param index index name * @param type type * @throws IOException In case of error */ public void deleteByQuery(String index, String type) throws IOException { logger.debug("deleteByQuery [{}]/[{}]", index, type); String deleteByQuery = "{\n" + " \"query\": {\n" + " \"match_all\": {}\n" + " }\n" + "}"; Request request = new Request("POST", "/" + index + "/" + type + "/_delete_by_query"); request.setJsonEntity(deleteByQuery); Response restResponse = client.getLowLevelClient().performRequest(request); Map<String, Object> response = asMap(restResponse); logger.debug("reindex response: {}", response); }
@Override public void performLowLevelRequest(String method, String endpoint, String jsonEntity) throws IOException { Request request = new Request(method, endpoint); if (!isNullOrEmpty(jsonEntity)) { request.setJsonEntity(jsonEntity); } client.getLowLevelClient().performRequest(request); }
@Override public void performLowLevelRequest(String method, String endpoint, String jsonEntity) throws IOException { Request request = new Request(method, endpoint); if (!isNullOrEmpty(jsonEntity)) { request.setJsonEntity(jsonEntity); } client.getLowLevelClient().performRequest(request); }
/** * Update settings in Elasticsearch * @param client Elasticsearch client * @param index Index name * @param settings Settings if any, null if no update settings * @throws Exception if the elasticsearch API call is failing */ private static void updateIndexWithSettingsInElasticsearch(RestClient client, String index, String settings) throws Exception { logger.trace("updateIndex([{}])", index); assert client != null; assert index != null; if (settings != null) { logger.trace("Found update settings for index [{}]: [{}]", index, settings); logger.debug("updating settings for index [{}]", index); Request request = new Request("PUT", "/" + index + "/_settings"); request.setJsonEntity(settings); client.performRequest(request); } logger.trace("/updateIndex([{}])", index); }
/** * Update settings in Elasticsearch * @param client Elasticsearch client * @param index Index name * @param settings Settings if any, null if no update settings * @throws Exception if the elasticsearch API call is failing */ private static void updateIndexWithSettingsInElasticsearch(RestClient client, String index, String settings) throws Exception { logger.trace("updateIndex([{}])", index); assert client != null; assert index != null; if (settings != null) { logger.trace("Found update settings for index [{}]: [{}]", index, settings); logger.debug("updating settings for index [{}]", index); Request request = new Request("PUT", "/" + index + "/_settings"); request.setJsonEntity(settings); client.performRequest(request); } logger.trace("/updateIndex([{}])", index); }
/** * Create a new index in Elasticsearch * @param client Elasticsearch client * @param template Template name * @param json JSon content for the template * @throws Exception if something goes wrong */ private static void createTemplateWithJsonInElasticsearch(RestClient client, String template, String json) throws Exception { logger.trace("createTemplate([{}])", template); assert client != null; assert template != null; Request request = new Request("PUT", "/_template/" + template); request.setJsonEntity(json); Response response = client.performRequest(request); if (response.getStatusLine().getStatusCode() != 200) { logger.warn("Could not create template [{}]", template); throw new Exception("Could not create template ["+template+"]."); } logger.trace("/createTemplate([{}])", template); }
/** * Create a new index in Elasticsearch * @param client Elasticsearch client * @param template Template name * @param json JSon content for the template * @throws Exception if something goes wrong */ private static void createTemplateWithJsonInElasticsearch(RestClient client, String template, String json) throws Exception { logger.trace("createTemplate([{}])", template); assert client != null; assert template != null; Request request = new Request("PUT", "/_template/" + template); request.setJsonEntity(json); Response response = client.performRequest(request); if (response.getStatusLine().getStatusCode() != 200) { logger.warn("Could not create template [{}]", template); throw new Exception("Could not create template ["+template+"]."); } logger.trace("/createTemplate([{}])", template); }
@Override public void createMapping(String indexName, String type, String jsonMapping) { Request request = new Request("PUT", String.format("/%s/%s/_mapping", indexName, type)); request.setJsonEntity(jsonMapping); Response response = performRequest(request); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new NuxeoException(String.format("Fail to create mapping on %s/%s: %s", indexName, type, response)); } }
/** * Create an alias if needed * @param client Client to use * @param alias Alias name * @param index Index name * @throws Exception When alias can not be set */ public static void createAlias(RestClient client, String alias, String index) throws Exception { logger.trace("createAlias({},{})", alias, index); assert client != null; assert alias != null; assert index != null; Request request = new Request("POST", "/_aliases/"); request.setJsonEntity("{\"actions\":[{\"add\":{\"index\":\"" + index +"\",\"alias\":\"" + alias +"\"}}]}"); Response response = client.performRequest(request); if (response.getStatusLine().getStatusCode() != 200) { logger.warn("Could not create alias [{}] on index [{}]", alias, index); throw new Exception("Could not create alias ["+alias+"] on index ["+index+"]."); } logger.trace("/createAlias({},{})", alias, index); } }
/** * Create a new index in Elasticsearch * @param client Elasticsearch client * @param index Index name * @param settings Settings if any, null if no specific settings * @throws Exception if the elasticsearch API call is failing */ private static void createIndexWithSettingsInElasticsearch(RestClient client, String index, String settings) throws Exception { logger.trace("createIndex([{}])", index); assert client != null; assert index != null; Request request = new Request("PUT", "/" + index); // If there are settings for this index, we use it. If not, using Elasticsearch defaults. if (settings != null) { logger.trace("Found settings for index [{}]: [{}]", index, settings); request.setJsonEntity(settings); } Response response = client.performRequest(request); if (response.getStatusLine().getStatusCode() != 200) { logger.warn("Could not create index [{}]", index); throw new Exception("Could not create index ["+index+"]."); } logger.trace("/createIndex([{}])", index); }
/** * Create an alias if needed * @param client Client to use * @param alias Alias name * @param index Index name * @throws Exception When alias can not be set */ public static void createAlias(RestClient client, String alias, String index) throws Exception { logger.trace("createAlias({},{})", alias, index); assert client != null; assert alias != null; assert index != null; Request request = new Request("POST", "/_aliases/"); request.setJsonEntity("{\"actions\":[{\"add\":{\"index\":\"" + index +"\",\"alias\":\"" + alias +"\"}}]}"); Response response = client.performRequest(request); if (response.getStatusLine().getStatusCode() != 200) { logger.warn("Could not create alias [{}] on index [{}]", alias, index); throw new Exception("Could not create alias ["+alias+"] on index ["+index+"]."); } logger.trace("/createAlias({},{})", alias, index); } }
/** * Create a new index in Elasticsearch * @param client Elasticsearch client * @param index Index name * @param settings Settings if any, null if no specific settings * @throws Exception if the elasticsearch API call is failing */ private static void createIndexWithSettingsInElasticsearch(RestClient client, String index, String settings) throws Exception { logger.trace("createIndex([{}])", index); assert client != null; assert index != null; Request request = new Request("PUT", "/" + index); // If there are settings for this index, we use it. If not, using Elasticsearch defaults. if (settings != null) { logger.trace("Found settings for index [{}]: [{}]", index, settings); request.setJsonEntity(settings); } Response response = client.performRequest(request); if (response.getStatusLine().getStatusCode() != 200) { logger.warn("Could not create index [{}]", index); throw new Exception("Could not create index ["+index+"]."); } logger.trace("/createIndex([{}])", index); }
@Override public void createIndex(String indexName, String jsonSettings) { Request request = new Request("PUT", "/" + indexName + "?timeout=" + CREATE_INDEX_TIMEOUT); request.setJsonEntity(jsonSettings); Response response = performRequest(request); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { throw new NuxeoException("Fail to create index: " + indexName + " :" + response); } }
static Request clearScroll(String scroll, Version remoteVersion) { Request request = new Request("DELETE", "/_search/scroll"); if (remoteVersion.before(Version.fromId(2000099))) { // Versions before 2.0.0 extract the plain scroll_id from the body request.setEntity(new NStringEntity(scroll, ContentType.TEXT_PLAIN)); return request; } try (XContentBuilder entity = JsonXContent.contentBuilder()) { entity.startObject() .array("scroll_id", scroll) .endObject(); request.setJsonEntity(Strings.toString(entity)); } catch (IOException e) { throw new ElasticsearchException("failed to build clear scroll entity", e); } return request; } }
static Request scroll(String scroll, TimeValue keepAlive, Version remoteVersion) { Request request = new Request("POST", "/_search/scroll"); if (remoteVersion.before(Version.V_5_0_0)) { /* Versions of Elasticsearch before 5.0 couldn't parse nanos or micros * so we toss out that resolution, rounding up so we shouldn't end up * with 0s. */ keepAlive = timeValueMillis((long) Math.ceil(keepAlive.millisFrac())); } request.addParameter("scroll", keepAlive.getStringRep()); if (remoteVersion.before(Version.fromId(2000099))) { // Versions before 2.0.0 extract the plain scroll_id from the body request.setEntity(new NStringEntity(scroll, ContentType.TEXT_PLAIN)); return request; } try (XContentBuilder entity = JsonXContent.contentBuilder()) { entity.startObject() .field("scroll_id", scroll) .endObject(); request.setJsonEntity(Strings.toString(entity)); } catch (IOException e) { throw new ElasticsearchException("failed to build scroll entity", e); } return request; }
static Request putLicense(PutLicenseRequest putLicenseRequest) { String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_xpack", "license").build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); RequestConverters.Params parameters = new RequestConverters.Params(request); parameters.withTimeout(putLicenseRequest.timeout()); parameters.withMasterTimeout(putLicenseRequest.masterNodeTimeout()); if (putLicenseRequest.isAcknowledge()) { parameters.putParam("acknowledge", "true"); } request.setJsonEntity(putLicenseRequest.getLicenseDefinition()); return request; }
static Request putLicense(PutLicenseRequest putLicenseRequest) { String endpoint = new RequestConverters.EndpointBuilder().addPathPartAsIs("_xpack", "license").build(); Request request = new Request(HttpPut.METHOD_NAME, endpoint); RequestConverters.Params parameters = new RequestConverters.Params(request); parameters.withTimeout(putLicenseRequest.timeout()); parameters.withMasterTimeout(putLicenseRequest.masterNodeTimeout()); if (putLicenseRequest.isAcknowledge()) { parameters.putParam("acknowledge", "true"); } request.setJsonEntity(putLicenseRequest.getLicenseDefinition()); return request; }