Search in sources :

Example 26 with LatchedActionListener

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

the class SearchDocumentationIT method testExplain.

public void testExplain() throws Exception {
    indexSearchTestData();
    RestHighLevelClient client = highLevelClient();
    // tag::explain-request
    ExplainRequest request = new ExplainRequest("contributors", "1");
    request.query(QueryBuilders.termQuery("user", "quuz"));
    // end::explain-request
    // tag::explain-request-routing
    // <1>
    request.routing("routing");
    // end::explain-request-routing
    // tag::explain-request-preference
    // <1>
    request.preference("_local");
    // end::explain-request-preference
    // tag::explain-request-source
    // <1>
    request.fetchSourceContext(new FetchSourceContext(true, new String[] { "user" }, null));
    // end::explain-request-source
    // tag::explain-request-stored-field
    // <1>
    request.storedFields(new String[] { "user" });
    // end::explain-request-stored-field
    // tag::explain-execute
    ExplainResponse response = client.explain(request, RequestOptions.DEFAULT);
    // end::explain-execute
    // tag::explain-response
    // <1>
    String index = response.getIndex();
    // <2>
    String id = response.getId();
    // <3>
    boolean exists = response.isExists();
    // <4>
    boolean match = response.isMatch();
    // <5>
    boolean hasExplanation = response.hasExplanation();
    // <6>
    Explanation explanation = response.getExplanation();
    // <7>
    GetResult getResult = response.getGetResult();
    // end::explain-response
    assertThat(index, equalTo("contributors"));
    assertThat(id, equalTo("1"));
    assertTrue(exists);
    assertTrue(match);
    assertTrue(hasExplanation);
    assertNotNull(explanation);
    assertNotNull(getResult);
    // tag::get-result
    // <1>
    Map<String, Object> source = getResult.getSource();
    // <2>
    Map<String, DocumentField> fields = getResult.getFields();
    // end::get-result
    assertThat(source, equalTo(Collections.singletonMap("user", "quuz")));
    assertThat(fields.get("user").getValue(), equalTo("quuz"));
    // tag::explain-execute-listener
    ActionListener<ExplainResponse> listener = new ActionListener<ExplainResponse>() {

        @Override
        public void onResponse(ExplainResponse explainResponse) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::explain-execute-listener
    CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::explain-execute-async
    // <1>
    client.explainAsync(request, RequestOptions.DEFAULT, listener);
    // end::explain-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : GetResult(org.opensearch.index.get.GetResult) DocumentField(org.opensearch.common.document.DocumentField) Explanation(org.apache.lucene.search.Explanation) ExplainResponse(org.opensearch.action.explain.ExplainResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) ExplainRequest(org.opensearch.action.explain.ExplainRequest) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener)

Example 27 with LatchedActionListener

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

the class SearchDocumentationIT method testSearchTemplateWithStoredScript.

public void testSearchTemplateWithStoredScript() throws Exception {
    indexSearchTestData();
    RestHighLevelClient client = highLevelClient();
    RestClient restClient = client();
    registerQueryScript(restClient);
    // tag::search-template-request-stored
    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", "opensearch");
    params.put("size", 5);
    request.setScriptParams(params);
    // end::search-template-request-stored
    // tag::search-template-request-options
    request.setExplain(true);
    request.setProfile(true);
    // end::search-template-request-options
    // tag::search-template-execute
    SearchTemplateResponse response = client.searchTemplate(request, RequestOptions.DEFAULT);
    // end::search-template-execute
    SearchResponse searchResponse = response.getResponse();
    assertNotNull(searchResponse);
    assertTrue(searchResponse.getHits().getTotalHits().value > 0);
    // tag::search-template-execute-listener
    ActionListener<SearchTemplateResponse> listener = new ActionListener<SearchTemplateResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::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::search-template-execute-async
    // <1>
    client.searchTemplateAsync(request, RequestOptions.DEFAULT, listener);
    // end::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) 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) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) SearchTemplateResponse(org.opensearch.script.mustache.SearchTemplateResponse) MultiSearchTemplateResponse(org.opensearch.script.mustache.MultiSearchTemplateResponse)

Example 28 with LatchedActionListener

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

the class StoredScriptsDocumentationIT method testPutScript.

public void testPutScript() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        // tag::put-stored-script-request
        PutStoredScriptRequest request = new PutStoredScriptRequest();
        // <1>
        request.id("id");
        request.content(new BytesArray("{\n" + "\"script\": {\n" + "\"lang\": \"painless\",\n" + "\"source\": \"Math.log(_score * 2) + params.multiplier\"" + "}\n" + "}\n"), // <2>
        XContentType.JSON);
        // end::put-stored-script-request
        // tag::put-stored-script-context
        // <1>
        request.context("context");
        // end::put-stored-script-context
        // tag::put-stored-script-timeout
        // <1>
        request.timeout(TimeValue.timeValueMinutes(2));
        // <2>
        request.timeout("2m");
        // end::put-stored-script-timeout
        // tag::put-stored-script-masterTimeout
        // <1>
        request.masterNodeTimeout(TimeValue.timeValueMinutes(1));
        // <2>
        request.masterNodeTimeout("1m");
    // end::put-stored-script-masterTimeout
    }
    {
        PutStoredScriptRequest request = new PutStoredScriptRequest();
        request.id("id");
        // tag::put-stored-script-content-painless
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.startObject("script");
            {
                builder.field("lang", "painless");
                builder.field("source", "Math.log(_score * 2) + params.multiplier");
            }
            builder.endObject();
        }
        builder.endObject();
        // <1>
        request.content(BytesReference.bytes(builder), XContentType.JSON);
        // end::put-stored-script-content-painless
        // tag::put-stored-script-execute
        AcknowledgedResponse putStoredScriptResponse = client.putScript(request, RequestOptions.DEFAULT);
        // end::put-stored-script-execute
        // tag::put-stored-script-response
        // <1>
        boolean acknowledged = putStoredScriptResponse.isAcknowledged();
        // end::put-stored-script-response
        assertTrue(acknowledged);
        // tag::put-stored-script-execute-listener
        ActionListener<AcknowledgedResponse> listener = new ActionListener<AcknowledgedResponse>() {

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

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::put-stored-script-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::put-stored-script-execute-async
        // <1>
        client.putScriptAsync(request, RequestOptions.DEFAULT, listener);
        // end::put-stored-script-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
    {
        PutStoredScriptRequest request = new PutStoredScriptRequest();
        request.id("id");
        // tag::put-stored-script-content-mustache
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder.startObject();
        {
            builder.startObject("script");
            {
                builder.field("lang", "mustache");
                builder.field("source", "{\"query\":{\"match\":{\"title\":\"{{query_string}}\"}}}");
            }
            builder.endObject();
        }
        builder.endObject();
        // <1>
        request.content(BytesReference.bytes(builder), XContentType.JSON);
        // end::put-stored-script-content-mustache
        client.putScript(request, RequestOptions.DEFAULT);
        Map<String, Object> script = getAsMap("/_scripts/id");
        assertThat(extractValue("script.lang", script), equalTo("mustache"));
        assertThat(extractValue("script.source", script), equalTo("{\"query\":{\"match\":{\"title\":\"{{query_string}}\"}}}"));
    }
}
Also used : LatchedActionListener(org.opensearch.action.LatchedActionListener) BytesArray(org.opensearch.common.bytes.BytesArray) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) Map(java.util.Map) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) IOException(java.io.IOException)

