Fixed ignoring name parameter for percolator queries (#42598)

Closes #40405
This commit is contained in:
Gürkan Kaymak 2019-05-28 10:37:04 +03:00 committed by jimczi
parent c079fb61bf
commit 1d09367a82
2 changed files with 43 additions and 4 deletions

View File

@ -181,7 +181,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
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");
}
@ -519,8 +519,12 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
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;
@ -555,7 +559,12 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
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
@ -654,6 +663,10 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
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))) {

View File

@ -54,6 +54,7 @@ import java.util.HashSet;
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;
@ -331,4 +332,29 @@ public class PercolateQueryBuilderTests extends AbstractQueryTestCase<PercolateQ
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);
}
}