Search in sources :

Example 21 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class IndicesClientDocumentationIT method testValidateQuery.

@SuppressWarnings("unused")
public void testValidateQuery() throws IOException, InterruptedException {
    RestHighLevelClient client = highLevelClient();
    String index = "some_index";
    createIndex(index, Settings.EMPTY);
    // tag::indices-validate-query-request
    // <1>
    ValidateQueryRequest request = new ValidateQueryRequest(index);
    // end::indices-validate-query-request
    // tag::indices-validate-query-request-query
    QueryBuilder builder = QueryBuilders.boolQuery().must(QueryBuilders.queryStringQuery("*:*")).filter(QueryBuilders.termQuery("user", "foobar"));
    // <2>
    request.query(builder);
    // end::indices-validate-query-request-query
    // tag::indices-validate-query-request-explain
    // <1>
    request.explain(true);
    // end::indices-validate-query-request-explain
    // tag::indices-validate-query-request-allShards
    // <1>
    request.allShards(true);
    // end::indices-validate-query-request-allShards
    // tag::indices-validate-query-request-rewrite
    // <1>
    request.rewrite(true);
    // end::indices-validate-query-request-rewrite
    // tag::indices-validate-query-execute
    // <1>
    ValidateQueryResponse response = client.indices().validateQuery(request, RequestOptions.DEFAULT);
    // end::indices-validate-query-execute
    // tag::indices-validate-query-response
    // <1>
    boolean isValid = response.isValid();
    // <2>
    int totalShards = response.getTotalShards();
    // <3>
    int successfulShards = response.getSuccessfulShards();
    // <4>
    int failedShards = response.getFailedShards();
    if (failedShards > 0) {
        for (DefaultShardOperationFailedException failure : response.getShardFailures()) {
            // <5>
            // <6>
            String failedIndex = failure.index();
            // <7>
            int shardId = failure.shardId();
            // <8>
            String reason = failure.reason();
        }
    }
    for (QueryExplanation explanation : response.getQueryExplanation()) {
        // <9>
        // <10>
        String explanationIndex = explanation.getIndex();
        // <11>
        int shardId = explanation.getShard();
        // <12>
        String explanationString = explanation.getExplanation();
    }
    // end::indices-validate-query-response
    // tag::indices-validate-query-execute-listener
    ActionListener<ValidateQueryResponse> listener = new ActionListener<ValidateQueryResponse>() {

        @Override
        public void onResponse(ValidateQueryResponse validateQueryResponse) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::indices-validate-query-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::indices-validate-query-execute-async
    // <1>
    client.indices().validateQueryAsync(request, RequestOptions.DEFAULT, listener);
    // end::indices-validate-query-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : ValidateQueryResponse(org.opensearch.action.admin.indices.validate.query.ValidateQueryResponse) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) QueryBuilder(org.opensearch.index.query.QueryBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) QueryExplanation(org.opensearch.action.admin.indices.validate.query.QueryExplanation) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener)

Example 22 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class IndicesClientDocumentationIT method testDeleteTemplate.

public void testDeleteTemplate() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
        putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
        putRequest.settings(Settings.builder().put("index.number_of_shards", 3));
        assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
    }
    // tag::delete-template-request
    DeleteIndexTemplateRequest request = new DeleteIndexTemplateRequest();
    // <1>
    request.name("my-template");
    // end::delete-template-request
    // tag::delete-template-request-masterTimeout
    // <1>
    request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.masterNodeTimeout("1m");
    // end::delete-template-request-masterTimeout
    // tag::delete-template-execute
    AcknowledgedResponse deleteTemplateAcknowledge = client.indices().deleteTemplate(request, RequestOptions.DEFAULT);
    // end::delete-template-execute
    // tag::delete-template-response
    // <1>
    boolean acknowledged = deleteTemplateAcknowledge.isAcknowledged();
    // end::delete-template-response
    assertThat(acknowledged, equalTo(true));
    {
        PutIndexTemplateRequest putRequest = new PutIndexTemplateRequest("my-template");
        putRequest.patterns(Arrays.asList("pattern-1", "log-*"));
        putRequest.settings(Settings.builder().put("index.number_of_shards", 3));
        assertTrue(client.indices().putTemplate(putRequest, RequestOptions.DEFAULT).isAcknowledged());
    }
    // tag::delete-template-execute-listener
    ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {

        @Override
        public void onResponse(AcknowledgedResponse response) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::delete-template-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::delete-template-execute-async
    // <1>
    client.indices().deleteTemplateAsync(request, RequestOptions.DEFAULT, listener);
    // end::delete-template-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException)

Example 23 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class SearchDocumentationIT method testRankEval.

public void testRankEval() throws Exception {
    indexSearchTestData();
    RestHighLevelClient client = highLevelClient();
    {
        // tag::rank-eval-request-basic
        // <1>
        EvaluationMetric metric = new PrecisionAtK();
        List<RatedDocument> ratedDocs = new ArrayList<>();
        // <2>
        ratedDocs.add(new RatedDocument("posts", "1", 1));
        SearchSourceBuilder searchQuery = new SearchSourceBuilder();
        // <3>
        searchQuery.query(QueryBuilders.matchQuery("user", "foobar"));
        // <4>
        RatedRequest ratedRequest = new RatedRequest("foobar_query", ratedDocs, searchQuery);
        List<RatedRequest> ratedRequests = Arrays.asList(ratedRequest);
        RankEvalSpec specification = // <5>
        new RankEvalSpec(ratedRequests, metric);
        // <6>
        RankEvalRequest request = new RankEvalRequest(specification, new String[] { "posts" });
        // end::rank-eval-request-basic
        // tag::rank-eval-execute
        RankEvalResponse response = client.rankEval(request, RequestOptions.DEFAULT);
        // end::rank-eval-execute
        // tag::rank-eval-response
        // <1>
        double evaluationResult = response.getMetricScore();
        assertEquals(1.0 / 3.0, evaluationResult, 0.0);
        Map<String, EvalQueryQuality> partialResults = response.getPartialResults();
        EvalQueryQuality evalQuality = // <2>
        partialResults.get("foobar_query");
        assertEquals("foobar_query", evalQuality.getId());
        // <3>
        double qualityLevel = evalQuality.metricScore();
        assertEquals(1.0 / 3.0, qualityLevel, 0.0);
        List<RatedSearchHit> hitsAndRatings = evalQuality.getHitsAndRatings();
        RatedSearchHit ratedSearchHit = hitsAndRatings.get(2);
        // <4>
        assertEquals("3", ratedSearchHit.getSearchHit().getId());
        // <5>
        assertFalse(ratedSearchHit.getRating().isPresent());
        MetricDetail metricDetails = evalQuality.getMetricDetails();
        String metricName = metricDetails.getMetricName();
        // <6>
        assertEquals(PrecisionAtK.NAME, metricName);
        PrecisionAtK.Detail detail = (PrecisionAtK.Detail) metricDetails;
        // <7>
        assertEquals(1, detail.getRelevantRetrieved());
        assertEquals(3, detail.getRetrieved());
        // end::rank-eval-response
        // tag::rank-eval-execute-listener
        ActionListener<RankEvalResponse> listener = new ActionListener<RankEvalResponse>() {

            @Override
            public void onResponse(RankEvalResponse response) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::rank-eval-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::rank-eval-execute-async
        // <1>
        client.rankEvalAsync(request, RequestOptions.DEFAULT, listener);
        // end::rank-eval-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
}
Also used : EvaluationMetric(org.opensearch.index.rankeval.EvaluationMetric) RankEvalSpec(org.opensearch.index.rankeval.RankEvalSpec) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) RatedDocument(org.opensearch.index.rankeval.RatedDocument) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) RatedSearchHit(org.opensearch.index.rankeval.RatedSearchHit) LatchedActionListener(org.opensearch.action.LatchedActionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) PrecisionAtK(org.opensearch.index.rankeval.PrecisionAtK) RankEvalResponse(org.opensearch.index.rankeval.RankEvalResponse) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) RatedRequest(org.opensearch.index.rankeval.RatedRequest) ArrayList(java.util.ArrayList) List(java.util.List) EvalQueryQuality(org.opensearch.index.rankeval.EvalQueryQuality) MetricDetail(org.opensearch.index.rankeval.MetricDetail) Map(java.util.Map) HashMap(java.util.HashMap) MetricDetail(org.opensearch.index.rankeval.MetricDetail)

Example 24 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class SearchDocumentationIT method testMultiSearch.

public void testMultiSearch() throws Exception {
    indexSearchTestData();
    RestHighLevelClient client = highLevelClient();
    {
        // tag::multi-search-request-basic
        // <1>
        MultiSearchRequest request = new MultiSearchRequest();
        // <2>
        SearchRequest firstSearchRequest = new SearchRequest();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("user", "foobar"));
        firstSearchRequest.source(searchSourceBuilder);
        // <3>
        request.add(firstSearchRequest);
        // <4>
        SearchRequest secondSearchRequest = new SearchRequest();
        searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchQuery("user", "quxx"));
        secondSearchRequest.source(searchSourceBuilder);
        request.add(secondSearchRequest);
        // end::multi-search-request-basic
        // tag::multi-search-execute
        MultiSearchResponse response = client.msearch(request, RequestOptions.DEFAULT);
        // end::multi-search-execute
        // tag::multi-search-response
        // <1>
        MultiSearchResponse.Item firstResponse = response.getResponses()[0];
        // <2>
        assertNull(firstResponse.getFailure());
        // <3>
        SearchResponse searchResponse = firstResponse.getResponse();
        assertEquals(4, searchResponse.getHits().getTotalHits().value);
        // <4>
        MultiSearchResponse.Item secondResponse = response.getResponses()[1];
        assertNull(secondResponse.getFailure());
        searchResponse = secondResponse.getResponse();
        assertEquals(1, searchResponse.getHits().getTotalHits().value);
        // end::multi-search-response
        // tag::multi-search-execute-listener
        ActionListener<MultiSearchResponse> listener = new ActionListener<MultiSearchResponse>() {

            @Override
            public void onResponse(MultiSearchResponse response) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::multi-search-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::multi-search-execute-async
        // <1>
        client.msearchAsync(request, RequestOptions.DEFAULT, listener);
        // end::multi-search-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) Item(org.opensearch.script.mustache.MultiSearchTemplateResponse.Item) LatchedActionListener(org.opensearch.action.LatchedActionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest)

Example 25 with LatchedActionListener

use of org.opensearch.action.LatchedActionListener in project OpenSearch by opensearch-project.

the class SearchDocumentationIT method testMultiSearchTemplateWithStoredScript.

public void testMultiSearchTemplateWithStoredScript() throws Exception {
    indexSearchTestData();
    RestHighLevelClient client = highLevelClient();
    RestClient restClient = client();
    registerQueryScript(restClient);
    // tag::multi-search-template-request-stored
    MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();
    String[] searchTerms = { "opensearch", "dashboards" };
    for (String searchTerm : searchTerms) {
        SearchTemplateRequest request = new SearchTemplateRequest();
        request.setRequest(new SearchRequest("posts"));
        request.setScriptType(ScriptType.STORED);
        request.setScript("title_search");
        Map<String, Object> params = new HashMap<>();
        params.put("field", "title");
        params.put("value", searchTerm);
        params.put("size", 5);
        request.setScriptParams(params);
        multiRequest.add(request);
    }
    // end::multi-search-template-request-stored
    // tag::multi-search-template-execute
    MultiSearchTemplateResponse multiResponse = client.msearchTemplate(multiRequest, RequestOptions.DEFAULT);
    // end::multi-search-template-execute
    assertNotNull(multiResponse);
    assertEquals(searchTerms.length, multiResponse.getResponses().length);
    assertNotNull(multiResponse.getResponses()[0]);
    SearchResponse searchResponse = multiResponse.getResponses()[0].getResponse().getResponse();
    assertTrue(searchResponse.getHits().getTotalHits().value > 0);
    // tag::multi-search-template-execute-listener
    ActionListener<MultiSearchTemplateResponse> listener = new ActionListener<MultiSearchTemplateResponse>() {

        @Override
        public void onResponse(MultiSearchTemplateResponse response) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::multi-search-template-execute-listener
    // Replace the empty listener by a blocking listener for tests.
    CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::multi-search-template-execute-async
    client.msearchTemplateAsync(multiRequest, RequestOptions.DEFAULT, listener);
    // end::multi-search-template-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) HashMap(java.util.HashMap) RestClient(org.opensearch.client.RestClient) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)63 LatchedActionListener (org.opensearch.action.LatchedActionListener)63 ActionListener (org.opensearch.action.ActionListener)57 IOException (java.io.IOException)43 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)38 OpenSearchException (org.opensearch.OpenSearchException)24 HashMap (java.util.HashMap)20 Map (java.util.Map)19 DefaultShardOperationFailedException (org.opensearch.action.support.DefaultShardOperationFailedException)18 ArrayList (java.util.ArrayList)17 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)16 Matchers.containsString (org.hamcrest.Matchers.containsString)15 Settings (org.opensearch.common.settings.Settings)15 TestThreadPool (org.opensearch.threadpool.TestThreadPool)14 List (java.util.List)12 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)12 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)11 CreateIndexResponse (org.opensearch.client.indices.CreateIndexResponse)11 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)10 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)10