Skip to content

Commit a977569

Browse files
committed
percolator: Deprecate document_type parameter.
The `document_type` parameter is no longer required to be specified, because by default from 6.0 only a single type is allowed. (`index.mapping.single_type` defaults to `true`)
1 parent 0b0390a commit a977569

File tree

12 files changed

+159
-112
lines changed

12 files changed

+159
-112
lines changed

docs/reference/migration/migrate_6_0/search.asciidoc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353

5454
* The `template` query has been removed. This query was deprecated since 5.0
5555

56+
* The `percolate` query's `document_type` has been deprecated. From 6.0 and later
57+
it is no longer required to specify the `document_type` parameter.
58+
5659
==== Search shards API
5760

5861
The search shards API no longer accepts the `type` url parameter, which didn't

docs/reference/query-dsl/percolate-query.asciidoc

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@ GET /my-index/_search
6565
"query" : {
6666
"percolate" : {
6767
"field" : "query",
68-
"document_type" : "doc",
6968
"document" : {
7069
"message" : "A new bonsai tree in the office"
7170
}
@@ -190,7 +189,6 @@ GET /my-index/_search
190189
"query" : {
191190
"percolate" : {
192191
"field": "query",
193-
"document_type" : "doc",
194192
"index" : "my-index",
195193
"type" : "doc",
196194
"id" : "2",
@@ -261,7 +259,6 @@ GET /my-index/_search
261259
"query" : {
262260
"percolate" : {
263261
"field": "query",
264-
"document_type" : "doc",
265262
"document" : {
266263
"message" : "The quick brown fox jumps over the lazy dog"
267264
}

modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQuery.java

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,14 @@ final class PercolateQuery extends Query implements Accountable {
4646
// cost of matching the query against the document, arbitrary as it would be really complex to estimate
4747
public static final float MATCH_COST = 1000;
4848

49-
private final String documentType;
5049
private final QueryStore queryStore;
5150
private final BytesReference documentSource;
5251
private final Query candidateMatchesQuery;
5352
private final Query verifiedMatchesQuery;
5453
private final IndexSearcher percolatorIndexSearcher;
5554

56-
PercolateQuery(String documentType, QueryStore queryStore, BytesReference documentSource,
57-
Query candidateMatchesQuery, IndexSearcher percolatorIndexSearcher, Query verifiedMatchesQuery) {
58-
this.documentType = Objects.requireNonNull(documentType);
55+
PercolateQuery(QueryStore queryStore, BytesReference documentSource,
56+
Query candidateMatchesQuery, IndexSearcher percolatorIndexSearcher, Query verifiedMatchesQuery) {
5957
this.documentSource = Objects.requireNonNull(documentSource);
6058
this.candidateMatchesQuery = Objects.requireNonNull(candidateMatchesQuery);
6159
this.queryStore = Objects.requireNonNull(queryStore);
@@ -67,8 +65,7 @@ final class PercolateQuery extends Query implements Accountable {
6765
public Query rewrite(IndexReader reader) throws IOException {
6866
Query rewritten = candidateMatchesQuery.rewrite(reader);
6967
if (rewritten != candidateMatchesQuery) {
70-
return new PercolateQuery(documentType, queryStore, documentSource, rewritten, percolatorIndexSearcher,
71-
verifiedMatchesQuery);
68+
return new PercolateQuery(queryStore, documentSource, rewritten, percolatorIndexSearcher, verifiedMatchesQuery);
7269
} else {
7370
return this;
7471
}
@@ -171,10 +168,6 @@ public IndexSearcher getPercolatorIndexSearcher() {
171168
return percolatorIndexSearcher;
172169
}
173170

174-
public String getDocumentType() {
175-
return documentType;
176-
}
177-
178171
public BytesReference getDocumentSource() {
179172
return documentSource;
180173
}
@@ -200,8 +193,8 @@ public int hashCode() {
200193

201194
@Override
202195
public String toString(String s) {
203-
return "PercolateQuery{document_type={" + documentType + "},document_source={" + documentSource.utf8ToString() +
204-
"},inner={" + candidateMatchesQuery.toString(s) + "}}";
196+
return "PercolateQuery{document_source={" + documentSource.utf8ToString() + "},inner={" +
197+
candidateMatchesQuery.toString(s) + "}}";
205198
}
206199

207200
@Override

modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java

Lines changed: 89 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
import org.elasticsearch.common.bytes.BytesReference;
5151
import org.elasticsearch.common.io.stream.StreamInput;
5252
import org.elasticsearch.common.io.stream.StreamOutput;
53+
import org.elasticsearch.common.logging.DeprecationLogger;
54+
import org.elasticsearch.common.logging.Loggers;
5355
import org.elasticsearch.common.lucene.search.Queries;
5456
import org.elasticsearch.common.xcontent.XContent;
5557
import org.elasticsearch.common.xcontent.XContentBuilder;
@@ -74,6 +76,7 @@
7476
import org.elasticsearch.indices.breaker.NoneCircuitBreakerService;
7577

7678
import java.io.IOException;
79+
import java.util.Collection;
7780
import java.util.Objects;
7881

7982
import static org.elasticsearch.index.mapper.SourceToParse.source;
@@ -82,6 +85,8 @@
8285
public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBuilder> {
8386
public static final String NAME = "percolate";
8487

88+
private static final DeprecationLogger DEPRECATION_LOGGER = new DeprecationLogger(Loggers.getLogger(ParseField.class));
89+
8590
static final ParseField DOCUMENT_FIELD = new ParseField("document");
8691
private static final ParseField QUERY_FIELD = new ParseField("field");
8792
private static final ParseField DOCUMENT_TYPE_FIELD = new ParseField("document_type");
@@ -93,6 +98,7 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
9398
private static final ParseField INDEXED_DOCUMENT_FIELD_VERSION = new ParseField("version");
9499

95100
private final String field;
101+
@Deprecated
96102
private final String documentType;
97103
private final BytesReference document;
98104
private final XContentType documentXContentType;
@@ -105,21 +111,30 @@ public class PercolateQueryBuilder extends AbstractQueryBuilder<PercolateQueryBu
105111
private final Long indexedDocumentVersion;
106112

107113
/**
108-
* @deprecated use {@link #PercolateQueryBuilder(String, String, BytesReference, XContentType)} with the document content type to avoid
109-
* autodetection
114+
* @deprecated use {@link #PercolateQueryBuilder(String, BytesReference, XContentType)} with the document content
115+
* type to avoid autodetection.
110116
*/
111117
@Deprecated
112118
public PercolateQueryBuilder(String field, String documentType, BytesReference document) {
113119
this(field, documentType, document, XContentFactory.xContentType(document));
114120
}
115121

122+
/**
123+
* Creates a percolator query builder instance for percolating a provided document.
124+
*
125+
* @param field The field that contains the percolator query
126+
* @param document The binary blob containing document to percolate
127+
* @param documentXContentType The content type of the binary blob containing the document to percolate
128+
*/
129+
public PercolateQueryBuilder(String field, BytesReference document, XContentType documentXContentType) {
130+
this(field, null, document, documentXContentType);
131+
}
132+
133+
@Deprecated
116134
public PercolateQueryBuilder(String field, String documentType, BytesReference document, XContentType documentXContentType) {
117135
if (field == null) {
118136
throw new IllegalArgumentException("[field] is a required argument");
119137
}
120-
if (documentType == null) {
121-
throw new IllegalArgumentException("[document_type] is a required argument");
122-
}
123138
if (document == null) {
124139
throw new IllegalArgumentException("[document] is a required argument");
125140
}
@@ -135,15 +150,30 @@ public PercolateQueryBuilder(String field, String documentType, BytesReference d
135150
indexedDocumentVersion = null;
136151
}
137152

138-
public PercolateQueryBuilder(String field, String documentType, String indexedDocumentIndex, String indexedDocumentType,
139-
String indexedDocumentId, String indexedDocumentRouting, String indexedDocumentPreference,
140-
Long indexedDocumentVersion) {
153+
/**
154+
* Creates a percolator query builder instance for percolating a document in a remote index.
155+
*
156+
* @param field The field that contains the percolator query
157+
* @param indexedDocumentIndex The index containing the document to percolate
158+
* @param indexedDocumentType The type containing the document to percolate
159+
* @param indexedDocumentId The id of the document to percolate
160+
* @param indexedDocumentRouting The routing value for the document to percolate
161+
* @param indexedDocumentPreference The preference to use when fetching the document to percolate
162+
* @param indexedDocumentVersion The expected version of the document to percolate
163+
*/
164+
public PercolateQueryBuilder(String field, String indexedDocumentIndex, String indexedDocumentType, String indexedDocumentId,
165+
String indexedDocumentRouting, String indexedDocumentPreference, Long indexedDocumentVersion) {
166+
this(field, null, indexedDocumentIndex, indexedDocumentType, indexedDocumentId, indexedDocumentRouting,
167+
indexedDocumentPreference, indexedDocumentVersion);
168+
}
169+
170+
@Deprecated
171+
public PercolateQueryBuilder(String field, String documentType, String indexedDocumentIndex,
172+
String indexedDocumentType, String indexedDocumentId, String indexedDocumentRouting,
173+
String indexedDocumentPreference, Long indexedDocumentVersion) {
141174
if (field == null) {
142175
throw new IllegalArgumentException("[field] is a required argument");
143176
}
144-
if (documentType == null) {
145-
throw new IllegalArgumentException("[document_type] is a required argument");
146-
}
147177
if (indexedDocumentIndex == null) {
148178
throw new IllegalArgumentException("[index] is a required argument");
149179
}
@@ -171,7 +201,11 @@ public PercolateQueryBuilder(String field, String documentType, String indexedDo
171201
PercolateQueryBuilder(StreamInput in) throws IOException {
172202
super(in);
173203
field = in.readString();
174-
documentType = in.readString();
204+
if (in.getVersion().before(Version.V_6_0_0_alpha3)) {
205+
documentType = in.readString();
206+
} else {
207+
documentType = in.readOptionalString();
208+
}
175209
indexedDocumentIndex = in.readOptionalString();
176210
indexedDocumentType = in.readOptionalString();
177211
indexedDocumentId = in.readOptionalString();
@@ -197,7 +231,11 @@ public PercolateQueryBuilder(String field, String documentType, String indexedDo
197231
@Override
198232
protected void doWriteTo(StreamOutput out) throws IOException {
199233
out.writeString(field);
200-
out.writeString(documentType);
234+
if (out.getVersion().before(Version.V_6_0_0_alpha3)) {
235+
out.writeString(documentType);
236+
} else {
237+
out.writeOptionalString(documentType);
238+
}
201239
out.writeOptionalString(indexedDocumentIndex);
202240
out.writeOptionalString(indexedDocumentType);
203241
out.writeOptionalString(indexedDocumentId);
@@ -281,11 +319,11 @@ public static PercolateQueryBuilder fromXContent(QueryParseContext parseContext)
281319
throw new ParsingException(parser.getTokenLocation(), "[" + PercolateQueryBuilder.NAME +
282320
"] query does not support [" + token + "]");
283321
}
284-
} else if (token.isValue()) {
322+
} else if (token.isValue() || token == XContentParser.Token.VALUE_NULL) {
285323
if (QUERY_FIELD.match(currentFieldName)) {
286324
field = parser.text();
287325
} else if (DOCUMENT_TYPE_FIELD.match(currentFieldName)) {
288-
documentType = parser.text();
326+
documentType = parser.textOrNull();
289327
} else if (INDEXED_DOCUMENT_FIELD_INDEX.match(currentFieldName)) {
290328
indexedDocumentIndex = parser.text();
291329
} else if (INDEXED_DOCUMENT_FIELD_TYPE.match(currentFieldName)) {
@@ -312,11 +350,6 @@ public static PercolateQueryBuilder fromXContent(QueryParseContext parseContext)
312350
}
313351
}
314352

315-
if (documentType == null) {
316-
throw new IllegalArgumentException("[" + PercolateQueryBuilder.NAME + "] query is missing required [" +
317-
DOCUMENT_TYPE_FIELD.getPreferredName() + "] parameter");
318-
}
319-
320353
PercolateQueryBuilder queryBuilder;
321354
if (source != null) {
322355
queryBuilder = new PercolateQueryBuilder(field, documentType, source, XContentType.JSON);
@@ -392,11 +425,42 @@ protected Query doToQuery(QueryShardContext context) throws IOException {
392425
throw new IllegalStateException("no document to percolate");
393426
}
394427

395-
MapperService mapperService = context.getMapperService();
396-
DocumentMapperForType docMapperForType = mapperService.documentMapperWithAutoCreate(documentType);
397-
DocumentMapper docMapper = docMapperForType.getDocumentMapper();
428+
MappedFieldType fieldType = context.fieldMapper(field);
429+
if (fieldType == null) {
430+
throw new QueryShardException(context, "field [" + field + "] does not exist");
431+
}
398432

399-
ParsedDocument doc = docMapper.parse(source(context.index().getName(), documentType, "_temp_id", document, documentXContentType));
433+
if (!(fieldType instanceof PercolatorFieldMapper.FieldType)) {
434+
throw new QueryShardException(context, "expected field [" + field +
435+
"] to be of type [percolator], but is of type [" + fieldType.typeName() + "]");
436+
}
437+
438+
final ParsedDocument doc;
439+
final DocumentMapper docMapper;
440+
final MapperService mapperService = context.getMapperService();
441+
if (context.getIndexSettings().isSingleType()) {
442+
Collection<String> types = mapperService.types();
443+
if (types.size() != 1) {
444+
throw new IllegalStateException("Only a single type should exist, but [" + types.size() + " types exists");
445+
}
446+
String type = types.iterator().next();
447+
if (documentType != null) {
448+
DEPRECATION_LOGGER.deprecated("[document_type] parameter has been deprecated because types have been deprecated");
449+
if (documentType.equals(type) == false) {
450+
throw new IllegalArgumentException("specified document_type [" + documentType +
451+
"] is not equal to the actual type [" + type + "]");
452+
}
453+
}
454+
docMapper = mapperService.documentMapper(type);
455+
doc = docMapper.parse(source(context.index().getName(), type, "_temp_id", document, documentXContentType));
456+
} else {
457+
if (documentType == null) {
458+
throw new IllegalArgumentException("[percolate] query is missing required [document_type] parameter");
459+
}
460+
DocumentMapperForType docMapperForType = mapperService.documentMapperWithAutoCreate(documentType);
461+
docMapper = docMapperForType.getDocumentMapper();
462+
doc = docMapper.parse(source(context.index().getName(), documentType, "_temp_id", document, documentXContentType));
463+
}
400464

401465
FieldNameAnalyzer fieldNameAnalyzer = (FieldNameAnalyzer) docMapper.mappers().indexAnalyzer();
402466
// Need to this custom impl because FieldNameAnalyzer is strict and the percolator sometimes isn't when
@@ -425,18 +489,10 @@ protected Analyzer getWrappedAnalyzer(String fieldName) {
425489
boolean mapUnmappedFieldsAsString = context.getIndexSettings()
426490
.getValue(PercolatorFieldMapper.INDEX_MAP_UNMAPPED_FIELDS_AS_STRING_SETTING);
427491
QueryShardContext percolateShardContext = wrap(context);
428-
MappedFieldType fieldType = context.fieldMapper(field);
429-
if (fieldType == null) {
430-
throw new QueryShardException(context, "field [" + field + "] does not exist");
431-
}
432492

433-
if (!(fieldType instanceof PercolatorFieldMapper.FieldType)) {
434-
throw new QueryShardException(context, "expected field [" + field +
435-
"] to be of type [percolator], but is of type [" + fieldType.typeName() + "]");
436-
}
437493
PercolatorFieldMapper.FieldType pft = (PercolatorFieldMapper.FieldType) fieldType;
438494
PercolateQuery.QueryStore queryStore = createStore(pft, percolateShardContext, mapUnmappedFieldsAsString);
439-
return pft.percolateQuery(documentType, queryStore, document, docSearcher);
495+
return pft.percolateQuery(queryStore, document, docSearcher);
440496
}
441497

442498
public String getField() {

modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorFieldMapper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ public Query termQuery(Object value, QueryShardContext context) {
176176
throw new QueryShardException(context, "Percolator fields are not searchable directly, use a percolate query instead");
177177
}
178178

179-
public Query percolateQuery(String documentType, PercolateQuery.QueryStore queryStore, BytesReference documentSource,
180-
IndexSearcher searcher) throws IOException {
179+
Query percolateQuery(PercolateQuery.QueryStore queryStore, BytesReference documentSource,
180+
IndexSearcher searcher) throws IOException {
181181
IndexReader indexReader = searcher.getIndexReader();
182182
Query candidateMatchesQuery = createCandidateQuery(indexReader);
183183
Query verifiedMatchesQuery;
@@ -190,7 +190,7 @@ public Query percolateQuery(String documentType, PercolateQuery.QueryStore query
190190
} else {
191191
verifiedMatchesQuery = new MatchNoDocsQuery("nested docs, so no verified matches");
192192
}
193-
return new PercolateQuery(documentType, queryStore, documentSource, candidateMatchesQuery, searcher, verifiedMatchesQuery);
193+
return new PercolateQuery(queryStore, documentSource, candidateMatchesQuery, searcher, verifiedMatchesQuery);
194194
}
195195

196196
Query createCandidateQuery(IndexReader indexReader) throws IOException {

modules/percolator/src/main/java/org/elasticsearch/percolator/PercolatorHighlightSubFetchPhase.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@
3333
import org.elasticsearch.common.settings.Settings;
3434
import org.elasticsearch.common.text.Text;
3535
import org.elasticsearch.index.query.ParsedQuery;
36+
import org.elasticsearch.search.SearchHit;
3637
import org.elasticsearch.search.fetch.FetchSubPhase;
3738
import org.elasticsearch.search.fetch.subphase.highlight.HighlightPhase;
3839
import org.elasticsearch.search.fetch.subphase.highlight.Highlighter;
3940
import org.elasticsearch.search.fetch.subphase.highlight.SearchContextHighlight;
40-
import org.elasticsearch.search.SearchHit;
4141
import org.elasticsearch.search.internal.SearchContext;
4242
import org.elasticsearch.search.internal.SubSearchContext;
4343

@@ -52,7 +52,7 @@
5252
*/
5353
public final class PercolatorHighlightSubFetchPhase extends HighlightPhase {
5454

55-
public PercolatorHighlightSubFetchPhase(Settings settings, Map<String, Highlighter> highlighters) {
55+
PercolatorHighlightSubFetchPhase(Settings settings, Map<String, Highlighter> highlighters) {
5656
super(settings, highlighters);
5757
}
5858

@@ -93,7 +93,7 @@ public void hitsExecute(SearchContext context, SearchHit[] hits) {
9393
if (query != null) {
9494
subSearchContext.parsedQuery(new ParsedQuery(query));
9595
hitContext.reset(
96-
new SearchHit(0, "unknown", new Text(percolateQuery.getDocumentType()), Collections.emptyMap()),
96+
new SearchHit(0, "unknown", new Text(hit.getType()), Collections.emptyMap()),
9797
percolatorLeafReaderContext, 0, percolatorIndexSearcher
9898
);
9999
hitContext.cache().clear();

0 commit comments

Comments
 (0)