percolator: Support filtering percolator queries by date using now

Closes #12185
This commit is contained in:
Martijn van Groningen 2015-07-13 23:12:42 +02:00
parent e598f16b58
commit 5cdbe60f6b
4 changed files with 30 additions and 3 deletions

View File

@ -37,6 +37,7 @@ public class PercolateShardRequest extends BroadcastShardRequest {
private BytesReference docSource; private BytesReference docSource;
private boolean onlyCount; private boolean onlyCount;
private int numberOfShards; private int numberOfShards;
private long startTime;
PercolateShardRequest() { PercolateShardRequest() {
} }
@ -48,6 +49,7 @@ public class PercolateShardRequest extends BroadcastShardRequest {
this.docSource = request.docSource(); this.docSource = request.docSource();
this.onlyCount = request.onlyCount(); this.onlyCount = request.onlyCount();
this.numberOfShards = numberOfShards; this.numberOfShards = numberOfShards;
this.startTime = request.startTime;
} }
PercolateShardRequest(ShardId shardId, OriginalIndices originalIndices) { PercolateShardRequest(ShardId shardId, OriginalIndices originalIndices) {
@ -60,6 +62,7 @@ public class PercolateShardRequest extends BroadcastShardRequest {
this.source = request.source(); this.source = request.source();
this.docSource = request.docSource(); this.docSource = request.docSource();
this.onlyCount = request.onlyCount(); this.onlyCount = request.onlyCount();
this.startTime = request.startTime;
} }
public String documentType() { public String documentType() {
@ -98,6 +101,10 @@ public class PercolateShardRequest extends BroadcastShardRequest {
return numberOfShards; return numberOfShards;
} }
public long getStartTime() {
return startTime;
}
OriginalIndices originalIndices() { OriginalIndices originalIndices() {
return originalIndices; return originalIndices;
} }
@ -110,6 +117,7 @@ public class PercolateShardRequest extends BroadcastShardRequest {
docSource = in.readBytesReference(); docSource = in.readBytesReference();
onlyCount = in.readBoolean(); onlyCount = in.readBoolean();
numberOfShards = in.readVInt(); numberOfShards = in.readVInt();
startTime = in.readLong(); // no vlong, this can be negative!
} }
@Override @Override
@ -120,6 +128,7 @@ public class PercolateShardRequest extends BroadcastShardRequest {
out.writeBytesReference(docSource); out.writeBytesReference(docSource);
out.writeBoolean(onlyCount); out.writeBoolean(onlyCount);
out.writeVInt(numberOfShards); out.writeVInt(numberOfShards);
out.writeLong(startTime);
} }
} }

View File

@ -98,6 +98,7 @@ public class PercolateContext extends SearchContext {
private final ConcurrentMap<BytesRef, Query> percolateQueries; private final ConcurrentMap<BytesRef, Query> percolateQueries;
private final int numberOfShards; private final int numberOfShards;
private final Query aliasFilter; private final Query aliasFilter;
private final long startTime;
private String[] types; private String[] types;
private Engine.Searcher docSearcher; private Engine.Searcher docSearcher;
@ -133,6 +134,7 @@ public class PercolateContext extends SearchContext {
this.scriptService = scriptService; this.scriptService = scriptService;
this.numberOfShards = request.getNumberOfShards(); this.numberOfShards = request.getNumberOfShards();
this.aliasFilter = aliasFilter; this.aliasFilter = aliasFilter;
this.startTime = request.getStartTime();
} }
public IndexSearcher docSearcher() { public IndexSearcher docSearcher() {
@ -337,7 +339,7 @@ public class PercolateContext extends SearchContext {
@Override @Override
protected long nowInMillisImpl() { protected long nowInMillisImpl() {
throw new UnsupportedOperationException(); return startTime;
} }
@Override @Override

View File

@ -180,6 +180,7 @@ public class PercolatorService extends AbstractComponent {
final PercolateContext context = new PercolateContext( final PercolateContext context = new PercolateContext(
request, searchShardTarget, indexShard, percolateIndexService, pageCacheRecycler, bigArrays, scriptService, aliasFilter, parseFieldMatcher request, searchShardTarget, indexShard, percolateIndexService, pageCacheRecycler, bigArrays, scriptService, aliasFilter, parseFieldMatcher
); );
SearchContext.setCurrent(context);
try { try {
ParsedDocument parsedDocument = parseRequest(percolateIndexService, request, context); ParsedDocument parsedDocument = parseRequest(percolateIndexService, request, context);
if (context.percolateQueries().isEmpty()) { if (context.percolateQueries().isEmpty()) {
@ -235,6 +236,7 @@ public class PercolatorService extends AbstractComponent {
percolatorIndex.prepare(context, parsedDocument); percolatorIndex.prepare(context, parsedDocument);
return action.doPercolate(request, context, isNested); return action.doPercolate(request, context, isNested);
} finally { } finally {
SearchContext.removeCurrent();
context.close(); context.close();
shardPercolateService.postPercolate(System.nanoTime() - startTime); shardPercolateService.postPercolate(System.nanoTime() - startTime);
} }
@ -258,7 +260,6 @@ public class PercolatorService extends AbstractComponent {
// not the in memory percolate doc // not the in memory percolate doc
String[] previousTypes = context.types(); String[] previousTypes = context.types();
context.types(new String[]{TYPE_NAME}); context.types(new String[]{TYPE_NAME});
SearchContext.setCurrent(context);
try { try {
parser = XContentFactory.xContent(source).createParser(source); parser = XContentFactory.xContent(source).createParser(source);
String currentFieldName = null; String currentFieldName = null;
@ -359,7 +360,6 @@ public class PercolatorService extends AbstractComponent {
throw new ElasticsearchParseException("failed to parse request", e); throw new ElasticsearchParseException("failed to parse request", e);
} finally { } finally {
context.types(previousTypes); context.types(previousTypes);
SearchContext.removeCurrent();
if (parser != null) { if (parser != null) {
parser.close(); parser.close();
} }

View File

@ -2079,5 +2079,21 @@ public class PercolatorTests extends ElasticsearchIntegrationTest {
assertThat(response.getMatches()[0].getId().string(), equalTo("1")); assertThat(response.getMatches()[0].getId().string(), equalTo("1"));
} }
@Test
public void testFilterByNow() throws Exception {
client().prepareIndex("index", PercolatorService.TYPE_NAME, "1")
.setSource(jsonBuilder().startObject().field("query", matchAllQuery()).field("created", "2015-07-10T14:41:54+0000").endObject())
.get();
refresh();
PercolateResponse response = client().preparePercolate()
.setIndices("index")
.setDocumentType("type")
.setPercolateDoc(new PercolateSourceBuilder.DocBuilder().setDoc("{}"))
.setPercolateQuery(rangeQuery("created").lte("now"))
.get();
assertMatchCount(response, 1);
}
} }