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

Added nowInMillis to ShardValidateQueryRequest in a backwards compatible manner

Fixes #3629
This commit is contained in:
Luca Cavanna 2013-09-15 03:06:25 +02:00
parent 06bfe0550d
commit 0d3895533c
4 changed files with 55 additions and 3 deletions

View File

@ -19,6 +19,7 @@
package org.elasticsearch.action.admin.indices.validate.query;
import org.elasticsearch.Version;
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationRequest;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.Strings;
@ -36,6 +37,7 @@ class ShardValidateQueryRequest extends BroadcastShardOperationRequest {
private BytesReference querySource;
private String[] types = Strings.EMPTY_ARRAY;
private boolean explain;
private long nowInMillis;
@Nullable
private String[] filteringAliases;
@ -50,6 +52,7 @@ class ShardValidateQueryRequest extends BroadcastShardOperationRequest {
this.types = request.types();
this.explain = request.explain();
this.filteringAliases = filteringAliases;
this.nowInMillis = request.nowInMillis;
}
public BytesReference querySource() {
@ -68,6 +71,10 @@ class ShardValidateQueryRequest extends BroadcastShardOperationRequest {
return filteringAliases;
}
public long nowInMillis() {
return this.nowInMillis;
}
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
@ -89,6 +96,12 @@ class ShardValidateQueryRequest extends BroadcastShardOperationRequest {
}
explain = in.readBoolean();
if (in.getVersion().onOrAfter(Version.V_0_90_6)) {
nowInMillis = in.readVLong();
} else {
nowInMillis = System.currentTimeMillis();
}
}
@Override
@ -110,5 +123,9 @@ class ShardValidateQueryRequest extends BroadcastShardOperationRequest {
}
out.writeBoolean(explain);
if (out.getVersion().onOrAfter(Version.V_0_90_6)) {
out.writeVLong(nowInMillis);
}
}
}

View File

@ -21,6 +21,7 @@ package org.elasticsearch.action.admin.indices.validate.query;
import jsr166y.ThreadLocalRandom;
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;
@ -73,6 +74,12 @@ public class TransportValidateQueryAction extends TransportBroadcastOperationAct
this.cacheRecycler = cacheRecycler;
}
@Override
protected void doExecute(ValidateQueryRequest request, ActionListener<ValidateQueryResponse> listener) {
request.nowInMillis = System.currentTimeMillis();
super.doExecute(request, listener);
}
@Override
protected String executor() {
return ThreadPool.Names.SEARCH;
@ -171,7 +178,7 @@ public class TransportValidateQueryAction extends TransportBroadcastOperationAct
valid = true;
} else {
SearchContext.setCurrent(new DefaultSearchContext(0,
new ShardSearchRequest().types(request.types()),
new ShardSearchRequest().types(request.types()).nowInMillis(request.nowInMillis()),
null, indexShard.acquireSearcher(), indexService, indexShard,
scriptService, cacheRecycler));
try {

View File

@ -56,6 +56,8 @@ public class ValidateQueryRequest extends BroadcastOperationRequest<ValidateQuer
private String[] types = Strings.EMPTY_ARRAY;
long nowInMillis;
ValidateQueryRequest() {
}
@ -128,7 +130,6 @@ public class ValidateQueryRequest extends BroadcastOperationRequest<ValidateQuer
@Required
public ValidateQueryRequest query(String querySource) {
this.querySource = new BytesArray(querySource);
;
this.querySourceUnsafe = false;
return this;
}

View File

@ -32,10 +32,15 @@ import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.test.AbstractIntegrationTest;
import org.hamcrest.Matcher;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.format.ISODateTimeFormat;
import org.junit.Test;
import java.io.IOException;
import static org.elasticsearch.index.query.QueryBuilders.queryString;
import static org.elasticsearch.test.hamcrest.ElasticSearchAssertions.*;
import static org.hamcrest.Matchers.*;
/**
@ -227,6 +232,29 @@ public class SimpleValidateQueryTests extends AbstractIntegrationTest {
}
}
@Test //https://github.com/elasticsearch/elasticsearch/issues/3629
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();
ValidateQueryResponse response = client().admin().indices().prepareValidateQuery()
.setQuery(queryString("past:[now-2M/d TO now/d]")).setExplain(true).get();
assertNoFailures(response);
assertThat(response.getQueryExplanation().size(), equalTo(1));
assertThat(response.getQueryExplanation().get(0).getError(), nullValue());
DateTime twoMonthsAgo = new DateTime(DateTimeZone.UTC).minusMonths(2).withTimeAtStartOfDay();
DateTime now = new DateTime(DateTimeZone.UTC).plusDays(1).withTimeAtStartOfDay();
assertThat(response.getQueryExplanation().get(0).getExplanation(),
equalTo("past:[" + twoMonthsAgo.getMillis() + " TO " + now.getMillis() + "]"));
assertThat(response.isValid(), equalTo(true));
}
private void assertExplanation(QueryBuilder queryBuilder, Matcher<String> matcher) {
ValidateQueryResponse response = client().admin().indices().prepareValidateQuery("test")
@ -239,5 +267,4 @@ public class SimpleValidateQueryTests extends AbstractIntegrationTest {
assertThat(response.getQueryExplanation().get(0).getExplanation(), matcher);
assertThat(response.isValid(), equalTo(true));
}
}