Skip to content

Commit 16d93e5

Browse files
authored
Merge pull request #19877 from areek/fix/remove_completion_payload
Remove payload option from completion suggester
2 parents 90dbce9 + d107141 commit 16d93e5

File tree

10 files changed

+23
-378
lines changed

10 files changed

+23
-378
lines changed

core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggester.java

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,39 +78,16 @@ protected Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Sugges
7878
TopSuggestDocsCollector collector = new TopDocumentsCollector(suggestionContext.getSize());
7979
suggest(searcher, suggestionContext.toQuery(), collector);
8080
int numResult = 0;
81-
List<LeafReaderContext> leaves = searcher.getIndexReader().leaves();
8281
for (TopSuggestDocs.SuggestScoreDoc suggestScoreDoc : collector.get().scoreLookupDocs()) {
8382
TopDocumentsCollector.SuggestDoc suggestDoc = (TopDocumentsCollector.SuggestDoc) suggestScoreDoc;
8483
// collect contexts
8584
Map<String, Set<CharSequence>> contexts = Collections.emptyMap();
8685
if (fieldType.hasContextMappings() && suggestDoc.getContexts().isEmpty() == false) {
8786
contexts = fieldType.getContextMappings().getNamedContexts(suggestDoc.getContexts());
8887
}
89-
// collect payloads
90-
final Map<String, List<Object>> payload = new HashMap<>(0);
91-
List<String> payloadFields = suggestionContext.getPayloadFields();
92-
if (payloadFields.isEmpty() == false) {
93-
final int readerIndex = ReaderUtil.subIndex(suggestDoc.doc, leaves);
94-
final LeafReaderContext subReaderContext = leaves.get(readerIndex);
95-
final int subDocId = suggestDoc.doc - subReaderContext.docBase;
96-
for (String field : payloadFields) {
97-
MapperService mapperService = suggestionContext.getShardContext().getMapperService();
98-
MappedFieldType payloadFieldType = mapperService.fullName(field);
99-
if (payloadFieldType != null) {
100-
QueryShardContext shardContext = suggestionContext.getShardContext();
101-
final AtomicFieldData data = shardContext.getForField(payloadFieldType)
102-
.load(subReaderContext);
103-
final ScriptDocValues scriptValues = data.getScriptValues();
104-
scriptValues.setNextDocId(subDocId);
105-
payload.put(field, new ArrayList<>(scriptValues.getValues()));
106-
} else {
107-
throw new IllegalArgumentException("payload field [" + field + "] does not exist");
108-
}
109-
}
110-
}
11188
if (numResult++ < suggestionContext.getSize()) {
11289
CompletionSuggestion.Entry.Option option = new CompletionSuggestion.Entry.Option(suggestDoc.doc,
113-
new Text(suggestDoc.key.toString()), suggestDoc.score, contexts, payload);
90+
new Text(suggestDoc.key.toString()), suggestDoc.score, contexts);
11491
completionSuggestEntry.addOption(option);
11592
} else {
11693
break;

core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestion.java

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,12 @@ protected Option newOption() {
194194

195195
public static class Option extends Suggest.Suggestion.Entry.Option {
196196
private Map<String, Set<CharSequence>> contexts;
197-
private Map<String, List<Object>> payload;
198197
private ScoreDoc doc;
199198
private InternalSearchHit hit;
200199

201-
public Option(int docID, Text text, float score, Map<String, Set<CharSequence>> contexts, Map<String, List<Object>> payload) {
200+
public Option(int docID, Text text, float score, Map<String, Set<CharSequence>> contexts) {
202201
super(text, score);
203202
this.doc = new ScoreDoc(docID, score);
204-
this.payload = payload;
205203
this.contexts = contexts;
206204
}
207205

@@ -216,10 +214,6 @@ protected void mergeInto(Suggest.Suggestion.Entry.Option otherOption) {
216214
throw new UnsupportedOperationException();
217215
}
218216

219-
public Map<String, List<Object>> getPayload() {
220-
return payload;
221-
}
222-
223217
public Map<String, Set<CharSequence>> getContexts() {
224218
return contexts;
225219
}
@@ -248,17 +242,6 @@ protected XContentBuilder innerToXContent(XContentBuilder builder, Params params
248242
} else {
249243
builder.field("score", getScore());
250244
}
251-
if (payload.size() > 0) {
252-
builder.startObject("payload");
253-
for (Map.Entry<String, List<Object>> entry : payload.entrySet()) {
254-
builder.startArray(entry.getKey());
255-
for (Object payload : entry.getValue()) {
256-
builder.value(payload);
257-
}
258-
builder.endArray();
259-
}
260-
builder.endObject();
261-
}
262245
if (contexts.size() > 0) {
263246
builder.startObject("contexts");
264247
for (Map.Entry<String, Set<CharSequence>> entry : contexts.entrySet()) {
@@ -281,17 +264,6 @@ public void readFrom(StreamInput in) throws IOException {
281264
this.hit = InternalSearchHit.readSearchHit(in,
282265
InternalSearchHits.streamContext().streamShardTarget(ShardTargetType.STREAM));
283266
}
284-
int payloadSize = in.readInt();
285-
this.payload = new LinkedHashMap<>(payloadSize);
286-
for (int i = 0; i < payloadSize; i++) {
287-
String payloadName = in.readString();
288-
int nValues = in.readVInt();
289-
List<Object> values = new ArrayList<>(nValues);
290-
for (int j = 0; j < nValues; j++) {
291-
values.add(in.readGenericValue());
292-
}
293-
this.payload.put(payloadName, values);
294-
}
295267
int contextSize = in.readInt();
296268
this.contexts = new LinkedHashMap<>(contextSize);
297269
for (int i = 0; i < contextSize; i++) {
@@ -315,15 +287,6 @@ public void writeTo(StreamOutput out) throws IOException {
315287
} else {
316288
out.writeBoolean(false);
317289
}
318-
out.writeInt(payload.size());
319-
for (Map.Entry<String, List<Object>> entry : payload.entrySet()) {
320-
out.writeString(entry.getKey());
321-
List<Object> values = entry.getValue();
322-
out.writeVInt(values.size());
323-
for (Object value : values) {
324-
out.writeGenericValue(value);
325-
}
326-
}
327290
out.writeInt(contexts.size());
328291
for (Map.Entry<String, Set<CharSequence>> entry : contexts.entrySet()) {
329292
out.writeString(entry.getKey());
@@ -341,14 +304,6 @@ public String toString() {
341304
stringBuilder.append(getText());
342305
stringBuilder.append(" score:");
343306
stringBuilder.append(getScore());
344-
stringBuilder.append(" payload:[");
345-
for (Map.Entry<String, List<Object>> entry : payload.entrySet()) {
346-
stringBuilder.append(" ");
347-
stringBuilder.append(entry.getKey());
348-
stringBuilder.append(":");
349-
stringBuilder.append(entry.getValue());
350-
}
351-
stringBuilder.append("]");
352307
stringBuilder.append(" context:[");
353308
for (Map.Entry<String, Set<CharSequence>> entry: contexts.entrySet()) {
354309
stringBuilder.append(" ");

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

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
*/
6363
public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSuggestionBuilder> {
6464
static final String SUGGESTION_NAME = "completion";
65-
static final ParseField PAYLOAD_FIELD = new ParseField("payload");
6665
static final ParseField CONTEXTS_FIELD = new ParseField("contexts", "context");
6766

6867
/**
@@ -78,7 +77,6 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug
7877
private static ObjectParser<CompletionSuggestionBuilder.InnerBuilder, ParseFieldMatcherSupplier> TLP_PARSER =
7978
new ObjectParser<>(SUGGESTION_NAME, null);
8079
static {
81-
TLP_PARSER.declareStringArray(CompletionSuggestionBuilder.InnerBuilder::payload, PAYLOAD_FIELD);
8280
TLP_PARSER.declareField((parser, completionSuggestionContext, context) -> {
8381
if (parser.currentToken() == XContentParser.Token.VALUE_BOOLEAN) {
8482
if (parser.booleanValue()) {
@@ -108,7 +106,6 @@ public class CompletionSuggestionBuilder extends SuggestionBuilder<CompletionSug
108106
protected FuzzyOptions fuzzyOptions;
109107
protected RegexOptions regexOptions;
110108
protected BytesReference contextBytes = null;
111-
protected List<String> payloadFields = Collections.emptyList();
112109

113110
public CompletionSuggestionBuilder(String field) {
114111
super(field);
@@ -123,24 +120,20 @@ private CompletionSuggestionBuilder(String fieldname, CompletionSuggestionBuilde
123120
fuzzyOptions = in.fuzzyOptions;
124121
regexOptions = in.regexOptions;
125122
contextBytes = in.contextBytes;
126-
payloadFields = in.payloadFields;
127123
}
128124

129125
/**
130126
* Read from a stream.
131127
*/
132128
public CompletionSuggestionBuilder(StreamInput in) throws IOException {
133129
super(in);
134-
payloadFields = new ArrayList<>();
135-
Collections.addAll(payloadFields, in.readStringArray());
136130
fuzzyOptions = in.readOptionalWriteable(FuzzyOptions::new);
137131
regexOptions = in.readOptionalWriteable(RegexOptions::new);
138132
contextBytes = in.readOptionalBytesReference();
139133
}
140134

141135
@Override
142136
public void doWriteTo(StreamOutput out) throws IOException {
143-
out.writeStringArray(payloadFields.toArray(new String[payloadFields.size()]));
144137
out.writeOptionalWriteable(fuzzyOptions);
145138
out.writeOptionalWriteable(regexOptions);
146139
out.writeOptionalBytesReference(contextBytes);
@@ -194,16 +187,6 @@ public CompletionSuggestionBuilder regex(String regex, RegexOptions regexOptions
194187
return this;
195188
}
196189

197-
/**
198-
* Sets the fields to be returned as suggestion payload.
199-
* Note: Only doc values enabled fields are supported
200-
*/
201-
public CompletionSuggestionBuilder payload(List<String> fields) {
202-
Objects.requireNonNull(fields, "payload must not be null");
203-
this.payloadFields = fields;
204-
return this;
205-
}
206-
207190
/**
208191
* Sets query contexts for completion
209192
* @param queryContexts named query contexts
@@ -348,13 +331,6 @@ private InnerBuilder field(String field) {
348331

349332
@Override
350333
protected XContentBuilder innerToXContent(XContentBuilder builder, Params params) throws IOException {
351-
if (payloadFields.isEmpty() == false) {
352-
builder.startArray(PAYLOAD_FIELD.getPreferredName());
353-
for (String field : payloadFields) {
354-
builder.value(field);
355-
}
356-
builder.endArray();
357-
}
358334
if (fuzzyOptions != null) {
359335
fuzzyOptions.toXContent(builder, params);
360336
}
@@ -388,7 +364,6 @@ public SuggestionContext build(QueryShardContext context) throws IOException {
388364
// copy over common settings to each suggestion builder
389365
final MapperService mapperService = context.getMapperService();
390366
populateCommonFields(mapperService, suggestionContext);
391-
suggestionContext.setPayloadFields(payloadFields);
392367
suggestionContext.setFuzzyOptions(fuzzyOptions);
393368
suggestionContext.setRegexOptions(regexOptions);
394369
MappedFieldType mappedFieldType = mapperService.fullName(suggestionContext.getField());
@@ -449,14 +424,13 @@ public String getWriteableName() {
449424

450425
@Override
451426
protected boolean doEquals(CompletionSuggestionBuilder other) {
452-
return Objects.equals(payloadFields, other.payloadFields) &&
453-
Objects.equals(fuzzyOptions, other.fuzzyOptions) &&
427+
return Objects.equals(fuzzyOptions, other.fuzzyOptions) &&
454428
Objects.equals(regexOptions, other.regexOptions) &&
455429
Objects.equals(contextBytes, other.contextBytes);
456430
}
457431

458432
@Override
459433
protected int doHashCode() {
460-
return Objects.hash(payloadFields, fuzzyOptions, regexOptions, contextBytes);
434+
return Objects.hash(fuzzyOptions, regexOptions, contextBytes);
461435
}
462436
}

core/src/main/java/org/elasticsearch/search/suggest/completion/CompletionSuggestionContext.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ protected CompletionSuggestionContext(QueryShardContext shardContext) {
4545
private FuzzyOptions fuzzyOptions;
4646
private RegexOptions regexOptions;
4747
private Map<String, List<ContextMapping.InternalQueryContext>> queryContexts = Collections.emptyMap();
48-
private List<String> payloadFields = Collections.emptyList();
4948
private CompletionFieldMapper2x.CompletionFieldType fieldType2x;
5049
private List<ContextQuery> contextQueries;
5150

@@ -73,14 +72,6 @@ void setQueryContexts(Map<String, List<ContextMapping.InternalQueryContext>> que
7372
this.queryContexts = queryContexts;
7473
}
7574

76-
void setPayloadFields(List<String> fields) {
77-
this.payloadFields = fields;
78-
}
79-
80-
List<String> getPayloadFields() {
81-
return payloadFields;
82-
}
83-
8475
public FuzzyOptions getFuzzyOptions() {
8576
return fuzzyOptions;
8677
}

core/src/test/java/org/elasticsearch/search/controller/SearchPhaseControllerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private AtomicArray<QuerySearchResultProvider> generateQueryResults(int nShards,
149149
float maxScore = randomIntBetween(suggestion.getSize(), (int) Float.MAX_VALUE);
150150
for (int i = 0; i < optionSize; i++) {
151151
completionEntry.addOption(new CompletionSuggestion.Entry.Option(i, new Text(""), maxScore,
152-
Collections.emptyMap(), Collections.emptyMap()));
152+
Collections.emptyMap()));
153153
float dec = randomIntBetween(0, optionSize);
154154
if (dec <= maxScore) {
155155
maxScore -= dec;

0 commit comments

Comments
 (0)