diff --git a/src/main/java/org/elasticsearch/action/explain/ExplainRequest.java b/src/main/java/org/elasticsearch/action/explain/ExplainRequest.java index d78037dee74..d14c6124812 100644 --- a/src/main/java/org/elasticsearch/action/explain/ExplainRequest.java +++ b/src/main/java/org/elasticsearch/action/explain/ExplainRequest.java @@ -19,11 +19,11 @@ package org.elasticsearch.action.explain; +import org.elasticsearch.Version; import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ValidateActions; import org.elasticsearch.action.support.single.shard.SingleShardOperationRequest; import org.elasticsearch.client.Requests; -import org.elasticsearch.common.Nullable; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.io.stream.StreamInput; @@ -51,6 +51,8 @@ public class ExplainRequest extends SingleShardOperationRequest private String[] filteringAlias = Strings.EMPTY_ARRAY; + long nowInMillis; + ExplainRequest() { } @@ -196,6 +198,12 @@ public class ExplainRequest extends SingleShardOperationRequest } fetchSourceContext = FetchSourceContext.optionalReadFromStream(in); + + if (in.getVersion().onOrAfter(Version.V_0_90_6)) { + nowInMillis = in.readVLong(); + } else { + nowInMillis = System.currentTimeMillis(); + } } @Override @@ -215,5 +223,9 @@ public class ExplainRequest extends SingleShardOperationRequest } FetchSourceContext.optionalWriteToStream(fetchSourceContext, out); + + if (out.getVersion().onOrAfter(Version.V_0_90_6)) { + out.writeVLong(nowInMillis); + } } } diff --git a/src/main/java/org/elasticsearch/action/explain/TransportExplainAction.java b/src/main/java/org/elasticsearch/action/explain/TransportExplainAction.java index d269abe4d31..6732bff5c90 100644 --- a/src/main/java/org/elasticsearch/action/explain/TransportExplainAction.java +++ b/src/main/java/org/elasticsearch/action/explain/TransportExplainAction.java @@ -22,6 +22,7 @@ package org.elasticsearch.action.explain; import org.apache.lucene.index.Term; import org.apache.lucene.search.Explanation; import org.elasticsearch.ElasticSearchException; +import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction; import org.elasticsearch.cache.recycler.CacheRecycler; import org.elasticsearch.cluster.ClusterService; @@ -75,6 +76,12 @@ public class TransportExplainAction extends TransportShardSingleOperationAction< this.cacheRecycler = cacheRecycler; } + @Override + protected void doExecute(ExplainRequest request, ActionListener listener) { + request.nowInMillis = System.currentTimeMillis(); + super.doExecute(request, listener); + } + protected String transportAction() { return ExplainAction.NAME; } @@ -102,7 +109,8 @@ public class TransportExplainAction extends TransportShardSingleOperationAction< SearchContext context = new DefaultSearchContext( 0, new ShardSearchRequest().types(new String[]{request.type()}) - .filteringAliases(request.filteringAlias()), + .filteringAliases(request.filteringAlias()) + .nowInMillis(request.nowInMillis), null, result.searcher(), indexService, indexShard, scriptService, cacheRecycler ); diff --git a/src/test/java/org/elasticsearch/explain/ExplainActionTests.java b/src/test/java/org/elasticsearch/explain/ExplainActionTests.java index 619b384eaf1..e8eb118f545 100644 --- a/src/test/java/org/elasticsearch/explain/ExplainActionTests.java +++ b/src/test/java/org/elasticsearch/explain/ExplainActionTests.java @@ -24,11 +24,15 @@ import org.elasticsearch.common.settings.ImmutableSettings; import org.elasticsearch.index.query.FilterBuilders; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.test.AbstractIntegrationTest; +import org.joda.time.DateTime; +import org.joda.time.DateTimeZone; +import org.joda.time.format.ISODateTimeFormat; import org.junit.Test; import java.util.Map; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; +import static org.elasticsearch.index.query.QueryBuilders.queryString; import static org.hamcrest.Matchers.equalTo; /** @@ -218,4 +222,19 @@ public class ExplainActionTests extends AbstractIntegrationTest { assertFalse(response.isMatch()); } + @Test + public void explainDateRangeInQueryString() { + client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).get(); + + 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(queryString("past:[now-2M/d TO now/d]")).get(); + assertThat(explainResponse.isExists(), equalTo(true)); + assertThat(explainResponse.isMatch(), equalTo(true)); + } }