Search in sources :

Example 1 with CountResponse

use of org.opensearch.client.core.CountResponse 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());
}
Also used : CountRequest(org.opensearch.client.core.CountRequest) MatchQueryBuilder(org.opensearch.index.query.MatchQueryBuilder) CountResponse(org.opensearch.client.core.CountResponse) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 2 with CountResponse

use of org.opensearch.client.core.CountResponse 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());
}
Also used : CountRequest(org.opensearch.client.core.CountRequest) CountResponse(org.opensearch.client.core.CountResponse)

Example 3 with CountResponse

use of org.opensearch.client.core.CountResponse 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());
    }
}
Also used : CountResponse(org.opensearch.client.core.CountResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) CountRequest(org.opensearch.client.core.CountRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) RestStatus(org.opensearch.rest.RestStatus) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure)

Example 4 with CountResponse

use of org.opensearch.client.core.CountResponse 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());
}
Also used : CountRequest(org.opensearch.client.core.CountRequest) MatchQueryBuilder(org.opensearch.index.query.MatchQueryBuilder) CountResponse(org.opensearch.client.core.CountResponse) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 5 with CountResponse

use of org.opensearch.client.core.CountResponse in project OpenSearch by opensearch-project.

the class SearchIT method testCountMultipleIndicesMatchQueryUsingConstructor.

public void testCountMultipleIndicesMatchQueryUsingConstructor() throws IOException {
    CountRequest countRequest;
    if (randomBoolean()) {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder().query(new MatchQueryBuilder("field", "value1"));
        countRequest = new CountRequest(new String[] { "index1", "index2", "index3" }, sourceBuilder);
    } else {
        QueryBuilder query = new MatchQueryBuilder("field", "value1");
        countRequest = new CountRequest(new String[] { "index1", "index2", "index3" }, query);
    }
    CountResponse countResponse = execute(countRequest, highLevelClient()::count, highLevelClient()::countAsync);
    assertCountHeader(countResponse);
    assertEquals(3, countResponse.getCount());
}
Also used : CountRequest(org.opensearch.client.core.CountRequest) MatchQueryBuilder(org.opensearch.index.query.MatchQueryBuilder) CountResponse(org.opensearch.client.core.CountResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) QueryBuilder(org.opensearch.index.query.QueryBuilder) TermsQueryBuilder(org.opensearch.index.query.TermsQueryBuilder) ScriptQueryBuilder(org.opensearch.index.query.ScriptQueryBuilder) MatchQueryBuilder(org.opensearch.index.query.MatchQueryBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Aggregations

CountRequest (org.opensearch.client.core.CountRequest)8 CountResponse (org.opensearch.client.core.CountResponse)8 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)5 MatchQueryBuilder (org.opensearch.index.query.MatchQueryBuilder)4 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 ActionListener (org.opensearch.action.ActionListener)1 LatchedActionListener (org.opensearch.action.LatchedActionListener)1 ShardSearchFailure (org.opensearch.action.search.ShardSearchFailure)1 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)1 QueryBuilder (org.opensearch.index.query.QueryBuilder)1 ScriptQueryBuilder (org.opensearch.index.query.ScriptQueryBuilder)1 TermsQueryBuilder (org.opensearch.index.query.TermsQueryBuilder)1 RestStatus (org.opensearch.rest.RestStatus)1