From 5cdbe60f6b66dee44457e86bc8af971d2a9ea4d8 Mon Sep 17 00:00:00 2001 From: Martijn van Groningen Date: Mon, 13 Jul 2015 23:12:42 +0200 Subject: [PATCH] percolator: Support filtering percolator queries by date using `now` Closes #12185 --- .../action/percolate/PercolateShardRequest.java | 9 +++++++++ .../percolator/PercolateContext.java | 4 +++- .../percolator/PercolatorService.java | 4 ++-- .../percolator/PercolatorTests.java | 16 ++++++++++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/action/percolate/PercolateShardRequest.java b/core/src/main/java/org/elasticsearch/action/percolate/PercolateShardRequest.java index f0b1a96e1d5..b03c667c939 100644 --- a/core/src/main/java/org/elasticsearch/action/percolate/PercolateShardRequest.java +++ b/core/src/main/java/org/elasticsearch/action/percolate/PercolateShardRequest.java @@ -37,6 +37,7 @@ public class PercolateShardRequest extends BroadcastShardRequest { private BytesReference docSource; private boolean onlyCount; private int numberOfShards; + private long startTime; PercolateShardRequest() { } @@ -48,6 +49,7 @@ public class PercolateShardRequest extends BroadcastShardRequest { this.docSource = request.docSource(); this.onlyCount = request.onlyCount(); this.numberOfShards = numberOfShards; + this.startTime = request.startTime; } PercolateShardRequest(ShardId shardId, OriginalIndices originalIndices) { @@ -60,6 +62,7 @@ public class PercolateShardRequest extends BroadcastShardRequest { this.source = request.source(); this.docSource = request.docSource(); this.onlyCount = request.onlyCount(); + this.startTime = request.startTime; } public String documentType() { @@ -98,6 +101,10 @@ public class PercolateShardRequest extends BroadcastShardRequest { return numberOfShards; } + public long getStartTime() { + return startTime; + } + OriginalIndices originalIndices() { return originalIndices; } @@ -110,6 +117,7 @@ public class PercolateShardRequest extends BroadcastShardRequest { docSource = in.readBytesReference(); onlyCount = in.readBoolean(); numberOfShards = in.readVInt(); + startTime = in.readLong(); // no vlong, this can be negative! } @Override @@ -120,6 +128,7 @@ public class PercolateShardRequest extends BroadcastShardRequest { out.writeBytesReference(docSource); out.writeBoolean(onlyCount); out.writeVInt(numberOfShards); + out.writeLong(startTime); } } diff --git a/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java b/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java index d9e689d5f75..9368860a689 100644 --- a/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java +++ b/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java @@ -98,6 +98,7 @@ public class PercolateContext extends SearchContext { private final ConcurrentMap percolateQueries; private final int numberOfShards; private final Query aliasFilter; + private final long startTime; private String[] types; private Engine.Searcher docSearcher; @@ -133,6 +134,7 @@ public class PercolateContext extends SearchContext { this.scriptService = scriptService; this.numberOfShards = request.getNumberOfShards(); this.aliasFilter = aliasFilter; + this.startTime = request.getStartTime(); } public IndexSearcher docSearcher() { @@ -337,7 +339,7 @@ public class PercolateContext extends SearchContext { @Override protected long nowInMillisImpl() { - throw new UnsupportedOperationException(); + return startTime; } @Override diff --git a/core/src/main/java/org/elasticsearch/percolator/PercolatorService.java b/core/src/main/java/org/elasticsearch/percolator/PercolatorService.java index 41a2a0b5412..b035f3fb8b0 100644 --- a/core/src/main/java/org/elasticsearch/percolator/PercolatorService.java +++ b/core/src/main/java/org/elasticsearch/percolator/PercolatorService.java @@ -180,6 +180,7 @@ public class PercolatorService extends AbstractComponent { final PercolateContext context = new PercolateContext( request, searchShardTarget, indexShard, percolateIndexService, pageCacheRecycler, bigArrays, scriptService, aliasFilter, parseFieldMatcher ); + SearchContext.setCurrent(context); try { ParsedDocument parsedDocument = parseRequest(percolateIndexService, request, context); if (context.percolateQueries().isEmpty()) { @@ -235,6 +236,7 @@ public class PercolatorService extends AbstractComponent { percolatorIndex.prepare(context, parsedDocument); return action.doPercolate(request, context, isNested); } finally { + SearchContext.removeCurrent(); context.close(); shardPercolateService.postPercolate(System.nanoTime() - startTime); } @@ -258,7 +260,6 @@ public class PercolatorService extends AbstractComponent { // not the in memory percolate doc String[] previousTypes = context.types(); context.types(new String[]{TYPE_NAME}); - SearchContext.setCurrent(context); try { parser = XContentFactory.xContent(source).createParser(source); String currentFieldName = null; @@ -359,7 +360,6 @@ public class PercolatorService extends AbstractComponent { throw new ElasticsearchParseException("failed to parse request", e); } finally { context.types(previousTypes); - SearchContext.removeCurrent(); if (parser != null) { parser.close(); } diff --git a/core/src/test/java/org/elasticsearch/percolator/PercolatorTests.java b/core/src/test/java/org/elasticsearch/percolator/PercolatorTests.java index 91fd2af3bd1..46144a0b39f 100644 --- a/core/src/test/java/org/elasticsearch/percolator/PercolatorTests.java +++ b/core/src/test/java/org/elasticsearch/percolator/PercolatorTests.java @@ -2079,5 +2079,21 @@ public class PercolatorTests extends ElasticsearchIntegrationTest { 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); + } + }