use of org.elasticsearch.client.Request 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.Request 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.Request in project beam by apache.
the class ElasticsearchIOTestUtils method setDefaultTemplate.
public static void setDefaultTemplate(RestClient restClient) throws IOException {
Request request = new Request("PUT", "/_template/default");
NStringEntity body = new NStringEntity("{" + "\"order\": 0," + "\"index_patterns\": [\"*\"]," + "\"template\": \"*\"," + "\"settings\": {" + " \"index.number_of_shards\": 1," + " \"index.number_of_replicas\": 0," + " \"index.store.stats_refresh_interval\": 0" + " }" + "}", ContentType.APPLICATION_JSON);
request.setEntity(body);
restClient.performRequest(request);
}
use of org.elasticsearch.client.Request in project beam by apache.
the class ElasticsearchIOTestUtils method copyIndex.
/**
* Synchronously deletes the target if it exists and then (re)creates it as a copy of source
* synchronously.
*/
static void copyIndex(RestClient restClient, String source, String target) throws IOException {
deleteIndex(restClient, target);
HttpEntity entity = new NStringEntity(String.format("{\"source\" : { \"index\" : \"%s\" }, \"dest\" : { \"index\" : \"%s\" } }", source, target), ContentType.APPLICATION_JSON);
Request request = new Request("POST", "/_reindex");
flushAndRefreshAllIndices(restClient);
request.setEntity(entity);
restClient.performRequest(request);
}
use of org.elasticsearch.client.Request 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);
}
Aggregations