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