Search in sources :

Example 1 with CountRequest

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());
}
Also used : CountRequest(org.opensearch.client.core.CountRequest) HashMap(java.util.HashMap) RandomSearchRequestGenerator.randomSearchRequest(org.opensearch.search.RandomSearchRequestGenerator.randomSearchRequest) MasterNodeRequest(org.opensearch.action.support.master.MasterNodeRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) RatedRequest(org.opensearch.index.rankeval.RatedRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) ReplicationRequest(org.opensearch.action.support.replication.ReplicationRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) CountRequest(org.opensearch.client.core.CountRequest) BulkShardRequest(org.opensearch.action.bulk.BulkShardRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) StringJoiner(java.util.StringJoiner) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 2 with CountRequest

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());
}
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 3 with CountRequest

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

Example 4 with CountRequest

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());
    }
}
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 5 with CountRequest

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());
}
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)

Aggregations

CountRequest (org.opensearch.client.core.CountRequest)11 CountResponse (org.opensearch.client.core.CountResponse)8 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)6 MatchQueryBuilder (org.opensearch.index.query.MatchQueryBuilder)4 DocWriteRequest (org.opensearch.action.DocWriteRequest)3 DeleteStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)3 GetStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)3 PutStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest)3 BulkRequest (org.opensearch.action.bulk.BulkRequest)3 DeleteRequest (org.opensearch.action.delete.DeleteRequest)3 ExplainRequest (org.opensearch.action.explain.ExplainRequest)3 FieldCapabilitiesRequest (org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)3 GetRequest (org.opensearch.action.get.GetRequest)3 MultiGetRequest (org.opensearch.action.get.MultiGetRequest)3 IndexRequest (org.opensearch.action.index.IndexRequest)3 ClearScrollRequest (org.opensearch.action.search.ClearScrollRequest)3 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)3 SearchRequest (org.opensearch.action.search.SearchRequest)3 SearchScrollRequest (org.opensearch.action.search.SearchScrollRequest)3 WriteRequest (org.opensearch.action.support.WriteRequest)3