use of org.elasticsearch.client.Response in project graylog2-server by Graylog2.
the class ClusterStateApi method fields.
public Map<String, Set<String>> fields(Collection<String> indices) {
final Request request = request(indices);
final JsonNode jsonResponse = client.execute((c, requestOptions) -> {
request.setOptions(requestOptions);
final Response response = c.getLowLevelClient().performRequest(request);
return objectMapper.readTree(response.getEntity().getContent());
}, "Unable to retrieve fields from indices: " + String.join(",", indices));
// noinspection UnstableApiUsage
return Streams.stream(jsonResponse.path("metadata").path("indices").fields()).flatMap(index -> allFieldsFromIndex(index.getKey(), index.getValue())).collect(groupingBy(Map.Entry::getKey, mapping(Map.Entry::getValue, Collectors.toSet())));
}
use of org.elasticsearch.client.Response in project beam by apache.
the class ElasticsearchIOTestUtils method refreshIndexAndGetCurrentNumDocs.
/**
* Forces a refresh of the given index to make recently inserted documents available for search.
*
* @param restClient To use for issuing queries
* @param index The Elasticsearch index
* @param type The Elasticsearch type
* @param urlParams Optional key/value pairs describing URL params for ES APIs
* @return The number of docs in the index
* @throws IOException On error communicating with Elasticsearch
*/
static long refreshIndexAndGetCurrentNumDocs(RestClient restClient, String index, String type, int backendVersion, @Nullable Map<String, String> urlParams) throws IOException {
long result = 0;
try {
flushAndRefreshAllIndices(restClient);
String endPoint = generateSearchPath(index, type);
Request request = new Request("GET", endPoint);
if (urlParams != null) {
request.addParameters(urlParams);
}
Response response = restClient.performRequest(request);
JsonNode searchResult = ElasticsearchIO.parseResponse(response.getEntity());
if (backendVersion >= 7) {
result = searchResult.path("hits").path("total").path("value").asLong();
} else {
result = searchResult.path("hits").path("total").asLong();
}
} catch (IOException e) {
// In that cases index/type has not been created (created upon first doc insertion)
if (!e.getMessage().contains("index_not_found_exception")) {
throw e;
}
}
return result;
}
use of org.elasticsearch.client.Response in project beam by apache.
the class ElasticsearchIOTestUtils method countByMatch.
/**
* Executes a match query for given field/value and returns the count of results.
*
* @param connectionConfiguration Specifies the index and type
* @param restClient To use to execute the call
* @param field The field to query
* @param value The value to match
* @param urlParams Optional key/value pairs describing URL params for ES APIs
* @param versionNumberCountPair Optional pair of [version_number, expected_num_doc_with_version]
* @return The count of documents in the search result
* @throws IOException On error communicating with Elasticsearch
*/
static int countByMatch(ConnectionConfiguration connectionConfiguration, RestClient restClient, String field, String value, @Nullable Map<String, String> urlParams, @Nullable KV<Integer, Long> versionNumberCountPair) throws IOException {
String size = versionNumberCountPair == null ? "10" : versionNumberCountPair.getValue().toString();
String requestBody = "{\n" + "\"size\": " + size + ",\n" + "\"version\" : true,\n" + " \"query\" : {\"match\": {\n" + " \"" + field + "\": \"" + value + "\"\n" + " }}\n" + "}\n";
String endPoint = generateSearchPath(connectionConfiguration);
HttpEntity httpEntity = new NStringEntity(requestBody, ContentType.APPLICATION_JSON);
Request request = new Request("GET", endPoint);
request.setEntity(httpEntity);
if (urlParams != null) {
request.addParameters(urlParams);
}
Response response = restClient.performRequest(request);
JsonNode searchResult = parseResponse(response.getEntity());
if (versionNumberCountPair != null) {
int numHits = 0;
for (JsonNode hit : searchResult.path("hits").path("hits")) {
if (hit.path("_version").asInt() == versionNumberCountPair.getKey()) {
numHits++;
}
}
return numHits;
}
if (getBackendVersion(connectionConfiguration) >= 7) {
return searchResult.path("hits").path("total").path("value").asInt();
} else {
return searchResult.path("hits").path("total").asInt();
}
}
use of org.elasticsearch.client.Response in project beam by apache.
the class ElasticsearchIOTestUtils method insertTestDocuments.
/**
* Inserts the given number of test documents into Elasticsearch.
*/
static void insertTestDocuments(ConnectionConfiguration connectionConfiguration, List<String> data, RestClient restClient) throws IOException {
StringBuilder bulkRequest = new StringBuilder();
int i = 0;
for (String document : data) {
bulkRequest.append(String.format("{ \"index\" : { \"_index\" : \"%s\", \"_type\" : \"%s\", \"_id\" : \"%s\" } }%n%s%n", connectionConfiguration.getIndex(), connectionConfiguration.getType(), i++, document));
}
String endPoint = String.format("/%s/%s/_bulk", connectionConfiguration.getIndex(), connectionConfiguration.getType());
HttpEntity requestBody = new NStringEntity(bulkRequest.toString(), ContentType.APPLICATION_JSON);
Request request = new Request("POST", endPoint);
request.setEntity(requestBody);
Response response = restClient.performRequest(request);
ElasticsearchIO.createWriteReport(response.getEntity(), Collections.emptySet(), true);
flushAndRefreshAllIndices(restClient);
}
use of org.elasticsearch.client.Response in project hazelcast by hazelcast.
the class ElasticCatClient method nodes.
/**
* Returns list of nodes currently in ES cluster
*/
public List<Node> nodes() {
try {
Request r = new Request("GET", "/_cat/nodes");
r.addParameter("format", "json");
r.addParameter("full_id", "true");
r.addParameter("h", "id,ip,name,http_address,master");
Response res = withRetry(() -> client.performRequest(r), retries);
try (InputStreamReader reader = new InputStreamReader(res.getEntity().getContent(), UTF_8)) {
JsonArray array = Json.parse(reader).asArray();
List<Node> nodes = new ArrayList<>(array.size());
for (JsonValue value : array) {
Optional<Node> shard = convertToNode(value);
shard.ifPresent(nodes::add);
}
LOG.fine("Nodes: " + nodes);
return nodes;
}
} catch (IOException e) {
throw new JetException("Could not get ES cluster nodes", e);
}
}
Aggregations