Search in sources :

Example 6 with ExplainResponse

use of org.elasticsearch.action.explain.ExplainResponse in project elasticsearch by elastic.

the class ExplainActionIT method testExplainWitSource.

@SuppressWarnings("unchecked")
public void testExplainWitSource() throws Exception {
    assertAcked(prepareCreate("test").addAlias(new Alias("alias")));
    ensureGreen("test");
    client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject().startObject("obj1").field("field1", "value1").field("field2", "value2").endObject().endObject()).get();
    refresh();
    ExplainResponse response = client().prepareExplain(indexOrAlias(), "test", "1").setQuery(QueryBuilders.matchAllQuery()).setFetchSource("obj1.field1", null).get();
    assertNotNull(response);
    assertTrue(response.isMatch());
    assertNotNull(response.getExplanation());
    assertTrue(response.getExplanation().isMatch());
    assertThat(response.getExplanation().getValue(), equalTo(1.0f));
    assertThat(response.getGetResult().isExists(), equalTo(true));
    assertThat(response.getGetResult().getId(), equalTo("1"));
    assertThat(response.getGetResult().getSource().size(), equalTo(1));
    assertThat(((Map<String, Object>) response.getGetResult().getSource().get("obj1")).get("field1").toString(), equalTo("value1"));
    response = client().prepareExplain(indexOrAlias(), "test", "1").setQuery(QueryBuilders.matchAllQuery()).setFetchSource(null, "obj1.field2").get();
    assertNotNull(response);
    assertTrue(response.isMatch());
    assertThat(((Map<String, Object>) response.getGetResult().getSource().get("obj1")).get("field1").toString(), equalTo("value1"));
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) ExplainResponse(org.elasticsearch.action.explain.ExplainResponse) Map(java.util.Map)

Example 7 with ExplainResponse

use of org.elasticsearch.action.explain.ExplainResponse in project elasticsearch by elastic.

the class ExplainActionIT method testExplainWithFields.

public void testExplainWithFields() throws Exception {
    assertAcked(prepareCreate("test").addMapping("test", "obj1.field1", "type=keyword,store=true", "obj1.field2", "type=keyword,store=true").addAlias(new Alias("alias")));
    ensureGreen("test");
    client().prepareIndex("test", "test", "1").setSource(jsonBuilder().startObject().startObject("obj1").field("field1", "value1").field("field2", "value2").endObject().endObject()).get();
    refresh();
    ExplainResponse response = client().prepareExplain(indexOrAlias(), "test", "1").setQuery(QueryBuilders.matchAllQuery()).setStoredFields("obj1.field1").get();
    assertNotNull(response);
    assertTrue(response.isMatch());
    assertNotNull(response.getExplanation());
    assertTrue(response.getExplanation().isMatch());
    assertThat(response.getExplanation().getValue(), equalTo(1.0f));
    assertThat(response.getGetResult().isExists(), equalTo(true));
    assertThat(response.getGetResult().getId(), equalTo("1"));
    Set<String> fields = new HashSet<>(response.getGetResult().getFields().keySet());
    assertThat(fields, equalTo(singleton("obj1.field1")));
    assertThat(response.getGetResult().getFields().get("obj1.field1").getValue().toString(), equalTo("value1"));
    assertThat(response.getGetResult().isSourceEmpty(), equalTo(true));
    refresh();
    response = client().prepareExplain(indexOrAlias(), "test", "1").setQuery(QueryBuilders.matchAllQuery()).setStoredFields("obj1.field1").setFetchSource(true).get();
    assertNotNull(response);
    assertTrue(response.isMatch());
    assertNotNull(response.getExplanation());
    assertTrue(response.getExplanation().isMatch());
    assertThat(response.getExplanation().getValue(), equalTo(1.0f));
    assertThat(response.getGetResult().isExists(), equalTo(true));
    assertThat(response.getGetResult().getId(), equalTo("1"));
    fields = new HashSet<>(response.getGetResult().getFields().keySet());
    assertThat(fields, equalTo(singleton("obj1.field1")));
    assertThat(response.getGetResult().getFields().get("obj1.field1").getValue().toString(), equalTo("value1"));
    assertThat(response.getGetResult().isSourceEmpty(), equalTo(false));
    response = client().prepareExplain(indexOrAlias(), "test", "1").setQuery(QueryBuilders.matchAllQuery()).setStoredFields("obj1.field1", "obj1.field2").get();
    assertNotNull(response);
    assertTrue(response.isMatch());
    String v1 = (String) response.getGetResult().field("obj1.field1").getValue();
    String v2 = (String) response.getGetResult().field("obj1.field2").getValue();
    assertThat(v1, equalTo("value1"));
    assertThat(v2, equalTo("value2"));
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) ExplainResponse(org.elasticsearch.action.explain.ExplainResponse) HashSet(java.util.HashSet)

