Skip to content

Commit

Permalink
percolator: Support filtering percolator queries by date using now
Browse files Browse the repository at this point in the history
Closes #12185
  • Loading branch information
martijnvg committed Jul 14, 2015
1 parent 7e51ec3 commit 3b55c97
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
Expand Up @@ -38,6 +38,7 @@ public class PercolateShardRequest extends BroadcastShardOperationRequest {
private BytesReference docSource;
private boolean onlyCount;
private int numberOfShards;
private long startTime = -1;

PercolateShardRequest() {
}
Expand All @@ -49,6 +50,7 @@ public class PercolateShardRequest extends BroadcastShardOperationRequest {
this.docSource = request.docSource();
this.onlyCount = request.onlyCount();
this.numberOfShards = numberOfShards;
this.startTime = request.startTime;
}

PercolateShardRequest(ShardId shardId, OriginalIndices originalIndices) {
Expand Down Expand Up @@ -99,6 +101,10 @@ public int getNumberOfShards() {
return numberOfShards;
}

public long getStartTime() {
return startTime;
}

OriginalIndices originalIndices() {
return originalIndices;
}
Expand All @@ -113,6 +119,9 @@ public void readFrom(StreamInput in) throws IOException {
if (in.getVersion().onOrAfter(Version.V_1_2_0)) {
numberOfShards = in.readVInt();
}
if (in.getVersion().onOrAfter(Version.V_1_6_1)) {
startTime = in.readLong();
}
}

@Override
Expand All @@ -125,6 +134,9 @@ public void writeTo(StreamOutput out) throws IOException {
if (out.getVersion().onOrAfter(Version.V_1_2_0)) {
out.writeVInt(numberOfShards);
}
if (out.getVersion().onOrAfter(Version.V_1_6_1)) {
out.writeLong(startTime);
}
}

}
Expand Up @@ -100,6 +100,7 @@ public class PercolateContext extends SearchContext {
private final int numberOfShards;
private final Filter aliasFilter;
private String[] types;
private final long startTime;

private Engine.Searcher docSearcher;
private Engine.Searcher engineSearcher;
Expand All @@ -126,6 +127,8 @@ public PercolateContext(PercolateShardRequest request, SearchShardTarget searchS
this.searchShardTarget = searchShardTarget;
this.percolateQueries = indexShard.percolateRegistry().percolateQueries();
this.types = new String[]{request.documentType()};
long startTime = request.getStartTime();
this.startTime = startTime == -1 ? System.currentTimeMillis() : startTime;
this.cacheRecycler = cacheRecycler;
this.pageCacheRecycler = pageCacheRecycler;
this.bigArrays = bigArrays.withCircuitBreaking();
Expand Down Expand Up @@ -349,7 +352,7 @@ public SearchContext queryBoost(float queryBoost) {

@Override
protected long nowInMillisImpl() {
throw new UnsupportedOperationException();
return startTime;
}

@Override
Expand Down
16 changes: 16 additions & 0 deletions src/test/java/org/elasticsearch/percolator/PercolatorTests.java
Expand Up @@ -2157,5 +2157,21 @@ public void testFailNicelyWithInnerHits() throws Exception {
assertThat(e.getCause().getMessage(), containsString("inner_hits unsupported"));
}
}

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

0 comments on commit 3b55c97

Please sign in to comment.