Search in sources :

Example 1 with ExplainResponse

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));
}
Also used : GetResult(org.opensearch.index.get.GetResult) DocumentField(org.opensearch.common.document.DocumentField) Explanation(org.apache.lucene.search.Explanation) ExplainResponse(org.opensearch.action.explain.ExplainResponse) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) ExplainRequest(org.opensearch.action.explain.ExplainRequest) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener)

Example 2 with ExplainResponse

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));
}
Also used : ZonedDateTime(java.time.ZonedDateTime) ExplainResponse(org.opensearch.action.explain.ExplainResponse)

Example 3 with ExplainResponse

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"));
}
Also used : Alias(org.opensearch.action.admin.indices.alias.Alias) ExplainResponse(org.opensearch.action.explain.ExplainResponse)

Example 4 with ExplainResponse

use of org.opensearch.action.explain.ExplainResponse in project OpenSearch by opensearch-project.

the class ExplainActionIT method testExplainWithFilteredAlias.

public void testExplainWithFilteredAlias() {
    assertAcked(prepareCreate("test").addMapping("test", "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());
}
Also used : Alias(org.opensearch.action.admin.indices.alias.Alias) ExplainResponse(org.opensearch.action.explain.ExplainResponse)

Example 5 with ExplainResponse

use of org.opensearch.action.explain.ExplainResponse in project OpenSearch by opensearch-project.

the class ChildQuerySearchIT method testExplainUsage.

public void testExplainUsage() throws Exception {
    assertAcked(prepareCreate("test").addMapping("doc", buildParentJoinFieldMappingFromSimplifiedDef("join_field", true, "parent", "child")));
    ensureGreen();
    String parentId = "p1";
    createIndexRequest("test", "parent", parentId, null, "p_field", "1").get();
    createIndexRequest("test", "child", "c1", parentId, "c_field", "1").get();
    refresh();
    SearchResponse searchResponse = client().prepareSearch("test").setExplain(true).setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)).get();
    assertHitCount(searchResponse, 1L);
    assertThat(searchResponse.getHits().getAt(0).getExplanation().getDescription(), containsString("join value p1"));
    searchResponse = client().prepareSearch("test").setExplain(true).setQuery(hasParentQuery("parent", termQuery("p_field", "1"), true)).get();
    assertHitCount(searchResponse, 1L);
    assertThat(searchResponse.getHits().getAt(0).getExplanation().getDescription(), containsString("join value p1"));
    ExplainResponse explainResponse = client().prepareExplain("test", parentId).setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)).get();
    assertThat(explainResponse.isExists(), equalTo(true));
    assertThat(explainResponse.getExplanation().toString(), containsString("join value p1"));
}
Also used : ExplainResponse(org.opensearch.action.explain.ExplainResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.opensearch.action.search.SearchResponse)

Aggregations

ExplainResponse (org.opensearch.action.explain.ExplainResponse)11 Alias (org.opensearch.action.admin.indices.alias.Alias)6 Matchers.containsString (org.hamcrest.Matchers.containsString)3 Map (java.util.Map)2 ExplainRequest (org.opensearch.action.explain.ExplainRequest)2 SearchResponse (org.opensearch.action.search.SearchResponse)2 IOException (java.io.IOException)1 ZonedDateTime (java.time.ZonedDateTime)1 ArrayList (java.util.ArrayList)1 Collections.emptyMap (java.util.Collections.emptyMap)1 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Explanation (org.apache.lucene.search.Explanation)1 ActionListener (org.opensearch.action.ActionListener)1 LatchedActionListener (org.opensearch.action.LatchedActionListener)1 RoutingMissingException (org.opensearch.action.RoutingMissingException)1 MultiGetRequest (org.opensearch.action.get.MultiGetRequest)1