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