Skip to content

Commit

Permalink
Set nowInMillis to search context created by the explain api so that …
Browse files Browse the repository at this point in the history
…"NOW" can be used within queries

Added nowInMillis to ExplainRequest in a backwards compatible manner

Fixes elastic#3626
  • Loading branch information
javanna committed Sep 17, 2013
1 parent 5467493 commit 5d53952
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
Expand Up @@ -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;
Expand Down Expand Up @@ -51,6 +51,8 @@ public class ExplainRequest extends SingleShardOperationRequest<ExplainRequest>

private String[] filteringAlias = Strings.EMPTY_ARRAY;

long nowInMillis;

ExplainRequest() {
}

Expand Down Expand Up @@ -196,6 +198,12 @@ public void readFrom(StreamInput in) throws IOException {
}

fetchSourceContext = FetchSourceContext.optionalReadFromStream(in);

if (in.getVersion().onOrAfter(Version.V_0_90_6)) {
nowInMillis = in.readVLong();
} else {
nowInMillis = System.currentTimeMillis();
}
}

@Override
Expand All @@ -215,5 +223,9 @@ public void writeTo(StreamOutput out) throws IOException {
}

FetchSourceContext.optionalWriteToStream(fetchSourceContext, out);

if (out.getVersion().onOrAfter(Version.V_0_90_6)) {
out.writeVLong(nowInMillis);
}
}
}
Expand Up @@ -22,6 +22,7 @@
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;
Expand Down Expand Up @@ -75,6 +76,12 @@ public TransportExplainAction(Settings settings, ThreadPool threadPool, ClusterS
this.cacheRecycler = cacheRecycler;
}

@Override
protected void doExecute(ExplainRequest request, ActionListener<ExplainResponse> listener) {
request.nowInMillis = System.currentTimeMillis();
super.doExecute(request, listener);
}

protected String transportAction() {
return ExplainAction.NAME;
}
Expand Down Expand Up @@ -102,7 +109,8 @@ protected ExplainResponse shardOperation(ExplainRequest request, int shardId) th
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
);
Expand Down
22 changes: 20 additions & 2 deletions src/test/java/org/elasticsearch/explain/ExplainActionTests.java
Expand Up @@ -19,18 +19,21 @@

package org.elasticsearch.explain;

import org.elasticsearch.AbstractSharedClusterTest;
import org.elasticsearch.action.explain.ExplainResponse;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.indices.IndexMissingException;
import org.elasticsearch.AbstractSharedClusterTest;
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.hamcrest.MatcherAssert.assertThat;
import static org.elasticsearch.index.query.QueryBuilders.queryString;
import static org.hamcrest.Matchers.equalTo;

/**
Expand Down Expand Up @@ -240,4 +243,19 @@ public void testExplainWithAlias() throws Exception {
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));
}
}

0 comments on commit 5d53952

Please sign in to comment.