mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-09 14:34:43 +00:00
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:
parent
d365a4ccba
commit
06bfe0550d
@ -70,6 +70,8 @@ public class CountRequest extends BroadcastOperationRequest<CountRequest> {
|
|||||||
|
|
||||||
private String[] types = Strings.EMPTY_ARRAY;
|
private String[] types = Strings.EMPTY_ARRAY;
|
||||||
|
|
||||||
|
long nowInMillis;
|
||||||
|
|
||||||
CountRequest() {
|
CountRequest() {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
package org.elasticsearch.action.count;
|
package org.elasticsearch.action.count;
|
||||||
|
|
||||||
|
import org.elasticsearch.Version;
|
||||||
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationRequest;
|
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationRequest;
|
||||||
import org.elasticsearch.common.Nullable;
|
import org.elasticsearch.common.Nullable;
|
||||||
import org.elasticsearch.common.Strings;
|
import org.elasticsearch.common.Strings;
|
||||||
@ -39,6 +40,8 @@ class ShardCountRequest extends BroadcastShardOperationRequest {
|
|||||||
|
|
||||||
private String[] types = Strings.EMPTY_ARRAY;
|
private String[] types = Strings.EMPTY_ARRAY;
|
||||||
|
|
||||||
|
private long nowInMillis;
|
||||||
|
|
||||||
@Nullable
|
@Nullable
|
||||||
private String[] filteringAliases;
|
private String[] filteringAliases;
|
||||||
|
|
||||||
@ -52,6 +55,7 @@ class ShardCountRequest extends BroadcastShardOperationRequest {
|
|||||||
this.querySource = request.querySource();
|
this.querySource = request.querySource();
|
||||||
this.types = request.types();
|
this.types = request.types();
|
||||||
this.filteringAliases = filteringAliases;
|
this.filteringAliases = filteringAliases;
|
||||||
|
this.nowInMillis = request.nowInMillis;
|
||||||
}
|
}
|
||||||
|
|
||||||
public float minScore() {
|
public float minScore() {
|
||||||
@ -70,6 +74,10 @@ class ShardCountRequest extends BroadcastShardOperationRequest {
|
|||||||
return filteringAliases;
|
return filteringAliases;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public long nowInMillis() {
|
||||||
|
return this.nowInMillis;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void readFrom(StreamInput in) throws IOException {
|
public void readFrom(StreamInput in) throws IOException {
|
||||||
super.readFrom(in);
|
super.readFrom(in);
|
||||||
@ -91,6 +99,11 @@ class ShardCountRequest extends BroadcastShardOperationRequest {
|
|||||||
filteringAliases[i] = in.readString();
|
filteringAliases[i] = in.readString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (in.getVersion().onOrAfter(Version.V_0_90_6)) {
|
||||||
|
nowInMillis = in.readVLong();
|
||||||
|
} else {
|
||||||
|
nowInMillis = System.currentTimeMillis();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -112,5 +125,8 @@ class ShardCountRequest extends BroadcastShardOperationRequest {
|
|||||||
} else {
|
} else {
|
||||||
out.writeVInt(0);
|
out.writeVInt(0);
|
||||||
}
|
}
|
||||||
|
if (out.getVersion().onOrAfter(Version.V_0_90_6)) {
|
||||||
|
out.writeVLong(nowInMillis);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@
|
|||||||
package org.elasticsearch.action.count;
|
package org.elasticsearch.action.count;
|
||||||
|
|
||||||
import org.elasticsearch.ElasticSearchException;
|
import org.elasticsearch.ElasticSearchException;
|
||||||
|
import org.elasticsearch.action.ActionListener;
|
||||||
import org.elasticsearch.action.ShardOperationFailedException;
|
import org.elasticsearch.action.ShardOperationFailedException;
|
||||||
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
|
import org.elasticsearch.action.support.DefaultShardOperationFailedException;
|
||||||
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException;
|
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException;
|
||||||
@ -75,6 +76,12 @@ public class TransportCountAction extends TransportBroadcastOperationAction<Coun
|
|||||||
this.cacheRecycler = cacheRecycler;
|
this.cacheRecycler = cacheRecycler;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doExecute(CountRequest request, ActionListener<CountResponse> listener) {
|
||||||
|
request.nowInMillis = System.currentTimeMillis();
|
||||||
|
super.doExecute(request, listener);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String executor() {
|
protected String executor() {
|
||||||
return ThreadPool.Names.SEARCH;
|
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());
|
SearchShardTarget shardTarget = new SearchShardTarget(clusterService.localNode().id(), request.index(), request.shardId());
|
||||||
SearchContext context = new DefaultSearchContext(0,
|
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,
|
shardTarget, indexShard.acquireSearcher(), indexService, indexShard,
|
||||||
scriptService, cacheRecycler);
|
scriptService, cacheRecycler);
|
||||||
SearchContext.setCurrent(context);
|
SearchContext.setCurrent(context);
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
|
|
||||||
package org.elasticsearch.count.query;
|
package org.elasticsearch.count.query;
|
||||||
|
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
|
||||||
import org.elasticsearch.ElasticSearchException;
|
import org.elasticsearch.ElasticSearchException;
|
||||||
import org.elasticsearch.action.count.CountResponse;
|
import org.elasticsearch.action.count.CountResponse;
|
||||||
import org.elasticsearch.action.search.SearchPhaseExecutionException;
|
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.FilterBuilders.*;
|
||||||
import static org.elasticsearch.index.query.QueryBuilders.*;
|
import static org.elasticsearch.index.query.QueryBuilders.*;
|
||||||
import static org.elasticsearch.test.hamcrest.ElasticSearchAssertions.*;
|
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);
|
assertHitCount(countResponse, 0l);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test @LuceneTestCase.AwaitsFix(bugUrl = "https://github.com/elasticsearch/elasticsearch/issues/3625")
|
@Test
|
||||||
public void testDateRangeInQueryString() {
|
public void testDateRangeInQueryString() {
|
||||||
client().admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 1)).execute().actionGet();
|
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().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();
|
CountResponse countResponse = client().prepareCount().setQuery(queryString("past:[now-2M/d TO now/d]")).execute().actionGet();
|
||||||
assertHitCount(countResponse, 1l);
|
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();
|
countResponse = client().prepareCount().setQuery(queryString("future:[now/d TO now+2M/d]").lowercaseExpandedTerms(false)).execute().actionGet();
|
||||||
assertHitCount(countResponse, 1l);
|
assertHitCount(countResponse, 1l);
|
||||||
|
|
||||||
try {
|
countResponse = client().prepareCount().setQuery(queryString("future:[now/D TO now+2M/d]").lowercaseExpandedTerms(false)).execute().actionGet();
|
||||||
client().prepareCount().setQuery(queryString("future:[now/D TO now+2M/d]").lowercaseExpandedTerms(false)).execute().actionGet();
|
//D is an unsupported unit in date math
|
||||||
fail("D is an unsupported unit in date math");
|
assertThat(countResponse.getSuccessfulShards(), equalTo(0));
|
||||||
} catch (Exception e) {
|
assertThat(countResponse.getFailedShards(), equalTo(1));
|
||||||
// expected
|
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
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user