Example 29 with LatchedActionListener

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

the class SearchDocumentationIT method testFieldCaps.

public void testFieldCaps() throws Exception {
    indexSearchTestData();
    RestHighLevelClient client = highLevelClient();
    // tag::field-caps-request
    FieldCapabilitiesRequest request = new FieldCapabilitiesRequest().fields("user").indices("posts", "authors", "contributors");
    // end::field-caps-request
    // tag::field-caps-request-indicesOptions
    // <1>
    request.indicesOptions(IndicesOptions.lenientExpandOpen());
    // end::field-caps-request-indicesOptions
    // tag::field-caps-execute
    FieldCapabilitiesResponse response = client.fieldCaps(request, RequestOptions.DEFAULT);
    // end::field-caps-execute
    // tag::field-caps-response
    // <1>
    Map<String, FieldCapabilities> userResponse = response.getField("user");
    FieldCapabilities textCapabilities = userResponse.get("keyword");
    boolean isSearchable = textCapabilities.isSearchable();
    boolean isAggregatable = textCapabilities.isAggregatable();
    // <2>
    String[] indices = textCapabilities.indices();
    // <3>
    String[] nonSearchableIndices = textCapabilities.nonSearchableIndices();
    // <4>
    String[] nonAggregatableIndices = textCapabilities.nonAggregatableIndices();
    // end::field-caps-response
    assertThat(userResponse.keySet(), containsInAnyOrder("keyword", "text"));
    assertTrue(isSearchable);
    assertFalse(isAggregatable);
    assertArrayEquals(indices, new String[] { "authors", "contributors" });
    assertNull(nonSearchableIndices);
    assertArrayEquals(nonAggregatableIndices, new String[] { "authors" });
    // tag::field-caps-execute-listener
    ActionListener<FieldCapabilitiesResponse> listener = new ActionListener<FieldCapabilitiesResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::field-caps-execute-listener
    // Replace the empty listener by a blocking listener for tests.
    CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::field-caps-execute-async
    // <1>
    client.fieldCapsAsync(request, RequestOptions.DEFAULT, listener);
    // end::field-caps-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : FieldCapabilities(org.opensearch.action.fieldcaps.FieldCapabilities) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) FieldCapabilitiesResponse(org.opensearch.action.fieldcaps.FieldCapabilitiesResponse) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)

Example 30 with LatchedActionListener

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

the class ClusterClientDocumentationIT method testGetComponentTemplates.

public void testGetComponentTemplates() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        Template template = new Template(Settings.builder().put("index.number_of_replicas", 3).build(), null, null);
        ComponentTemplate componentTemplate = new ComponentTemplate(template, null, null);
        PutComponentTemplateRequest putComponentTemplateRequest = new PutComponentTemplateRequest().name("ct1").componentTemplate(componentTemplate);
        client.cluster().putComponentTemplate(putComponentTemplateRequest, RequestOptions.DEFAULT);
        assertTrue(client.cluster().putComponentTemplate(putComponentTemplateRequest, RequestOptions.DEFAULT).isAcknowledged());
    }
    // tag::get-component-templates-request
    // <1>
    GetComponentTemplatesRequest request = new GetComponentTemplatesRequest("ct1");
    // end::get-component-templates-request
    // tag::get-component-templates-request-masterTimeout
    // <1>
    request.setMasterNodeTimeout(TimeValue.timeValueMinutes(1));
    // <2>
    request.setMasterNodeTimeout("1m");
    // end::get-component-templates-request-masterTimeout
    // tag::get-component-templates-execute
    GetComponentTemplatesResponse getTemplatesResponse = client.cluster().getComponentTemplate(request, RequestOptions.DEFAULT);
    // end::get-component-templates-execute
    // tag::get-component-templates-response
    // <1>
    Map<String, ComponentTemplate> templates = getTemplatesResponse.getComponentTemplates();
    // end::get-component-templates-response
    assertThat(templates.size(), is(1));
    assertThat(templates.get("ct1"), is(notNullValue()));
    // tag::get-component-templates-execute-listener
    ActionListener<GetComponentTemplatesResponse> listener = new ActionListener<GetComponentTemplatesResponse>() {

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

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::get-component-templates-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::get-component-templates-execute-async
    // <1>
    client.cluster().getComponentTemplateAsync(request, RequestOptions.DEFAULT, listener);
    // end::get-component-templates-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : GetComponentTemplatesRequest(org.opensearch.client.indices.GetComponentTemplatesRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) Template(org.opensearch.cluster.metadata.Template) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) PutComponentTemplateRequest(org.opensearch.client.indices.PutComponentTemplateRequest) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) ComponentTemplate(org.opensearch.cluster.metadata.ComponentTemplate) GetComponentTemplatesResponse(org.opensearch.client.indices.GetComponentTemplatesResponse)

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