Set nowInMillis to search context created by the explain api so that "NOW" can be used within queries

Added nowInMillis to ExplainRequest in a backwards compatible manner

Fixes #3626
This commit is contained in:
Luca Cavanna 2013-09-15 03:24:22 +02:00
parent 0d3895533c
commit 52e9e753e6
3 changed files with 41 additions and 2 deletions

View File

@ -19,11 +19,11 @@
package org.elasticsearch.action.explain; package org.elasticsearch.action.explain;
import org.elasticsearch.Version;
import org.elasticsearch.action.ActionRequestValidationException; import org.elasticsearch.action.ActionRequestValidationException;
import org.elasticsearch.action.ValidateActions; import org.elasticsearch.action.ValidateActions;
import org.elasticsearch.action.support.single.shard.SingleShardOperationRequest; import org.elasticsearch.action.support.single.shard.SingleShardOperationRequest;
import org.elasticsearch.client.Requests; import org.elasticsearch.client.Requests;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings; import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.io.stream.StreamInput; import org.elasticsearch.common.io.stream.StreamInput;
@ -51,6 +51,8 @@ public class ExplainRequest extends SingleShardOperationRequest<ExplainRequest>
private String[] filteringAlias = Strings.EMPTY_ARRAY; private String[] filteringAlias = Strings.EMPTY_ARRAY;
long nowInMillis;
ExplainRequest() { ExplainRequest() {
} }
@ -196,6 +198,12 @@ public class ExplainRequest extends SingleShardOperationRequest<ExplainRequest>
} }
fetchSourceContext = FetchSourceContext.optionalReadFromStream(in); fetchSourceContext = FetchSourceContext.optionalReadFromStream(in);
if (in.getVersion().onOrAfter(Version.V_0_90_6)) {
nowInMillis = in.readVLong();
} else {
nowInMillis = System.currentTimeMillis();
}
} }
@Override @Override
@ -215,5 +223,9 @@ public class ExplainRequest extends SingleShardOperationRequest<ExplainRequest>
} }
FetchSourceContext.optionalWriteToStream(fetchSourceContext, out); FetchSourceContext.optionalWriteToStream(fetchSourceContext, out);
if (out.getVersion().onOrAfter(Version.V_0_90_6)) {
out.writeVLong(nowInMillis);
}
} }
} }

View File

@ -22,6 +22,7 @@ package org.elasticsearch.action.explain;
import org.apache.lucene.index.Term; import org.apache.lucene.index.Term;
import org.apache.lucene.search.Explanation; import org.apache.lucene.search.Explanation;
import org.elasticsearch.ElasticSearchException; import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction; import org.elasticsearch.action.support.single.shard.TransportShardSingleOperationAction;
import org.elasticsearch.cache.recycler.CacheRecycler; import org.elasticsearch.cache.recycler.CacheRecycler;
import org.elasticsearch.cluster.ClusterService; import org.elasticsearch.cluster.ClusterService;
@ -75,6 +76,12 @@ public class TransportExplainAction extends TransportShardSingleOperationAction<
this.cacheRecycler = cacheRecycler; this.cacheRecycler = cacheRecycler;
} }
@Override
protected void doExecute(ExplainRequest request, ActionListener<ExplainResponse> listener) {
request.nowInMillis = System.currentTimeMillis();
super.doExecute(request, listener);
}
protected String transportAction() { protected String transportAction() {
return ExplainAction.NAME; return ExplainAction.NAME;
} }
@ -102,7 +109,8 @@ public class TransportExplainAction extends TransportShardSingleOperationAction<
SearchContext context = new DefaultSearchContext( SearchContext context = new DefaultSearchContext(
0, 0,
new ShardSearchRequest().types(new String[]{request.type()}) new ShardSearchRequest().types(new String[]{request.type()})
.filteringAliases(request.filteringAlias()), .filteringAliases(request.filteringAlias())
.nowInMillis(request.nowInMillis),
null, result.searcher(), indexService, indexShard, null, result.searcher(), indexService, indexShard,
scriptService, cacheRecycler scriptService, cacheRecycler
); );

View File

@ -24,11 +24,15 @@ import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.index.query.FilterBuilders; import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.test.AbstractIntegrationTest; 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 org.junit.Test;
import java.util.Map; import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.queryString;
import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.equalTo;
/** /**
@ -218,4 +222,19 @@ public class ExplainActionTests extends AbstractIntegrationTest {
assertFalse(response.isMatch()); 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));
}
} }