Skip to content

Commit

Permalink
Fixed ignoring name parameter for percolator queries (#42598)
Browse files Browse the repository at this point in the history
Closes #40405
  • Loading branch information
gurkankaymak authored and jimczi committed May 28, 2019
1 parent c079fb6 commit 1d09367
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ public PercolateQueryBuilder(String field, String documentType, List<BytesRefere
this.documentSupplier = null;
}

private PercolateQueryBuilder(String field, String documentType, Supplier<BytesReference> documentSupplier) {
protected PercolateQueryBuilder(String field, String documentType, Supplier<BytesReference> documentSupplier) {
if (field == null) {
throw new IllegalArgumentException("[field] is a required argument");
}
Expand Down Expand Up @@ -519,8 +519,12 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) {
if (source == null) {
return this; // not executed yet
} else {
return new PercolateQueryBuilder(field, documentType, Collections.singletonList(source),
XContentHelper.xContentType(source));
PercolateQueryBuilder rewritten = new PercolateQueryBuilder(field, documentType,
Collections.singletonList(source), XContentHelper.xContentType(source));
if (name != null) {
rewritten.setName(name);
}
return rewritten;
}
}
GetRequest getRequest;
Expand Down Expand Up @@ -555,7 +559,12 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) {
listener.onResponse(null);
}, listener::onFailure));
});
return new PercolateQueryBuilder(field, documentType, documentSupplier::get);

PercolateQueryBuilder rewritten = new PercolateQueryBuilder(field, documentType, documentSupplier::get);
if (name != null) {
rewritten.setName(name);
}
return rewritten;
}

@Override
Expand Down Expand Up @@ -654,6 +663,10 @@ XContentType getXContentType() {
return documentXContentType;
}

public String getQueryName() {
return name;
}

static IndexSearcher createMultiDocumentSearcher(Analyzer analyzer, Collection<ParsedDocument> docs) {
RAMDirectory ramDirectory = new RAMDirectory();
try (IndexWriter indexWriter = new IndexWriter(ramDirectory, new IndexWriterConfig(analyzer))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;

import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;
import static org.hamcrest.Matchers.equalTo;
Expand Down Expand Up @@ -331,4 +332,29 @@ public void testFieldAlias() throws IOException {
assertEquals(query.getVerifiedMatchesQuery(), aliasQuery.getVerifiedMatchesQuery());
}

public void testSettingNameWhileRewriting() {
String testName = "name1";
QueryShardContext shardContext = createShardContext();
PercolateQueryBuilder percolateQueryBuilder = doCreateTestQueryBuilder(true);
percolateQueryBuilder.setName(testName);

QueryBuilder rewrittenQueryBuilder = percolateQueryBuilder.doRewrite(shardContext);

assertEquals(testName, ((PercolateQueryBuilder) rewrittenQueryBuilder).getQueryName());
assertNotEquals(rewrittenQueryBuilder, percolateQueryBuilder);
}

public void testSettingNameWhileRewritingWhenDocumentSupplierAndSourceNotNull() {
Supplier<BytesReference> supplier = () -> new BytesArray("{\"test\": \"test\"}");
String testName = "name1";
QueryShardContext shardContext = createShardContext();
PercolateQueryBuilder percolateQueryBuilder = new PercolateQueryBuilder(queryField, null, supplier);
percolateQueryBuilder.setName(testName);

QueryBuilder rewrittenQueryBuilder = percolateQueryBuilder.doRewrite(shardContext);

assertEquals(testName, ((PercolateQueryBuilder) rewrittenQueryBuilder).getQueryName());
assertNotEquals(rewrittenQueryBuilder, percolateQueryBuilder);
}

}

0 comments on commit 1d09367

Please sign in to comment.