Example 8 with ExplainResponse

use of org.elasticsearch.action.explain.ExplainResponse in project elasticsearch by elastic.

the class ExplainActionIT method testExplainDateRangeInQueryString.

public void testExplainDateRangeInQueryString() {
    createIndex("test");
    String aMonthAgo = ISODateTimeFormat.yearMonthDay().print(new DateTime(DateTimeZone.UTC).minusMonths(1));
    String aMonthFromNow = ISODateTimeFormat.yearMonthDay().print(new DateTime(DateTimeZone.UTC).plusMonths(1));
    client().prepareIndex("test", "type", "1").setSource("past", aMonthAgo, "future", aMonthFromNow).get();
    refresh();
    ExplainResponse explainResponse = client().prepareExplain("test", "type", "1").setQuery(queryStringQuery("past:[now-2M/d TO now/d]")).get();
    assertThat(explainResponse.isExists(), equalTo(true));
    assertThat(explainResponse.isMatch(), equalTo(true));
}
Also used : ExplainResponse(org.elasticsearch.action.explain.ExplainResponse) DateTime(org.joda.time.DateTime)

Example 9 with ExplainResponse

use of org.elasticsearch.action.explain.ExplainResponse in project elasticsearch by elastic.

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", "test", "1").setSource("field", "value1").get();
    ExplainResponse response = client().prepareExplain(indexOrAlias(), "test", "1").setQuery(QueryBuilders.matchAllQuery()).get();
    assertNotNull(response);
    // not a match b/c not realtime
    assertFalse(response.isExists());
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getType(), equalTo("test"));
    assertThat(response.getId(), equalTo("1"));
    // not a match b/c not realtime
    assertFalse(response.isMatch());
    refresh();
    response = client().prepareExplain(indexOrAlias(), "test", "1").setQuery(QueryBuilders.matchAllQuery()).get();
    assertNotNull(response);
    assertTrue(response.isMatch());
    assertNotNull(response.getExplanation());
    assertTrue(response.getExplanation().isMatch());
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getType(), equalTo("test"));
    assertThat(response.getId(), equalTo("1"));
    assertThat(response.getExplanation().getValue(), equalTo(1.0f));
    response = client().prepareExplain(indexOrAlias(), "test", "1").setQuery(QueryBuilders.termQuery("field", "value2")).get();
    assertNotNull(response);
    assertTrue(response.isExists());
    assertFalse(response.isMatch());
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getType(), equalTo("test"));
    assertThat(response.getId(), equalTo("1"));
    assertNotNull(response.getExplanation());
    assertFalse(response.getExplanation().isMatch());
    response = client().prepareExplain(indexOrAlias(), "test", "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.getType(), 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(), "test", "2").setQuery(QueryBuilders.matchAllQuery()).get();
    assertNotNull(response);
    assertFalse(response.isExists());
    assertFalse(response.isMatch());
    assertThat(response.getIndex(), equalTo("test"));
    assertThat(response.getType(), equalTo("test"));
    assertThat(response.getId(), equalTo("2"));
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) ExplainResponse(org.elasticsearch.action.explain.ExplainResponse)

Example 10 with ExplainResponse

use of org.elasticsearch.action.explain.ExplainResponse in project elasticsearch by elastic.

the class ChildQuerySearchIT method testExplainUsage.

public void testExplainUsage() throws Exception {
    assertAcked(prepareCreate("test").addMapping("parent").addMapping("child", "_parent", "type=parent"));
    ensureGreen();
    String parentId = "p1";
    client().prepareIndex("test", "parent", parentId).setSource("p_field", "1").get();
    client().prepareIndex("test", "child", "c1").setSource("c_field", "1").setParent(parentId).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", "parent", parentId).setQuery(hasChildQuery("child", termQuery("c_field", "1"), ScoreMode.Max)).get();
    assertThat(explainResponse.isExists(), equalTo(true));
    assertThat(explainResponse.getExplanation().getDetails()[0].getDescription(), containsString("join value p1"));
}
Also used : ExplainResponse(org.elasticsearch.action.explain.ExplainResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

ExplainResponse (org.elasticsearch.action.explain.ExplainResponse)10 Alias (org.elasticsearch.action.admin.indices.alias.Alias)6 Map (java.util.Map)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 IOException (java.io.IOException)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 Explanation (org.apache.lucene.search.Explanation)1 RoutingMissingException (org.elasticsearch.action.RoutingMissingException)1 ExplainRequest (org.elasticsearch.action.explain.ExplainRequest)1 MultiGetRequest (org.elasticsearch.action.get.MultiGetRequest)1 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)1 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)1 MultiTermVectorsResponse (org.elasticsearch.action.termvectors.MultiTermVectorsResponse)1 TermVectorsRequest (org.elasticsearch.action.termvectors.TermVectorsRequest)1