private Map<String, Object> parseResponse(Response response) throws IOException { final int code = response.getStatusLine().getStatusCode(); if (code >= 200 & code < 300) { InputStream inputStream = response.getEntity().getContent(); byte[] result = IOUtils.toByteArray(inputStream); inputStream.close(); return mapper.readValue(new String(result, charset), Map.class); } else { String errorMessage = String.format("ElasticSearch reported an error while trying to run the query: %s", response.getStatusLine().getReasonPhrase()); throw new IOException(errorMessage); } }
/** * Determines whether a resource exists in ES. This will call a GET method to a particular path and * return true if status 200; false otherwise. * * @param resourcePath The path of the resource to get. * @return True if it exists; false otherwise. * @throws IOException If an error occurred during requests to ES. */ public boolean doesResourceExist(final String resourcePath) throws IOException { Response response = elasticSearchAdminClient.performRequest(HttpMethod.HEAD, resourcePath); return response.getStatusLine().getStatusCode() == HttpStatus.SC_OK; }
try { final Response searchResponse = search(table, key); final int statusCode = searchResponse.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_NOT_FOUND) { return Status.NOT_FOUND; final Response deleteResponse = restClient.performRequest("DELETE", "/" + indexKey + "/" + table + "/" + hit.get("_id")); if (deleteResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { return Status.ERROR;
@Override public Status insert(final String table, final String key, final Map<String, ByteIterator> values) { try { final Map<String, String> data = StringByteIterator.getStringMap(values); data.put(KEY, key); final Response response = restClient.performRequest( "POST", "/" + indexKey + "/" + table + "/", Collections.<String, String>emptyMap(), new NStringEntity(new ObjectMapper().writeValueAsString(data), ContentType.APPLICATION_JSON)); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_CREATED) { return Status.ERROR; } if (!isRefreshNeeded) { synchronized (this) { isRefreshNeeded = true; } } return Status.OK; } catch (final Exception e) { e.printStackTrace(); return Status.ERROR; } }
@Override public boolean typeExists(String index, String type) { RestClient restClient = client.getLowLevelClient(); try { Response response = restClient.performRequest("HEAD", index + "/_mapping/" + type); return (response.getStatusLine().getStatusCode() == 200); } catch (Exception e) { throw new ElasticsearchException("Error while checking type exists for index: " + index + " type : " + type + " ", e); } }
try { final Response searchResponse = search(table, key); final int statusCode = searchResponse.getStatusLine().getStatusCode(); if (statusCode == 404) { return Status.NOT_FOUND;
try { final Response searchResponse = search(table, key); final int statusCode = searchResponse.getStatusLine().getStatusCode(); if (statusCode == 404) { return Status.NOT_FOUND; params, new NStringEntity(new ObjectMapper().writeValueAsString(source), ContentType.APPLICATION_JSON)); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { return Status.ERROR;
if (errorResponse.getStatusLine().getStatusCode() == HttpStatus.SC_BAD_REQUEST) { JsonNode root = objectMapper.readTree(EntityUtils.toString(errorResponse.getEntity())); String errorCode = root.get("error").get("type").asText();
final boolean exists = existsResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK; final int statusCode = deleteResponse.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { throw new DBException("delete [" + indexKey + "] failed with status [" + statusCode + "]"); final StringEntity entity = new StringEntity(builder.string()); final Response createResponse = performRequest(restClient, "PUT", "/" + indexKey, params, entity); final int statusCode = createResponse.getStatusLine().getStatusCode(); if (statusCode != HttpStatus.SC_OK) { throw new DBException("create [" + indexKey + "] failed with status [" + statusCode + "]"); final int healthStatusCode = healthResponse.getStatusLine().getStatusCode(); if (healthStatusCode != HttpStatus.SC_OK) { throw new DBException("cluster health [" + indexKey + "] failed with status [" + healthStatusCode + "]");
@Override public void recreateIndex(String indexName) throws IOException { RestClient restClient = restWriter.getRestLowLevelClient(); try { restClient.performRequest("DELETE", "/" + indexName); } catch (Exception e) { // ok since index may not exist } String indexSettings = "{\"settings\" : {\"index\":{\"number_of_shards\":1,\"number_of_replicas\":1}}}"; HttpEntity entity = new StringEntity(indexSettings, ContentType.APPLICATION_JSON); Response putResponse = restClient.performRequest("PUT", "/" + indexName, Collections.emptyMap(), entity); Assert.assertEquals(putResponse.getStatusLine().getStatusCode(),200, "Recreate index succeeded"); }
@Test public void elasticsearchVersion() throws IOException { try (ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch:5.6.12")) { container.start(); Response response = getClient(container).performRequest(new Request("GET", "/")); assertThat(response.getStatusLine().getStatusCode(), is(200)); String responseAsString = EntityUtils.toString(response.getEntity()); assertThat(responseAsString, containsString("5.6.12")); } }
@Test public void elasticsearchDefaultTest() throws IOException { try (ElasticsearchContainer container = new ElasticsearchContainer() .withEnv("foo", "bar") // dummy env for compiler checking correct generics usage ) { container.start(); Response response = getClient(container).performRequest(new Request("GET", "/")); assertThat(response.getStatusLine().getStatusCode(), is(200)); assertThat(EntityUtils.toString(response.getEntity()), containsString(ELASTICSEARCH_DEFAULT_VERSION)); // The default image is running with the features under Elastic License response = getClient(container).performRequest(new Request("GET", "/_xpack/")); assertThat(response.getStatusLine().getStatusCode(), is(200)); // For now we test that we have the monitoring feature available assertThat(EntityUtils.toString(response.getEntity()), containsString("monitoring")); } }
@Test public void elasticsearchOssImage() throws IOException { try (ElasticsearchContainer container = new ElasticsearchContainer("docker.elastic.co/elasticsearch/elasticsearch-oss:" + ELASTICSEARCH_DEFAULT_VERSION)) { container.start(); Response response = getClient(container).performRequest(new Request("GET", "/")); assertThat(response.getStatusLine().getStatusCode(), is(200)); // The OSS image does not have any feature under Elastic License assertThrows("We should not have /_xpack endpoint with an OSS License", ResponseException.class, () -> getClient(container).performRequest(new Request("GET", "/_xpack/"))); } }
/** * https://www.elastic.co/guide/en/elasticsearch/reference/5.6/indices-put-mapping.html * @param index * @param mappingType https://www.elastic.co/guide/en/elasticsearch/reference/5.6/mapping.html#mapping-type * @param mapping * @throws IOException */ public void putMapping(String index, String mappingType, String mapping) throws IOException { HttpEntity entity = new StringEntity(mapping); Response response = lowLevelClient.performRequest("PUT" , "/" + index + "/_mapping/" + mappingType , Collections.emptyMap() , entity ); if(response.getStatusLine().getStatusCode() != 200) { String responseStr = IOUtils.toString(response.getEntity().getContent()); throw new IllegalStateException("Got a " + response.getStatusLine().getStatusCode() + " due to " + responseStr); } }
/** * Gets ALL Elasticsearch indices, or null if status code returned is not OK 200. */ public String[] getIndices() throws IOException { Response response = lowLevelClient.performRequest("GET", "/_cat/indices"); if(response.getStatusLine().getStatusCode() == 200) { String responseStr = IOUtils.toString(response.getEntity().getContent()); List<String> indices = new ArrayList<>(); for(String line : Splitter.on("\n").split(responseStr)) { Iterable<String> splits = Splitter.on(" ").split(line.replaceAll("\\s+", " ").trim()); if(Iterables.size(splits) > 3) { String index = Iterables.get(splits, 2, ""); if(!StringUtils.isEmpty(index)) { indices.add(index.trim()); } } } String[] ret = new String[indices.size()]; ret=indices.toArray(ret); return ret; } return null; }
@Override public void deleteIndex(String index) throws IOException { try { Response response = lowLevelClient.performRequest("DELETE", "/" + index); logger.trace("delete index response: {}", asMap(response)); } catch (ResponseException e) { if (e.getResponse().getStatusLine().getStatusCode() == 404) { logger.trace("index does not exist. Ignoring error..."); return; } throw 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); try { Response restResponse = lowLevelClient.performRequest("GET", "/" + index); logger.trace("get index metadata response: {}", asMap(restResponse)); return true; } catch (ResponseException e) { if (e.getResponse().getStatusLine().getStatusCode() == 404) { logger.debug("index [{}] does not exist", index); return false; } throw e; } }
/** * Check if a pipeline exists * @param pipelineName pipeline name * @return true if the pipeline exists, false otherwise * @throws IOException In case of error */ public boolean isExistingPipeline(String pipelineName) throws IOException { logger.debug("is existing pipeline [{}]", pipelineName); try { Response restResponse = lowLevelClient.performRequest("GET", "/_ingest/pipeline/" + pipelineName); logger.trace("get pipeline metadata response: {}", asMap(restResponse)); return true; } catch (ResponseException e) { if (e.getResponse().getStatusLine().getStatusCode() == 404) { logger.debug("pipeline [{}] does not exist", pipelineName); return false; } throw e; } }
@Override protected void doHealthCheck(Health.Builder builder) throws Exception { Response response = this.client .performRequest(new Request("GET", "/_cluster/health/")); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() != HttpStatus.SC_OK) { builder.down(); builder.withDetail("statusCode", statusLine.getStatusCode()); builder.withDetail("reasonPhrase", statusLine.getReasonPhrase()); return; } try (InputStream inputStream = response.getEntity().getContent()) { doHealthCheck(builder, StreamUtils.copyToString(inputStream, StandardCharsets.UTF_8)); } }
@Before public void setup() throws Exception { client = ElasticsearchClientFactory.create(globals()); retrieveDao = new ElasticsearchRetrieveLatestDao(client); writer = new ElasticsearchBulkDocumentWriter<>(client) .withRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); // add bro template JSONObject broTemplate = JSONUtils.INSTANCE.load(new File(broTemplatePath), JSONObject.class); String broTemplateJson = JSONUtils.INSTANCE.toJSON(broTemplate, true); HttpEntity broEntity = new NStringEntity(broTemplateJson, ContentType.APPLICATION_JSON); Response response = client .getLowLevelClient() .performRequest("PUT", "/_template/bro_template", Collections.emptyMap(), broEntity); assertThat(response.getStatusLine().getStatusCode(), CoreMatchers.equalTo(200)); }