Search in sources :

Example 1 with AnalyzeResponse

use of org.opensearch.client.indices.AnalyzeResponse in project OpenSearch by opensearch-project.

the class IndicesClientIT method testAnalyze.

public void testAnalyze() throws Exception {
    RestHighLevelClient client = highLevelClient();
    AnalyzeRequest noindexRequest = AnalyzeRequest.withGlobalAnalyzer("english", "One two three");
    AnalyzeResponse noindexResponse = execute(noindexRequest, client.indices()::analyze, client.indices()::analyzeAsync);
    assertThat(noindexResponse.getTokens(), hasSize(3));
    AnalyzeRequest detailsRequest = AnalyzeRequest.withGlobalAnalyzer("english", "One two three").explain(true);
    AnalyzeResponse detailsResponse = execute(detailsRequest, client.indices()::analyze, client.indices()::analyzeAsync);
    assertNotNull(detailsResponse.detail());
}
Also used : AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) AnalyzeResponse(org.opensearch.client.indices.AnalyzeResponse)

Example 2 with AnalyzeResponse

use of org.opensearch.client.indices.AnalyzeResponse in project OpenSearch by opensearch-project.

the class IndicesClientDocumentationIT method testAnalyze.

public void testAnalyze() throws IOException, InterruptedException {
    RestHighLevelClient client = highLevelClient();
    {
        // tag::analyze-builtin-request
        AnalyzeRequest request = // <1>
        AnalyzeRequest.withGlobalAnalyzer(// <1>
        "english", "Some text to analyze", // <2>
        "Some more text to analyze");
    // end::analyze-builtin-request
    }
    {
        // tag::analyze-custom-request
        Map<String, Object> stopFilter = new HashMap<>();
        stopFilter.put("type", "stop");
        // <1>
        stopFilter.put("stopwords", new String[] { "to" });
        AnalyzeRequest request = // <2>
        AnalyzeRequest.buildCustomAnalyzer("standard").addCharFilter(// <3>
        "html_strip").addTokenFilter(// <4>
        "lowercase").addTokenFilter(// <5>
        stopFilter).build("<b>Some text to analyze</b>");
    // end::analyze-custom-request
    }
    {
        // tag::analyze-custom-normalizer-request
        AnalyzeRequest request = AnalyzeRequest.buildCustomNormalizer().addTokenFilter("lowercase").build("<b>BaR</b>");
        // end::analyze-custom-normalizer-request
        // tag::analyze-request-explain
        // <1>
        request.explain(true);
        // <2>
        request.attributes("keyword", "type");
        // end::analyze-request-explain
        // tag::analyze-execute
        AnalyzeResponse response = client.indices().analyze(request, RequestOptions.DEFAULT);
        // end::analyze-execute
        // tag::analyze-response-tokens
        // <1>
        List<AnalyzeResponse.AnalyzeToken> tokens = response.getTokens();
        // end::analyze-response-tokens
        // tag::analyze-response-detail
        // <1>
        DetailAnalyzeResponse detail = response.detail();
        // end::analyze-response-detail
        assertNull(tokens);
        assertNotNull(detail.tokenizer());
    }
    CreateIndexRequest req = new CreateIndexRequest("my_index");
    CreateIndexResponse resp = client.indices().create(req, RequestOptions.DEFAULT);
    assertTrue(resp.isAcknowledged());
    PutMappingRequest pmReq = new PutMappingRequest("my_index").source(XContentFactory.jsonBuilder().startObject().startObject("properties").startObject("my_field").field("type", "text").field("analyzer", "english").endObject().endObject().endObject());
    AcknowledgedResponse pmResp = client.indices().putMapping(pmReq, RequestOptions.DEFAULT);
    assertTrue(pmResp.isAcknowledged());
    {
        // tag::analyze-index-request
        AnalyzeRequest request = AnalyzeRequest.withIndexAnalyzer(// <1>
        "my_index", // <2>
        "my_analyzer", "some text to analyze");
        // end::analyze-index-request
        // tag::analyze-execute-listener
        ActionListener<AnalyzeResponse> listener = new ActionListener<AnalyzeResponse>() {

            @Override
            public void onResponse(AnalyzeResponse analyzeTokens) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::analyze-execute-listener
        // use a built-in analyzer in the test
        request = AnalyzeRequest.withField("my_index", "my_field", "some text to analyze");
        // Use a blocking listener in the test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::analyze-execute-async
        // <1>
        client.indices().analyzeAsync(request, RequestOptions.DEFAULT, listener);
        // end::analyze-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
    {
        // tag::analyze-index-normalizer-request
        AnalyzeRequest request = AnalyzeRequest.withNormalizer(// <1>
        "my_index", // <2>
        "my_normalizer", "some text to analyze");
    // end::analyze-index-normalizer-request
    }
    {
        // tag::analyze-field-request
        AnalyzeRequest request = AnalyzeRequest.withField("my_index", "my_field", "some text to analyze");
    // end::analyze-field-request
    }
}
Also used : PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) AcknowledgedResponse(org.opensearch.action.support.master.AcknowledgedResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) CountDownLatch(java.util.concurrent.CountDownLatch) DetailAnalyzeResponse(org.opensearch.client.indices.DetailAnalyzeResponse) AnalyzeResponse(org.opensearch.client.indices.AnalyzeResponse) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) LatchedActionListener(org.opensearch.action.LatchedActionListener) DetailAnalyzeResponse(org.opensearch.client.indices.DetailAnalyzeResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) List(java.util.List) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

AnalyzeRequest (org.opensearch.client.indices.AnalyzeRequest)2 AnalyzeResponse (org.opensearch.client.indices.AnalyzeResponse)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 OpenSearchException (org.opensearch.OpenSearchException)1 ActionListener (org.opensearch.action.ActionListener)1 LatchedActionListener (org.opensearch.action.LatchedActionListener)1 DefaultShardOperationFailedException (org.opensearch.action.support.DefaultShardOperationFailedException)1 AcknowledgedResponse (org.opensearch.action.support.master.AcknowledgedResponse)1 RestHighLevelClient (org.opensearch.client.RestHighLevelClient)1 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)1 CreateIndexResponse (org.opensearch.client.indices.CreateIndexResponse)1 DetailAnalyzeResponse (org.opensearch.client.indices.DetailAnalyzeResponse)1 PutMappingRequest (org.opensearch.client.indices.PutMappingRequest)1