use of org.opensearch.client.core.CountRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testCount.
public void testCount() throws Exception {
String[] indices = randomIndicesNames(0, 5);
CountRequest countRequest = new CountRequest(indices);
Map<String, String> expectedParams = new HashMap<>();
setRandomCountParams(countRequest, expectedParams);
setRandomIndicesOptions(countRequest::indicesOptions, countRequest::indicesOptions, expectedParams);
if (randomBoolean()) {
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
countRequest.source(searchSourceBuilder);
} else {
countRequest.query(new MatchAllQueryBuilder());
}
Request request = RequestConverters.count(countRequest);
StringJoiner endpoint = new StringJoiner("/", "/", "");
String index = String.join(",", indices);
if (Strings.hasLength(index)) {
endpoint.add(index);
}
endpoint.add("_count");
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
assertEquals(endpoint.toString(), request.getEndpoint());
assertEquals(expectedParams, request.getParameters());
assertToXContentBody(countRequest, request.getEntity());
}
use of org.opensearch.client.core.CountRequest in project OpenSearch by opensearch-project.
the class SearchIT method testCountMultipleIndicesMatchQuery.
public void testCountMultipleIndicesMatchQuery() throws IOException {
CountRequest countRequest = new CountRequest("index1", "index2", "index3");
if (randomBoolean()) {
countRequest.source(new SearchSourceBuilder().query(new MatchQueryBuilder("field", "value1")));
} else {
countRequest.query(new MatchQueryBuilder("field", "value1"));
}
CountResponse countResponse = execute(countRequest, highLevelClient()::count, highLevelClient()::countAsync);
assertCountHeader(countResponse);
assertEquals(3, countResponse.getCount());
}
use of org.opensearch.client.core.CountRequest in project OpenSearch by opensearch-project.
the class SearchIT method testCountOneIndexNoQuery.
public void testCountOneIndexNoQuery() throws IOException {
CountRequest countRequest = new CountRequest("index");
CountResponse countResponse = execute(countRequest, highLevelClient()::count, highLevelClient()::countAsync);
assertCountHeader(countResponse);
assertEquals(5, countResponse.getCount());
}
use of org.opensearch.client.core.CountRequest in project OpenSearch by opensearch-project.
the class SearchDocumentationIT method testCount.
@SuppressWarnings({ "unused", "unchecked" })
public void testCount() throws Exception {
indexCountTestData();
RestHighLevelClient client = highLevelClient();
{
// tag::count-request-basic
// <1>
CountRequest countRequest = new CountRequest();
// <2>
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// <3>
searchSourceBuilder.query(QueryBuilders.matchAllQuery());
// <4>
countRequest.source(searchSourceBuilder);
// end::count-request-basic
}
{
// tag::count-request-args
CountRequest countRequest = // <1>
new CountRequest("blog").routing(// <2>
"routing").indicesOptions(// <3>
IndicesOptions.lenientExpandOpen()).preference(// <4>
"_local");
// end::count-request-args
assertNotNull(client.count(countRequest, RequestOptions.DEFAULT));
}
{
// tag::count-source-basics
// <1>
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
// <2>
sourceBuilder.query(QueryBuilders.termQuery("user", "foobar"));
// end::count-source-basics
// tag::count-source-setter
CountRequest countRequest = new CountRequest();
countRequest.indices("blog", "author");
countRequest.source(sourceBuilder);
// end::count-source-setter
// tag::count-execute
CountResponse countResponse = client.count(countRequest, RequestOptions.DEFAULT);
// end::count-execute
// tag::count-execute-listener
ActionListener<CountResponse> listener = new ActionListener<CountResponse>() {
@Override
public void onResponse(CountResponse countResponse) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::count-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::count-execute-async
// <1>
client.countAsync(countRequest, RequestOptions.DEFAULT, listener);
// end::count-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
// tag::count-response-1
long count = countResponse.getCount();
RestStatus status = countResponse.status();
Boolean terminatedEarly = countResponse.isTerminatedEarly();
// end::count-response-1
// tag::count-response-2
int totalShards = countResponse.getTotalShards();
int skippedShards = countResponse.getSkippedShards();
int successfulShards = countResponse.getSuccessfulShards();
int failedShards = countResponse.getFailedShards();
for (ShardSearchFailure failure : countResponse.getShardFailures()) {
// failures should be handled here
}
// end::count-response-2
assertNotNull(countResponse);
assertEquals(4, countResponse.getCount());
}
}
use of org.opensearch.client.core.CountRequest in project OpenSearch by opensearch-project.
the class SearchIT method testCountAllIndicesMatchQuery.
public void testCountAllIndicesMatchQuery() throws IOException {
CountRequest countRequest = new CountRequest();
countRequest.source(new SearchSourceBuilder().query(new MatchQueryBuilder("field", "value1")));
CountResponse countResponse = execute(countRequest, highLevelClient()::count, highLevelClient()::countAsync);
assertCountHeader(countResponse);
assertEquals(3, countResponse.getCount());
}
Aggregations