Skip to content

Remove types from percolate query API #46985

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

Merged
merged 7 commits into from
Sep 30, 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 @@ -19,7 +19,6 @@

package org.elasticsearch.percolator;

import org.apache.logging.log4j.LogManager;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.DelegatingAnalyzerWrapper;
import org.apache.lucene.index.BinaryDocValues;
Expand Down Expand Up @@ -54,7 +53,6 @@
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand Down Expand Up @@ -92,49 +90,28 @@
public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBuilder> {
public static final String NAME = "percolate";

private static final DeprecationLogger deprecationLogger = new DeprecationLogger(LogManager.getLogger(ParseField.class));
static final String DOCUMENT_TYPE_DEPRECATION_MESSAGE = "[types removal] Types are deprecated in [percolate] queries. " +
"The [document_type] should no longer be specified.";
static final String TYPE_DEPRECATION_MESSAGE = "[types removal] Types are deprecated in [percolate] queries. " +
"The [type] of the indexed document should no longer be specified.";

static final ParseField DOCUMENT_FIELD = new ParseField("document");
static final ParseField DOCUMENTS_FIELD = new ParseField("documents");
private static final ParseField NAME_FIELD = new ParseField("name");
private static final ParseField QUERY_FIELD = new ParseField("field");
private static final ParseField DOCUMENT_TYPE_FIELD = new ParseField("document_type");
private static final ParseField INDEXED_DOCUMENT_FIELD_INDEX = new ParseField("index");
private static final ParseField INDEXED_DOCUMENT_FIELD_TYPE = new ParseField("type");
private static final ParseField INDEXED_DOCUMENT_FIELD_ID = new ParseField("id");
private static final ParseField INDEXED_DOCUMENT_FIELD_ROUTING = new ParseField("routing");
private static final ParseField INDEXED_DOCUMENT_FIELD_PREFERENCE = new ParseField("preference");
private static final ParseField INDEXED_DOCUMENT_FIELD_VERSION = new ParseField("version");

private final String field;
private String name;
@Deprecated
private final String documentType;
private final List<BytesReference> documents;
private final XContentType documentXContentType;

private final String indexedDocumentIndex;
@Deprecated
private final String indexedDocumentType;
private final String indexedDocumentId;
private final String indexedDocumentRouting;
private final String indexedDocumentPreference;
private final Long indexedDocumentVersion;
private final Supplier<BytesReference> documentSupplier;

/**
* @deprecated use {@link #PercolateQueryBuilder(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));
}

/**
* Creates a percolator query builder instance for percolating a provided document.
*
Expand All @@ -143,7 +120,7 @@ public PercolateQueryBuilder(String field, String documentType, BytesReference d
* @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);
this(field, Collections.singletonList(document), documentXContentType);
}

/**
Expand All @@ -154,67 +131,35 @@ public PercolateQueryBuilder(String field, BytesReference document, XContentType
* @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);
}

@Deprecated
public PercolateQueryBuilder(String field, String documentType, List<BytesReference> documents, XContentType documentXContentType) {
if (field == null) {
throw new IllegalArgumentException("[field] is a required argument");
}
if (documents == null) {
throw new IllegalArgumentException("[document] is a required argument");
}
this.field = field;
this.documentType = documentType;
this.documents = documents;
this.documentXContentType = Objects.requireNonNull(documentXContentType);
indexedDocumentIndex = null;
indexedDocumentType = null;
indexedDocumentId = null;
indexedDocumentRouting = null;
indexedDocumentPreference = null;
indexedDocumentVersion = null;
this.documentSupplier = null;
}

protected PercolateQueryBuilder(String field, String documentType, Supplier<BytesReference> documentSupplier) {
if (field == null) {
throw new IllegalArgumentException("[field] is a required argument");
}
this.field = field;
this.documentType = documentType;
this.documents = Collections.emptyList();
this.documentXContentType = null;
this.documentSupplier = documentSupplier;
indexedDocumentIndex = null;
indexedDocumentType = null;
indexedDocumentId = null;
indexedDocumentRouting = null;
indexedDocumentPreference = null;
indexedDocumentVersion = null;
}

/**
* Creates a percolator query builder instance for percolating a document in a remote index.
*
* @param field The field that contains the percolator query
* @param indexedDocumentIndex The index containing the document to percolate
* @param indexedDocumentType The type containing the document to percolate
* @param indexedDocumentId The id of the document to percolate
* @param indexedDocumentRouting The routing value for the document to percolate
* @param indexedDocumentPreference The preference to use when fetching the document to percolate
* @param indexedDocumentVersion The expected version of the document to percolate
*/
public PercolateQueryBuilder(String field, String indexedDocumentIndex, String indexedDocumentType, String indexedDocumentId,
String indexedDocumentRouting, String indexedDocumentPreference, Long indexedDocumentVersion) {
this(field, null, indexedDocumentIndex, indexedDocumentType, indexedDocumentId, indexedDocumentRouting,
indexedDocumentPreference, indexedDocumentVersion);
}

@Deprecated
public PercolateQueryBuilder(String field, String documentType, String indexedDocumentIndex,
String indexedDocumentType, String indexedDocumentId, String indexedDocumentRouting,
public PercolateQueryBuilder(String field, String indexedDocumentIndex,
String indexedDocumentId, String indexedDocumentRouting,
String indexedDocumentPreference, Long indexedDocumentVersion) {
if (field == null) {
throw new IllegalArgumentException("[field] is a required argument");
Expand All @@ -226,9 +171,7 @@ public PercolateQueryBuilder(String field, String documentType, String indexedDo
throw new IllegalArgumentException("[id] is a required argument");
}
this.field = field;
this.documentType = documentType;
this.indexedDocumentIndex = indexedDocumentIndex;
this.indexedDocumentType = indexedDocumentType;
this.indexedDocumentId = indexedDocumentId;
this.indexedDocumentRouting = indexedDocumentRouting;
this.indexedDocumentPreference = indexedDocumentPreference;
Expand All @@ -238,16 +181,37 @@ public PercolateQueryBuilder(String field, String documentType, String indexedDo
this.documentSupplier = null;
}

protected PercolateQueryBuilder(String field, Supplier<BytesReference> documentSupplier) {
if (field == null) {
throw new IllegalArgumentException("[field] is a required argument");
}
this.field = field;
this.documents = Collections.emptyList();
this.documentXContentType = null;
this.documentSupplier = documentSupplier;
indexedDocumentIndex = null;
indexedDocumentId = null;
indexedDocumentRouting = null;
indexedDocumentPreference = null;
indexedDocumentVersion = null;
}

/**
* Read from a stream.
*/
PercolateQueryBuilder(StreamInput in) throws IOException {
super(in);
field = in.readString();
name = in.readOptionalString();
documentType = in.readOptionalString();
if (in.getVersion().before(Version.V_8_0_0)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this query type, a 7.x 'typeless' query is represented by a null documentType and indexedDocumentType. So I think that here and in doWriteTo, we should always assume these parameters are null instead of _doc.

String documentType = in.readOptionalString();
assert documentType == null;
}
indexedDocumentIndex = in.readOptionalString();
indexedDocumentType = in.readOptionalString();
if (in.getVersion().before(Version.V_8_0_0)) {
String indexedDocumentType = in.readOptionalString();
assert indexedDocumentType == null;
}
indexedDocumentId = in.readOptionalString();
indexedDocumentRouting = in.readOptionalString();
indexedDocumentPreference = in.readOptionalString();
Expand Down Expand Up @@ -281,9 +245,15 @@ protected void doWriteTo(StreamOutput out) throws IOException {
}
out.writeString(field);
out.writeOptionalString(name);
out.writeOptionalString(documentType);
if (out.getVersion().before(Version.V_8_0_0)) {
// In 7x, typeless percolate queries are represented by null documentType values
out.writeOptionalString(null);
}
out.writeOptionalString(indexedDocumentIndex);
out.writeOptionalString(indexedDocumentType);
if (out.getVersion().before(Version.V_8_0_0)) {
// In 7x, typeless percolate queries are represented by null indexedDocumentType values
out.writeOptionalString(null);
}
out.writeOptionalString(indexedDocumentId);
out.writeOptionalString(indexedDocumentRouting);
out.writeOptionalString(indexedDocumentPreference);
Expand All @@ -305,7 +275,6 @@ protected void doWriteTo(StreamOutput out) throws IOException {
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.field(DOCUMENT_TYPE_FIELD.getPreferredName(), documentType);
builder.field(QUERY_FIELD.getPreferredName(), field);
if (name != null) {
builder.field(NAME_FIELD.getPreferredName(), name);
Expand All @@ -321,13 +290,10 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
}
builder.endArray();
}
if (indexedDocumentIndex != null || indexedDocumentType != null || indexedDocumentId != null) {
if (indexedDocumentIndex != null || indexedDocumentId != null) {
if (indexedDocumentIndex != null) {
builder.field(INDEXED_DOCUMENT_FIELD_INDEX.getPreferredName(), indexedDocumentIndex);
}
if (indexedDocumentType != null) {
builder.field(INDEXED_DOCUMENT_FIELD_TYPE.getPreferredName(), indexedDocumentType);
}
if (indexedDocumentId != null) {
builder.field(INDEXED_DOCUMENT_FIELD_ID.getPreferredName(), indexedDocumentId);
}
Expand All @@ -350,10 +316,8 @@ public static PercolateQueryBuilder fromXContent(XContentParser parser) throws I

String field = null;
String name = null;
String documentType = null;

String indexedDocumentIndex = null;
String indexedDocumentType = null;
String indexedDocumentId = null;
String indexedDocumentRouting = null;
String indexedDocumentPreference = null;
Expand Down Expand Up @@ -415,12 +379,8 @@ public static PercolateQueryBuilder fromXContent(XContentParser parser) throws I
field = parser.text();
} else if (NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
name = parser.textOrNull();
} else if (DOCUMENT_TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
documentType = parser.textOrNull();
} else if (INDEXED_DOCUMENT_FIELD_INDEX.match(currentFieldName, parser.getDeprecationHandler())) {
indexedDocumentIndex = parser.text();
} else if (INDEXED_DOCUMENT_FIELD_TYPE.match(currentFieldName, parser.getDeprecationHandler())) {
indexedDocumentType = parser.text();
} else if (INDEXED_DOCUMENT_FIELD_ID.match(currentFieldName, parser.getDeprecationHandler())) {
indexedDocumentId = parser.text();
} else if (INDEXED_DOCUMENT_FIELD_ROUTING.match(currentFieldName, parser.getDeprecationHandler())) {
Expand All @@ -445,10 +405,10 @@ 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, documents, XContentType.JSON);
} else if (indexedDocumentId != null) {
queryBuilder = new PercolateQueryBuilder(field, documentType, indexedDocumentIndex, indexedDocumentType,
indexedDocumentId, indexedDocumentRouting, indexedDocumentPreference, indexedDocumentVersion);
queryBuilder = new PercolateQueryBuilder(field, indexedDocumentIndex, indexedDocumentId, indexedDocumentRouting,
indexedDocumentPreference, indexedDocumentVersion);
} else {
throw new IllegalArgumentException("[" + PercolateQueryBuilder.NAME + "] query, nothing to percolate");
}
Expand All @@ -463,18 +423,16 @@ public static PercolateQueryBuilder fromXContent(XContentParser parser) throws I
@Override
protected boolean doEquals(PercolateQueryBuilder other) {
return Objects.equals(field, other.field)
&& Objects.equals(documentType, other.documentType)
&& Objects.equals(documents, other.documents)
&& Objects.equals(indexedDocumentIndex, other.indexedDocumentIndex)
&& Objects.equals(indexedDocumentType, other.indexedDocumentType)
&& Objects.equals(documentSupplier, other.documentSupplier)
&& Objects.equals(indexedDocumentId, other.indexedDocumentId);

}

@Override
protected int doHashCode() {
return Objects.hash(field, documentType, documents, indexedDocumentIndex, indexedDocumentType, indexedDocumentId, documentSupplier);
return Objects.hash(field, documents, indexedDocumentIndex, indexedDocumentId, documentSupplier);
}

@Override
Expand All @@ -491,21 +449,15 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) {
if (source == null) {
return this; // not executed yet
} else {
PercolateQueryBuilder rewritten = new PercolateQueryBuilder(field, documentType,
PercolateQueryBuilder rewritten = new PercolateQueryBuilder(field,
Collections.singletonList(source), XContentHelper.xContentType(source));
if (name != null) {
rewritten.setName(name);
}
return rewritten;
}
}
GetRequest getRequest;
if (indexedDocumentType != null) {
deprecationLogger.deprecatedAndMaybeLog("percolate_with_type", TYPE_DEPRECATION_MESSAGE);
getRequest = new GetRequest(indexedDocumentIndex, indexedDocumentId);
} else {
getRequest = new GetRequest(indexedDocumentIndex, indexedDocumentId);
}
GetRequest getRequest = new GetRequest(indexedDocumentIndex, indexedDocumentId);
getRequest.preference("_local");
getRequest.routing(indexedDocumentRouting);
getRequest.preference(indexedDocumentPreference);
Expand All @@ -517,22 +469,20 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryShardContext) {
client.get(getRequest, ActionListener.wrap(getResponse -> {
if (getResponse.isExists() == false) {
throw new ResourceNotFoundException(
"indexed document [{}{}/{}] couldn't be found", indexedDocumentIndex,
indexedDocumentType == null ? "" : "/" + indexedDocumentType, indexedDocumentId
"indexed document [{}/{}] couldn't be found", indexedDocumentIndex, indexedDocumentId
);
}
if(getResponse.isSourceEmpty()) {
throw new IllegalArgumentException(
"indexed document [" + indexedDocumentIndex + (indexedDocumentType == null ? "" : "/" + indexedDocumentType) +
"/" + indexedDocumentId + "] source disabled"
"indexed document [" + indexedDocumentIndex + "/" + indexedDocumentId + "] source disabled"
);
}
documentSupplier.set(getResponse.getSourceAsBytesRef());
listener.onResponse(null);
}, listener::onFailure));
});

PercolateQueryBuilder rewritten = new PercolateQueryBuilder(field, documentType, documentSupplier::get);
PercolateQueryBuilder rewritten = new PercolateQueryBuilder(field, documentSupplier::get);
if (name != null) {
rewritten.setName(name);
}
Expand Down Expand Up @@ -566,13 +516,6 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
final DocumentMapper docMapper;
final MapperService mapperService = context.getMapperService();
String type = mapperService.documentMapper().type();
if (documentType != null) {
deprecationLogger.deprecatedAndMaybeLog("percolate_with_document_type", DOCUMENT_TYPE_DEPRECATION_MESSAGE);
if (documentType.equals(type) == false) {
throw new IllegalArgumentException("specified document_type [" + documentType +
"] is not equal to the actual type [" + type + "]");
}
}
docMapper = mapperService.documentMapper(type);
for (BytesReference document : documents) {
docs.add(docMapper.parse(new SourceToParse(context.index().getName(), type, "_temp_id", document, documentXContentType)));
Expand Down Expand Up @@ -622,10 +565,6 @@ public String getField() {
return field;
}

public String getDocumentType() {
return documentType;
}

public List<BytesReference> getDocuments() {
return documents;
}
Expand Down
Loading