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

View File

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

View File

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

View File

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