diff options
Diffstat (limited to 'modules/percolator/src/main/java/org/elasticsearch/percolator')
-rw-r--r-- | modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java index 632c502283..97a65a29ad 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java @@ -57,6 +57,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.common.xcontent.XContentParser; +import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.analysis.FieldNameAnalyzer; import org.elasticsearch.index.mapper.DocumentMapper; import org.elasticsearch.index.mapper.DocumentMapperForType; @@ -94,6 +95,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu private final String field; private final String documentType; private final BytesReference document; + private final XContentType documentXContentType; private final String indexedDocumentIndex; private final String indexedDocumentType; @@ -102,7 +104,16 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu private final String indexedDocumentPreference; private final Long indexedDocumentVersion; + /** + * @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, document, XContentFactory.xContentType(document)); + } + + public PercolateQueryBuilder(String field, String documentType, BytesReference document, XContentType documentXContentType) { if (field == null) { throw new IllegalArgumentException("[field] is a required argument"); } @@ -115,6 +126,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu this.field = field; this.documentType = documentType; this.document = document; + this.documentXContentType = Objects.requireNonNull(documentXContentType); indexedDocumentIndex = null; indexedDocumentType = null; indexedDocumentId = null; @@ -150,6 +162,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu this.indexedDocumentPreference = indexedDocumentPreference; this.indexedDocumentVersion = indexedDocumentVersion; this.document = null; + this.documentXContentType = null; } /** @@ -170,6 +183,15 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu indexedDocumentVersion = null; } document = in.readOptionalBytesReference(); + if (document != null) { + if (in.getVersion().after(Version.V_5_3_0_UNRELEASED)) { // TODO update to onOrAfter after backporting + documentXContentType = XContentType.readFrom(in); + } else { + documentXContentType = XContentFactory.xContentType(document); + } + } else { + documentXContentType = null; + } } @Override @@ -188,6 +210,9 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu out.writeBoolean(false); } out.writeOptionalBytesReference(document); + if (document != null && out.getVersion().after(Version.V_5_3_0_UNRELEASED)) { // TODO update to onOrAfter after backporting + documentXContentType.writeTo(out); + } } @Override @@ -294,7 +319,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu PercolateQueryBuilder queryBuilder; if (source != null) { - queryBuilder = new PercolateQueryBuilder(field, documentType, source); + queryBuilder = new PercolateQueryBuilder(field, documentType, source, XContentType.JSON); } else if (indexedDocumentId != null) { queryBuilder = new PercolateQueryBuilder(field, documentType, indexedDocumentIndex, indexedDocumentType, indexedDocumentId, indexedDocumentRouting, indexedDocumentPreference, indexedDocumentVersion); @@ -350,7 +375,8 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu "indexed document [" + indexedDocumentIndex + "/" + indexedDocumentType + "/" + indexedDocumentId + "] source disabled" ); } - return new PercolateQueryBuilder(field, documentType, getResponse.getSourceAsBytesRef()); + final BytesReference source = getResponse.getSourceAsBytesRef(); + return new PercolateQueryBuilder(field, documentType, source, XContentFactory.xContentType(source)); } @Override @@ -370,7 +396,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu DocumentMapperForType docMapperForType = mapperService.documentMapperWithAutoCreate(documentType); DocumentMapper docMapper = docMapperForType.getDocumentMapper(); - ParsedDocument doc = docMapper.parse(source(context.index().getName(), documentType, "_temp_id", document)); + ParsedDocument doc = docMapper.parse(source(context.index().getName(), documentType, "_temp_id", document, documentXContentType)); FieldNameAnalyzer fieldNameAnalyzer = (FieldNameAnalyzer) docMapper.mappers().indexAnalyzer(); // Need to this custom impl because FieldNameAnalyzer is strict and the percolator sometimes isn't when @@ -435,6 +461,11 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu return document; } + //pkg-private for testing + XContentType getXContentType() { + return documentXContentType; + } + static IndexSearcher createMultiDocumentSearcher(Analyzer analyzer, ParsedDocument doc) { RAMDirectory ramDirectory = new RAMDirectory(); try (IndexWriter indexWriter = new IndexWriter(ramDirectory, new IndexWriterConfig(analyzer))) { |