Skip to content

Commit

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

Added nowInMillis to ShardCountRequest in a backwards compatible manner

Fixes elastic#3625
  • Loading branch information
javanna committed Sep 17, 2013
1 parent ddc2b42 commit 4e012ad
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 11 deletions.
Expand Up @@ -70,6 +70,8 @@ public class CountRequest extends BroadcastOperationRequest<CountRequest> {

private String[] types = Strings.EMPTY_ARRAY;

long nowInMillis;

CountRequest() {
}

Expand Down
Expand Up @@ -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;
Expand All @@ -39,6 +40,8 @@ class ShardCountRequest extends BroadcastShardOperationRequest {

private String[] types = Strings.EMPTY_ARRAY;

private long nowInMillis;

@Nullable
private String[] filteringAliases;

Expand All @@ -52,6 +55,7 @@ public ShardCountRequest(String index, int shardId, @Nullable String[] filtering
this.querySource = request.querySource();
this.types = request.types();
this.filteringAliases = filteringAliases;
this.nowInMillis = request.nowInMillis;
}

public float minScore() {
Expand All @@ -70,6 +74,10 @@ public String[] filteringAliases() {
return filteringAliases;
}

public long nowInMillis() {
return this.nowInMillis;
}

@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
Expand All @@ -91,6 +99,11 @@ public void readFrom(StreamInput in) throws IOException {
filteringAliases[i] = in.readString();
}
}
if (in.getVersion().onOrAfter(Version.V_0_90_6)) {
nowInMillis = in.readVLong();
} else {
nowInMillis = System.currentTimeMillis();
}
}

@Override
Expand All @@ -112,5 +125,8 @@ public void writeTo(StreamOutput out) throws IOException {
} else {
out.writeVInt(0);
}
if (out.getVersion().onOrAfter(Version.V_0_90_6)) {
out.writeVLong(nowInMillis);
}
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -75,6 +76,12 @@ public TransportCountAction(Settings settings, ThreadPool threadPool, ClusterSer
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;
Expand Down Expand Up @@ -153,7 +160,9 @@ protected ShardCountResponse shardOperation(ShardCountRequest request) throws El

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);
Expand Down
22 changes: 12 additions & 10 deletions src/test/java/org/elasticsearch/count/query/SimpleQueryTests.java
Expand Up @@ -19,7 +19,7 @@

package org.elasticsearch.count.query;

import org.apache.lucene.util.LuceneTestCase;
import org.elasticsearch.AbstractSharedClusterTest;
import org.elasticsearch.ElasticSearchException;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.search.SearchPhaseExecutionException;
Expand All @@ -29,7 +29,6 @@
import org.elasticsearch.index.query.*;
import org.elasticsearch.index.query.CommonTermsQueryBuilder.Operator;
import org.elasticsearch.index.query.MatchQueryBuilder.Type;
import org.elasticsearch.AbstractSharedClusterTest;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.ISODateTimeFormat;
Expand All @@ -41,6 +40,9 @@
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;

/**
*
Expand Down Expand Up @@ -197,7 +199,7 @@ public void testLowercaseExpandedTerms() {
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();

Expand All @@ -206,20 +208,20 @@ public void testDateRangeInQueryString() {

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);

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
Expand Down

0 comments on commit 4e012ad

Please sign in to comment.