use of org.opensearch.action.explain.ExplainResponse in project OpenSearch by opensearch-project.
the class ExplainActionIT method testSimple.
public void testSimple() throws Exception {
assertAcked(prepareCreate("test").addAlias(new Alias("alias")).setSettings(Settings.builder().put("index.refresh_interval", -1)));
ensureGreen("test");
client().prepareIndex("test").setId("1").setSource("field", "value1").get();
ExplainResponse response = client().prepareExplain(indexOrAlias(), "1").setQuery(QueryBuilders.matchAllQuery()).get();
assertNotNull(response);
// not a match b/c not realtime
assertFalse(response.isExists());
assertThat(response.getIndex(), equalTo("test"));
assertThat(response.getId(), equalTo("1"));
// not a match b/c not realtime
assertFalse(response.isMatch());
refresh();
response = client().prepareExplain(indexOrAlias(), "1").setQuery(QueryBuilders.matchAllQuery()).get();
assertNotNull(response);
assertTrue(response.isMatch());
assertNotNull(response.getExplanation());
assertTrue(response.getExplanation().isMatch());
assertThat(response.getIndex(), equalTo("test"));
assertThat(response.getId(), equalTo("1"));
assertThat(response.getExplanation().getValue(), equalTo(1.0f));
response = client().prepareExplain(indexOrAlias(), "1").setQuery(QueryBuilders.termQuery("field", "value2")).get();
assertNotNull(response);
assertTrue(response.isExists());
assertFalse(response.isMatch());
assertThat(response.getIndex(), equalTo("test"));
assertThat(response.getId(), equalTo("1"));
assertNotNull(response.getExplanation());
assertFalse(response.getExplanation().isMatch());
response = client().prepareExplain(indexOrAlias(), "1").setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("field", "value1")).must(QueryBuilders.termQuery("field", "value2"))).get();
assertNotNull(response);
assertTrue(response.isExists());
assertFalse(response.isMatch());
assertThat(response.getIndex(), equalTo("test"));
assertThat(response.getId(), equalTo("1"));
assertNotNull(response.getExplanation());
assertFalse(response.getExplanation().isMatch());
assertThat(response.getExplanation().getDetails().length, equalTo(2));
response = client().prepareExplain(indexOrAlias(), "2").setQuery(QueryBuilders.matchAllQuery()).get();
assertNotNull(response);
assertFalse(response.isExists());
assertFalse(response.isMatch());
assertThat(response.getIndex(), equalTo("test"));
assertThat(response.getId(), equalTo("2"));
}
use of org.opensearch.action.explain.ExplainResponse in project OpenSearch by opensearch-project.
the class ExplainActionIT method testExplainDateRangeInQueryString.
public void testExplainDateRangeInQueryString() {
createIndex("test");
ZonedDateTime now = ZonedDateTime.now(ZoneOffset.UTC);
String aMonthAgo = DateTimeFormatter.ISO_LOCAL_DATE.format(now.minusMonths(1));
String aMonthFromNow = DateTimeFormatter.ISO_LOCAL_DATE.format(now.plusMonths(1));
client().prepareIndex("test").setId("1").setSource("past", aMonthAgo, "future", aMonthFromNow).get();
refresh();
ExplainResponse explainResponse = client().prepareExplain("test", "1").setQuery(queryStringQuery("past:[now-2M/d TO now/d]")).get();
assertThat(explainResponse.isExists(), equalTo(true));
assertThat(explainResponse.isMatch(), equalTo(true));
}
use of org.opensearch.action.explain.ExplainResponse in project OpenSearch by opensearch-project.
the class ExplainActionIT method testExplainWithFilteredAlias.
public void testExplainWithFilteredAlias() {
assertAcked(prepareCreate("test").setMapping("field2", "type=text").addAlias(new Alias("alias1").filter(QueryBuilders.termQuery("field2", "value2"))));
ensureGreen("test");
client().prepareIndex("test").setId("1").setSource("field1", "value1", "field2", "value1").get();
refresh();
ExplainResponse response = client().prepareExplain("alias1", "1").setQuery(QueryBuilders.matchAllQuery()).get();
assertNotNull(response);
assertTrue(response.isExists());
assertFalse(response.isMatch());
}
use of org.opensearch.action.explain.ExplainResponse 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.explain.ExplainResponse in project fesen-httpclient by codelibs.
the class Elasticsearch7ClientTest method test_explain.
@Test
void test_explain() throws Exception {
final String index = "test_explain";
final String type = "test_type";
final String id = "1";
CountDownLatch latch = new CountDownLatch(1);
client.prepareIndex(index, type, id).setRefreshPolicy(RefreshPolicy.IMMEDIATE).setSource("{" + "\"user\":\"user_" + id + "\"," + "\"postDate\":\"2018-07-30\"," + "\"text\":\"test\"" + "}", XContentType.JSON).execute().actionGet();
client.admin().indices().prepareRefresh(index).execute().actionGet();
client.prepareExplain(index, type, id).setQuery(QueryBuilders.termQuery("text", "test")).execute(wrap(res -> {
assertTrue(res.hasExplanation());
latch.countDown();
}, e -> {
e.printStackTrace();
try {
fail();
} finally {
latch.countDown();
}
}));
latch.await();
{
ExplainResponse explainResponse = client.prepareExplain(index, type, id).setQuery(QueryBuilders.termQuery("text", "test")).execute().actionGet();
assertTrue(explainResponse.hasExplanation());
}
}
Aggregations