Skip to content

Commit

Permalink
fixes elastic#40405 added name parameter to the constructors and fixe…
Browse files Browse the repository at this point in the history
…d tests
  • Loading branch information
gurkankaymak committed Apr 4, 2019
1 parent 50e47e3 commit f099ecf
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 51 deletions.
Expand Up @@ -130,12 +130,12 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
private final Supplier<BytesReference> documentSupplier;

/**
* @deprecated use {@link #PercolateQueryBuilder(String, BytesReference, XContentType)} with the document content
* @deprecated use {@link #PercolateQueryBuilder(String, String, BytesReference, XContentType)} with the document content
* type to avoid autodetection.
*/
@Deprecated
public PercolateQueryBuilder(String field, String documentType, BytesReference document) {
this(field, documentType, Collections.singletonList(document), XContentHelper.xContentType(document));
public PercolateQueryBuilder(String field, String documentType, String name, BytesReference document) {
this(field, documentType, name, Collections.singletonList(document), XContentHelper.xContentType(document));
}

/**
Expand All @@ -145,8 +145,8 @@ public PercolateQueryBuilder(String field, String documentType, BytesReference d
* @param document The binary blob containing document to percolate
* @param documentXContentType The content type of the binary blob containing the document to percolate
*/
public PercolateQueryBuilder(String field, BytesReference document, XContentType documentXContentType) {
this(field, null, Collections.singletonList(document), documentXContentType);
public PercolateQueryBuilder(String field, String name, BytesReference document, XContentType documentXContentType) {
this(field, null, name, Collections.singletonList(document), documentXContentType);
}

/**
Expand All @@ -156,12 +156,12 @@ public PercolateQueryBuilder(String field, BytesReference document, XContentType
* @param documents The binary blob containing document to percolate
* @param documentXContentType The content type of the binary blob containing the document to percolate
*/
public PercolateQueryBuilder(String field, List<BytesReference> documents, XContentType documentXContentType) {
this(field, null, documents, documentXContentType);
public PercolateQueryBuilder(String field, String name, List<BytesReference> documents, XContentType documentXContentType) {
this(field, null, name, documents, documentXContentType);
}

@Deprecated
public PercolateQueryBuilder(String field, String documentType, List<BytesReference> documents, XContentType documentXContentType) {
public PercolateQueryBuilder(String field, String documentType, String name, List<BytesReference> documents, XContentType documentXContentType) {
if (field == null) {
throw new IllegalArgumentException("[field] is a required argument");
}
Expand All @@ -170,6 +170,7 @@ public PercolateQueryBuilder(String field, String documentType, List<BytesRefere
}
this.field = field;
this.documentType = documentType;
this.name = name;
this.documents = documents;
this.documentXContentType = Objects.requireNonNull(documentXContentType);
indexedDocumentIndex = null;
Expand All @@ -181,12 +182,13 @@ public PercolateQueryBuilder(String field, String documentType, List<BytesRefere
this.documentSupplier = null;
}

private PercolateQueryBuilder(String field, String documentType, Supplier<BytesReference> documentSupplier) {
private PercolateQueryBuilder(String field, String documentType, String name, Supplier<BytesReference> documentSupplier) {
if (field == null) {
throw new IllegalArgumentException("[field] is a required argument");
}
this.field = field;
this.documentType = documentType;
this.name = name;
this.documents = Collections.emptyList();
this.documentXContentType = null;
this.documentSupplier = documentSupplier;
Expand Down Expand Up @@ -473,7 +475,7 @@ public static PercolateQueryBuilder fromXContent(XContentParser parser) throws I

PercolateQueryBuilder queryBuilder;
if (documents.isEmpty() == false) {
queryBuilder = new PercolateQueryBuilder(field, documentType, documents, XContentType.JSON);
queryBuilder = new PercolateQueryBuilder(field, documentType, name, documents, XContentType.JSON);
} else if (indexedDocumentId != null) {
queryBuilder = new PercolateQueryBuilder(field, documentType, indexedDocumentIndex, indexedDocumentType,
indexedDocumentId, indexedDocumentRouting, indexedDocumentPreference, indexedDocumentVersion);
Expand Down Expand Up @@ -519,7 +521,7 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) {
if (source == null) {
return this; // not executed yet
} else {
return new PercolateQueryBuilder(field, documentType, Collections.singletonList(source),
return new PercolateQueryBuilder(field, documentType, name, Collections.singletonList(source),
XContentHelper.xContentType(source));
}
}
Expand Down Expand Up @@ -555,7 +557,9 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) {
listener.onResponse(null);
}, listener::onFailure));
});
return new PercolateQueryBuilder(field, documentType, documentSupplier::get);


return new PercolateQueryBuilder(field, documentType, name, documentSupplier::get);
}

@Override
Expand Down
Expand Up @@ -201,12 +201,12 @@ protected Set<String> getObjectsHoldingArbitraryContent() {

public void testRequiredParameters() {
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> {
new PercolateQueryBuilder(null, new BytesArray("{}"), XContentType.JSON);
new PercolateQueryBuilder(null, "name1", new BytesArray("{}"), XContentType.JSON);
});
assertThat(e.getMessage(), equalTo("[field] is a required argument"));

e = expectThrows(IllegalArgumentException.class,
() -> new PercolateQueryBuilder("_field", "_document_type", null, null));
() -> new PercolateQueryBuilder("_field", "_document_type", "name2", null, null));
assertThat(e.getMessage(), equalTo("[document] is a required argument"));

e = expectThrows(IllegalArgumentException.class, () -> {
Expand Down Expand Up @@ -322,6 +322,7 @@ public void testFieldAlias() throws IOException {
PercolateQuery query = (PercolateQuery) rewrittenBuilder.toQuery(shardContext);

PercolateQueryBuilder aliasBuilder = new PercolateQueryBuilder(aliasField,
"name1",
builder.getDocuments(),
builder.getXContentType());
QueryBuilder rewrittenAliasBuilder = rewriteAndFetch(aliasBuilder, shardContext);
Expand Down
Expand Up @@ -42,13 +42,13 @@ protected void initializeAdditionalMappings(MapperService mapperService) throws
public void testDetectsNestedDocuments() throws IOException {
QueryShardContext shardContext = createShardContext();

PercolateQueryBuilder builder = new PercolateQueryBuilder(queryField,
PercolateQueryBuilder builder = new PercolateQueryBuilder(queryField, "name1",
new BytesArray("{ \"foo\": \"bar\" }"), XContentType.JSON);
QueryBuilder rewrittenBuilder = rewriteAndFetch(builder, shardContext);
PercolateQuery query = (PercolateQuery) rewrittenBuilder.toQuery(shardContext);
assertFalse(query.excludesNestedDocs());

builder = new PercolateQueryBuilder(queryField,
builder = new PercolateQueryBuilder(queryField, "name2",
new BytesArray("{ \"foo\": \"bar\", \"some_nested_object\": [ { \"baz\": 42 } ] }"), XContentType.JSON);
rewrittenBuilder = rewriteAndFetch(builder, shardContext);
query = (PercolateQuery) rewrittenBuilder.toQuery(shardContext);
Expand Down

0 comments on commit f099ecf

Please sign in to comment.