use of org.opensearch.client.RestHighLevelClient 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));
}
use of org.opensearch.client.RestHighLevelClient 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));
}
use of org.opensearch.client.RestHighLevelClient 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));
}
}
use of org.opensearch.client.RestHighLevelClient 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));
}
}
use of org.opensearch.client.RestHighLevelClient in project OpenSearch by opensearch-project.
the class SearchDocumentationIT method testSearchRequestSuggestions.
@SuppressWarnings({ "unused", "rawtypes" })
public void testSearchRequestSuggestions() throws IOException {
RestHighLevelClient client = highLevelClient();
{
BulkRequest request = new BulkRequest();
request.add(new IndexRequest("posts").id("1").source(XContentType.JSON, "user", "foobar"));
request.add(new IndexRequest("posts").id("2").source(XContentType.JSON, "user", "quxx"));
request.add(new IndexRequest("posts").id("3").source(XContentType.JSON, "user", "quzz"));
request.add(new IndexRequest("posts").id("4").source(XContentType.JSON, "user", "corge"));
request.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE);
BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
assertSame(RestStatus.OK, bulkResponse.status());
assertFalse(bulkResponse.hasFailures());
}
{
SearchRequest searchRequest = new SearchRequest();
// tag::search-request-suggestion
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
SuggestionBuilder termSuggestionBuilder = // <1>
SuggestBuilders.termSuggestion("user").text("fooarb");
SuggestBuilder suggestBuilder = new SuggestBuilder();
// <2>
suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);
searchSourceBuilder.suggest(suggestBuilder);
// end::search-request-suggestion
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
{
// tag::search-request-suggestion-get
// <1>
Suggest suggest = searchResponse.getSuggest();
// <2>
TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");
for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
// <3>
for (TermSuggestion.Entry.Option option : entry) {
// <4>
String suggestText = option.getText().string();
}
}
// end::search-request-suggestion-get
assertEquals(1, termSuggestion.getEntries().size());
assertEquals(1, termSuggestion.getEntries().get(0).getOptions().size());
assertEquals("foobar", termSuggestion.getEntries().get(0).getOptions().get(0).getText().string());
}
}
}
Aggregations