Skip to content

Commit e20f8d0

Browse files
committed
Deprecates completion query without context
This change deprecates completion queries without context that target a context enabled completion field. Querying without context degrades the search performance considerably (even when the number of indexed contexts is low). This commit targets master but the deprecation will take place in 6.x and the functionality will be removed in 7 in a follow up. Closes elastic#29222
1 parent b5a793b commit e20f8d0

File tree

3 files changed

+53
-10
lines changed

3 files changed

+53
-10
lines changed

docs/reference/search/suggesters/context-suggest.asciidoc

+3-3
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,9 @@ POST place/_search?pretty
156156
// CONSOLE
157157
// TEST[continued]
158158

159-
NOTE: When no categories are provided at query-time, all indexed documents are considered.
160-
Querying with no categories on a category enabled completion field should be avoided, as it
161-
will degrade search performance.
159+
Note: deprecated[7.0.0, When no categories are provided at query-time, all indexed documents are considered.
160+
Querying with no categories on a category enabled completion field is deprecated and will be removed in the next major release
161+
as it degrades search performance considerably.]
162162

163163
Suggestions with certain categories can be boosted higher than others.
164164
The following filters suggestions by categories and additionally boosts

rest-api-spec/src/main/resources/rest-api-spec/test/suggest/30_context.yml

+34
Original file line numberDiff line numberDiff line change
@@ -349,3 +349,37 @@ setup:
349349
- length: { suggest.result: 1 }
350350
- length: { suggest.result.0.options: 1 }
351351
- match: { suggest.result.0.options.0.text: "foo" }
352+
353+
---
354+
"Querying without contexts is deprecated":
355+
- skip:
356+
version: " - 6.99.99"
357+
reason: this feature was deprecated in 7.0
358+
features: "warnings"
359+
360+
- do:
361+
index:
362+
index: test
363+
type: test
364+
id: 1
365+
body:
366+
suggest_context:
367+
input: "foo"
368+
contexts:
369+
color: "red"
370+
371+
- do:
372+
indices.refresh: {}
373+
374+
- do:
375+
warnings:
376+
- "The ability to query with no context on a context enabled completion field is deprecated and will be removed in the next major release."
377+
search:
378+
body:
379+
suggest:
380+
result:
381+
text: "foo"
382+
completion:
383+
field: suggest_context
384+
385+
- length: { suggest.result: 1 }

server/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionBuilder.java

+16-7
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import org.elasticsearch.common.bytes.BytesReference;
2525
import org.elasticsearch.common.io.stream.StreamInput;
2626
import org.elasticsearch.common.io.stream.StreamOutput;
27+
import org.elasticsearch.common.logging.DeprecationLogger;
28+
import org.elasticsearch.common.logging.Loggers;
2729
import org.elasticsearch.common.unit.Fuzziness;
2830
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
2931
import org.elasticsearch.common.xcontent.NamedXContentRegistry;
@@ -57,6 +59,10 @@
5759
* indexing.
5860
*/
5961
public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSuggestionBuilder> {
62+
63+
private static final DeprecationLogger DEPRECATION_LOGGER =
64+
new DeprecationLogger(Loggers.getLogger(CompletionSuggestionBuilder.class));
65+
6066
private static final XContentType CONTEXT_BYTES_XCONTENT_TYPE = XContentType.JSON;
6167
static final String SUGGESTION_NAME = "completion";
6268
static final ParseField CONTEXTS_FIELD = new ParseField("contexts", "context");
@@ -298,16 +304,19 @@ public SuggestionContext build(QueryShardContext context) throws IOException {
298304
if (mappedFieldType == null || mappedFieldType instanceof CompletionFieldMapper.CompletionFieldType == false) {
299305
throw new IllegalArgumentException("Field [" + suggestionContext.getField() + "] is not a completion suggest field");
300306
}
301-
if (mappedFieldType instanceof CompletionFieldMapper.CompletionFieldType) {
302-
CompletionFieldMapper.CompletionFieldType type = (CompletionFieldMapper.CompletionFieldType) mappedFieldType;
303-
suggestionContext.setFieldType(type);
304-
if (type.hasContextMappings() && contextBytes != null) {
307+
CompletionFieldMapper.CompletionFieldType type = (CompletionFieldMapper.CompletionFieldType) mappedFieldType;
308+
suggestionContext.setFieldType(type);
309+
if (type.hasContextMappings()) {
310+
if (contextBytes == null) {
311+
DEPRECATION_LOGGER.deprecated("The ability to query with no context on a context enabled completion field is deprecated " +
312+
"and will be removed in the next major release.");
313+
} else {
305314
Map<String, List<ContextMapping.InternalQueryContext>> queryContexts = parseContextBytes(contextBytes,
306-
context.getXContentRegistry(), type.getContextMappings());
315+
context.getXContentRegistry(), type.getContextMappings());
307316
suggestionContext.setQueryContexts(queryContexts);
308-
} else if (contextBytes != null) {
309-
throw new IllegalArgumentException("suggester [" + type.name() + "] doesn't expect any context");
310317
}
318+
} else if (contextBytes != null) {
319+
throw new IllegalArgumentException("suggester [" + type.name() + "] doesn't expect any context");
311320
}
312321
assert suggestionContext.getFieldType() != null : "no completion field type set";
313322
return suggestionContext;

0 commit comments

Comments
 (0)