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

Added nowInMillis to ShardCountRequest in a backwards compatible manner

Fixes #3625
This commit is contained in:
Luca Cavanna 2013-09-13 17:36:52 +02:00
parent d365a4ccba
commit 06bfe0550d
4 changed files with 39 additions and 10 deletions

View File

@ -70,6 +70,8 @@ public class CountRequest extends BroadcastOperationRequest<CountRequest> {
private String[] types = Strings.EMPTY_ARRAY;
long nowInMillis;
CountRequest() {
}

View File

@ -19,6 +19,7 @@
package org.elasticsearch.action.count;
import org.elasticsearch.Version;
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationRequest;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
@ -39,6 +40,8 @@ class ShardCountRequest extends BroadcastShardOperationRequest {
private String[] types = Strings.EMPTY_ARRAY;
private long nowInMillis;
@Nullable
private String[] filteringAliases;
@ -52,6 +55,7 @@ class ShardCountRequest extends BroadcastShardOperationRequest {
this.querySource = request.querySource();
this.types = request.types();
this.filteringAliases = filteringAliases;
this.nowInMillis = request.nowInMillis;
}
public float minScore() {
@ -70,6 +74,10 @@ class ShardCountRequest extends BroadcastShardOperationRequest {
return filteringAliases;
}
public long nowInMillis() {
return this.nowInMillis;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
@ -91,6 +99,11 @@ class ShardCountRequest extends BroadcastShardOperationRequest {
filteringAliases[i] = in.readString();
}
}
if (in.getVersion().onOrAfter(Version.V_0_90_6)) {
nowInMillis = in.readVLong();
} else {
nowInMillis = System.currentTimeMillis();
}
}
@Override
@ -112,5 +125,8 @@ class ShardCountRequest extends BroadcastShardOperationRequest {
} else {
out.writeVInt(0);
}
if (out.getVersion().onOrAfter(Version.V_0_90_6)) {
out.writeVLong(nowInMillis);
}
}
}

View File

@ -20,6 +20,7 @@
package org.elasticsearch.action.count;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ShardOperationFailedException;
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException;
@ -75,6 +76,12 @@ public class TransportCountAction extends TransportBroadcastOperationAction<Coun
this.cacheRecycler = cacheRecycler;
}
@Override
protected void doExecute(CountRequest request, ActionListener<CountResponse> listener) {
request.nowInMillis = System.currentTimeMillis();
super.doExecute(request, listener);
}
@Override
protected String executor() {
return ThreadPool.Names.SEARCH;
@ -153,7 +160,9 @@ public class TransportCountAction extends TransportBroadcastOperationAction<Coun
SearchShardTarget shardTarget = new SearchShardTarget(clusterService.localNode().id(), request.index(), request.shardId());
SearchContext context = new DefaultSearchContext(0,
new ShardSearchRequest().types(request.types()).filteringAliases(request.filteringAliases()),
new ShardSearchRequest().types(request.types())
.filteringAliases(request.filteringAliases())
.nowInMillis(request.nowInMillis()),
shardTarget, indexShard.acquireSearcher(), indexService, indexShard,
scriptService, cacheRecycler);
SearchContext.setCurrent(context);

View File

@ -19,7 +19,6 @@
package org.elasticsearch.count.query;
import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
@ -41,6 +40,9 @@ import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.FilterBuilders.*;
import static org.elasticsearch.index.query.QueryBuilders.*;
import static org.elasticsearch.test.hamcrest.ElasticSearchAssertions.*;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
/**
*
@ -197,7 +199,7 @@ public class SimpleQueryTests extends AbstractIntegrationTest {
assertHitCount(countResponse, 0l);
}
@Test @LuceneTestCase.AwaitsFix(bugUrl = "https://github.com/elasticsearch/elasticsearch/issues/3625")
@Test
public void testDateRangeInQueryString() {
client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet();
@ -206,7 +208,7 @@ public class SimpleQueryTests extends AbstractIntegrationTest {
client().prepareIndex("test", "type", "1").setSource("past", aMonthAgo, "future", aMonthFromNow).execute().actionGet();
client().admin().indices().prepareRefresh().execute().actionGet();
refresh();
CountResponse countResponse = client().prepareCount().setQuery(queryString("past:[now-2M/d TO now/d]")).execute().actionGet();
assertHitCount(countResponse, 1l);
@ -214,12 +216,12 @@ public class SimpleQueryTests extends AbstractIntegrationTest {
countResponse = client().prepareCount().setQuery(queryString("future:[now/d TO now+2M/d]").lowercaseExpandedTerms(false)).execute().actionGet();
assertHitCount(countResponse, 1l);
try {
client().prepareCount().setQuery(queryString("future:[now/D TO now+2M/d]").lowercaseExpandedTerms(false)).execute().actionGet();
fail("D is an unsupported unit in date math");
} catch (Exception e) {
// expected
}
countResponse = client().prepareCount().setQuery(queryString("future:[now/D TO now+2M/d]").lowercaseExpandedTerms(false)).execute().actionGet();
//D is an unsupported unit in date math
assertThat(countResponse.getSuccessfulShards(), equalTo(0));
assertThat(countResponse.getFailedShards(), equalTo(1));
assertThat(countResponse.getShardFailures().length, equalTo(1));
assertThat(countResponse.getShardFailures()[0].reason(), allOf(containsString("Failed to parse"), containsString("unit [D] not supported for date math")));
}
@Test