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