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 e598f16 commit 5cdbe60
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
Expand Up @@ -37,6 +37,7 @@ public class PercolateShardRequest extends BroadcastShardRequest {
private BytesReference docSource;
private boolean onlyCount;
private int numberOfShards;
private long startTime;

PercolateShardRequest() {
}
Expand All @@ -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) {
Expand All @@ -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() {
Expand Down Expand Up @@ -98,6 +101,10 @@ public int getNumberOfShards() {
return numberOfShards;
}

public long getStartTime() {
return startTime;
}

OriginalIndices originalIndices() {
return originalIndices;
}
Expand All @@ -110,6 +117,7 @@ public void readFrom(StreamInput in) throws IOException {
docSource = in.readBytesReference();
onlyCount = in.readBoolean();
numberOfShards = in.readVInt();
startTime = in.readLong(); // no vlong, this can be negative!
}

@Override
Expand All @@ -120,6 +128,7 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeBytesReference(docSource);
out.writeBoolean(onlyCount);
out.writeVInt(numberOfShards);
out.writeLong(startTime);
}

}
Expand Up @@ -98,6 +98,7 @@ public class PercolateContext extends SearchContext {
private final ConcurrentMap<BytesRef, Query> percolateQueries;
private final int numberOfShards;
private final Query aliasFilter;
private final long startTime;
private String[] types;

private Engine.Searcher docSearcher;
Expand Down Expand Up @@ -133,6 +134,7 @@ public PercolateContext(PercolateShardRequest request, SearchShardTarget searchS
this.scriptService = scriptService;
this.numberOfShards = request.getNumberOfShards();
this.aliasFilter = aliasFilter;
this.startTime = request.getStartTime();
}

public IndexSearcher docSearcher() {
Expand Down Expand Up @@ -337,7 +339,7 @@ public SearchContext queryBoost(float queryBoost) {

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

@Override
Expand Down
Expand Up @@ -180,6 +180,7 @@ public PercolateShardResponse percolate(PercolateShardRequest request) {
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()) {
Expand Down Expand Up @@ -235,6 +236,7 @@ public PercolateShardResponse percolate(PercolateShardRequest request) {
percolatorIndex.prepare(context, parsedDocument);
return action.doPercolate(request, context, isNested);
} finally {
SearchContext.removeCurrent();
context.close();
shardPercolateService.postPercolate(System.nanoTime() - startTime);
}
Expand All @@ -258,7 +260,6 @@ private ParsedDocument parseRequest(IndexService documentIndexService, Percolate
// 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;
Expand Down Expand Up @@ -359,7 +360,6 @@ private ParsedDocument parseRequest(IndexService documentIndexService, Percolate
throw new ElasticsearchParseException("failed to parse request", e);
} finally {
context.types(previousTypes);
SearchContext.removeCurrent();
if (parser != null) {
parser.close();
}
Expand Down
Expand Up @@ -2079,5 +2079,21 @@ public void testPercolateDocumentWithParentField() throws Exception {
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);
}

}

0 comments on commit 5cdbe60

Please sign in to comment.