/** * Queries {@code _mapping} definition to automatically detect all types for an index * * @return list of types associated with this index * @throws IOException for any IO related issues * @throws IllegalStateException if reply is not understood */ private Set<String> listTypesFromElastic() throws IOException { final String endpoint = "/" + index + "/_mapping"; final Response response = client.performRequest("GET", endpoint); try (InputStream is = response.getEntity().getContent()) { JsonNode root = mapper.readTree(is); if (!root.isObject() || root.size() != 1) { final String message = String.format(Locale.ROOT, "Invalid response for %s/%s " + "Expected object of size 1 got %s (of size %d)", response.getHost(), response.getRequestLine(), root.getNodeType(), root.size()); throw new IllegalStateException(message); } JsonNode mappings = root.iterator().next().get("mappings"); if (mappings == null || mappings.size() == 0) { final String message = String.format(Locale.ROOT, "Index %s does not have any types", index); throw new IllegalStateException(message); } Set<String> types = Sets.newHashSet(mappings.fieldNames()); types.remove("_default_"); return types; } }
private ElasticsearchJson.Result httpRequest(ObjectNode query) throws IOException { Objects.requireNonNull(query, "query"); String uri = String.format(Locale.ROOT, "/%s/%s/_search", indexName, typeName); Hook.QUERY_PLAN.run(query); final String json = mapper.writeValueAsString(query); LOGGER.debug("Elasticsearch Query: {}", json); HttpEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON); Response response = restClient.performRequest("POST", uri, Collections.emptyMap(), entity); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { final String error = EntityUtils.toString(response.getEntity()); final String message = String.format(Locale.ROOT, "Error while querying Elastic (on %s/%s) status: %s\nQuery:\n%s\nError:\n%s\n", response.getHost(), response.getRequestLine(), response.getStatusLine(), query, error); throw new RuntimeException(message); } try (InputStream is = response.getEntity().getContent()) { return mapper.readValue(is, ElasticsearchJson.Result.class); } }
private RequestBuilder handleResponseException(ResponseException e) throws OptimisticLockException { if (customExceptionHandler != null) { HttpEntity result = customExceptionHandler.apply(e); if (result != null) { responseEntity = result; return this; } } JSONObject error = extractErrorJSON(e); if (e.getResponse().getStatusLine().getStatusCode() == 409) { throw new OptimisticLockException(error.getString(PARAM_REASON), e); } throw Exceptions.handle() .to(Elastic.LOG) .error(e) .withSystemErrorMessage("Elasticsearch (%s) reported an error: %s (%s)", e.getResponse().getHost(), error == null ? "unknown" : error.getString(PARAM_REASON), error == null ? "-" : error.getString(PARAM_TYPE)) .handle(); }
private static String buildMessage(Response response) throws IOException { String message = response.getRequestLine().getMethod() + " " + response.getHost() + response.getRequestLine().getUri() + ": " + response.getStatusLine().toString(); HttpEntity entity = response.getEntity(); if (entity != null) { if (entity.isRepeatable() == false) { entity = new BufferedHttpEntity(entity); response.getHttpResponse().setEntity(entity); } message += "\n" + EntityUtils.toString(entity); } return message; }
private static String buildMessage(Response response) throws IOException { String message = String.format(Locale.ROOT, "method [%s], host [%s], URI [%s], status line [%s]", response.getRequestLine().getMethod(), response.getHost(), response.getRequestLine().getUri(), response.getStatusLine().toString() ); HttpEntity entity = response.getEntity(); if (entity != null) { if (entity.isRepeatable() == false) { entity = new BufferedHttpEntity(entity); response.getHttpResponse().setEntity(entity); } message += "\n" + EntityUtils.toString(entity); } return message; }
protected JSONObject extractErrorJSON(ResponseException e) { try { JSONObject response = JSON.parseObject(EntityUtils.toString(e.getResponse().getEntity())); return response.getJSONObject(PARAM_ERROR); } catch (IOException ex) { Exceptions.handle(Elastic.LOG, ex); throw Exceptions.handle() .to(Elastic.LOG) .error(e) .withSystemErrorMessage("Elasticsearch (%s) reported an error which cannot be unpacked: %s", e.getResponse().getHost()) .handle(); } }
private static String buildMessage(Response response) throws IOException { String message = String.format(Locale.ROOT, "method [%s], host [%s], URI [%s], status line [%s]", response.getRequestLine().getMethod(), response.getHost(), response.getRequestLine().getUri(), response.getStatusLine().toString() ); if (response.hasWarnings()) { message += "\n" + "Warnings: " + response.getWarnings(); } HttpEntity entity = response.getEntity(); if (entity != null) { if (entity.isRepeatable() == false) { entity = new BufferedHttpEntity(entity); response.getHttpResponse().setEntity(entity); } message += "\n" + EntityUtils.toString(entity); } return message; }