Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed ignoring name parameter for percolator queries #42598

Merged
merged 1 commit into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,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 @@ -491,8 +491,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 @@ -527,7 +531,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 @@ -626,6 +635,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);
}

}