use of org.elasticsearch.client.Response in project janusgraph by JanusGraph.
the class RestElasticSearchClient method bulkRequest.
@Override
public void bulkRequest(List<ElasticSearchMutation> requests, String ingestPipeline) throws IOException {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
for (final ElasticSearchMutation request : requests) {
final Map actionData = ImmutableMap.of(request.getRequestType().name().toLowerCase(), ImmutableMap.of("_index", request.getIndex(), "_type", request.getType(), "_id", request.getId()));
outputStream.write(mapWriter.writeValueAsBytes(actionData));
outputStream.write("\n".getBytes(UTF8_CHARSET));
if (request.getSource() != null) {
outputStream.write(mapWriter.writeValueAsBytes(request.getSource()));
outputStream.write("\n".getBytes(UTF8_CHARSET));
}
}
final StringBuilder builder = new StringBuilder();
if (ingestPipeline != null) {
APPEND_OP.apply(builder).append("pipeline=").append(ingestPipeline);
}
if (bulkRefresh != null && !bulkRefresh.toLowerCase().equals("false")) {
APPEND_OP.apply(builder).append("refresh=").append(bulkRefresh);
}
builder.insert(0, REQUEST_SEPARATOR + "_bulk");
final Response response = performRequest(REQUEST_TYPE_POST, builder.toString(), outputStream.toByteArray());
try (final InputStream inputStream = response.getEntity().getContent()) {
final RestBulkResponse bulkResponse = mapper.readValue(inputStream, RestBulkResponse.class);
final List<Object> errors = bulkResponse.getItems().stream().flatMap(item -> item.values().stream()).filter(item -> item.getError() != null && item.getStatus() != 404).map(RestBulkItemResponse::getError).collect(Collectors.toList());
if (!errors.isEmpty()) {
errors.forEach(error -> log.error("Failed to execute ES query: {}", error));
throw new IOException("Failure(s) in Elasticsearch bulk request: " + errors);
}
}
}
use of org.elasticsearch.client.Response in project janusgraph by JanusGraph.
the class RestElasticSearchClient method getMajorVersion.
@Override
public ElasticMajorVersion getMajorVersion() {
if (majorVersion != null) {
return majorVersion;
}
majorVersion = DEFAULT_VERSION;
try {
final Response response = delegate.performRequest(REQUEST_TYPE_GET, REQUEST_SEPARATOR);
try (final InputStream inputStream = response.getEntity().getContent()) {
final ClusterInfo info = mapper.readValue(inputStream, ClusterInfo.class);
majorVersion = ElasticMajorVersion.parse(info.getVersion() != null ? (String) info.getVersion().get("number") : null);
}
} catch (final IOException e) {
log.warn("Unable to determine Elasticsearch server version. Default to {}.", majorVersion, e);
}
return majorVersion;
}
use of org.elasticsearch.client.Response in project janusgraph by JanusGraph.
the class RestElasticSearchClient method clusterHealthRequest.
@Override
public void clusterHealthRequest(String timeout) throws IOException {
final Map<String, String> params = ImmutableMap.of("wait_for_status", "yellow", "timeout", timeout);
final Response response = delegate.performRequest(REQUEST_TYPE_GET, REQUEST_SEPARATOR + "_cluster" + REQUEST_SEPARATOR + "health", params);
try (final InputStream inputStream = response.getEntity().getContent()) {
final Map<String, Object> values = mapReader.readValue(inputStream);
if (!values.containsKey("timed_out")) {
throw new IOException("Unexpected response for Elasticsearch cluster health request");
} else if (!Objects.equals(values.get("timed_out"), false)) {
throw new IOException("Elasticsearch timeout waiting for yellow status");
}
}
}
use of org.elasticsearch.client.Response in project janusgraph by JanusGraph.
the class RestElasticSearchClient method search.
@Override
public RestSearchResponse search(String scrollId) throws IOException {
final String path;
final byte[] requestData;
if (ElasticMajorVersion.ONE == majorVersion) {
path = REQUEST_SEPARATOR + "_search" + REQUEST_SEPARATOR + "scroll" + REQUEST_PARAM_BEGINNING + "scroll=" + scrollKeepAlive;
requestData = scrollId.getBytes(UTF8_CHARSET);
} else {
path = REQUEST_SEPARATOR + "_search" + REQUEST_SEPARATOR + "scroll";
final Map<String, Object> request = new HashMap<>();
request.put("scroll", scrollKeepAlive);
request.put("scroll_id", scrollId);
requestData = mapper.writeValueAsBytes(request);
if (log.isDebugEnabled()) {
log.debug("Elasticsearch request: " + mapper.writerWithDefaultPrettyPrinter().writeValueAsString(request));
}
}
final Response response = performRequest(REQUEST_TYPE_POST, path, requestData);
try (final InputStream inputStream = response.getEntity().getContent()) {
return mapper.readValue(inputStream, RestSearchResponse.class);
}
}
use of org.elasticsearch.client.Response in project nifi by apache.
the class ElasticSearchClientServiceImpl method search.
@Override
public SearchResponse search(String query, String index, String type) throws IOException {
Response response = runQuery(query, index, type);
Map<String, Object> parsed = parseResponse(response);
int took = (Integer) parsed.get("took");
boolean timedOut = (Boolean) parsed.get("timed_out");
Map<String, Object> aggregations = parsed.get("aggregations") != null ? (Map<String, Object>) parsed.get("aggregations") : new HashMap<>();
Map<String, Object> hitsParent = (Map<String, Object>) parsed.get("hits");
int count = (Integer) hitsParent.get("total");
List<Map<String, Object>> hits = (List<Map<String, Object>>) hitsParent.get("hits");
SearchResponse esr = new SearchResponse(hits, aggregations, count, took, timedOut);
if (getLogger().isDebugEnabled()) {
StringBuilder sb = new StringBuilder();
sb.append("******************");
sb.append(String.format("Took: %d", took));
sb.append(String.format("Timed out: %s", timedOut));
sb.append(String.format("Aggregation count: %d", aggregations.size()));
sb.append(String.format("Hit count: %d", hits.size()));
sb.append(String.format("Total found: %d", count));
sb.append("******************");
getLogger().debug(sb.toString());
}
return esr;
}
Aggregations