forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextExpansionQueryBuilder.java
341 lines (304 loc) · 15 KB
/
TextExpansionQueryBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
package org.elasticsearch.xpack.ml.queries;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.SetOnce;
import org.elasticsearch.TransportVersion;
import org.elasticsearch.TransportVersions;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.common.ParsingException;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.core.TimeValue;
import org.elasticsearch.index.query.AbstractQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.QueryRewriteContext;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.xcontent.ObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xpack.core.ml.action.CoordinatedInferenceAction;
import org.elasticsearch.xpack.core.ml.action.InferModelAction;
import org.elasticsearch.xpack.core.ml.inference.TrainedModelPrefixStrings;
import org.elasticsearch.xpack.core.ml.inference.results.TextExpansionResults;
import org.elasticsearch.xpack.core.ml.inference.results.WarningInferenceResults;
import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TextExpansionConfigUpdate;
import org.elasticsearch.xpack.core.termsenum.action.TermsEnumRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.Collectors;
import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN;
import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin;
import static org.elasticsearch.xpack.core.ml.action.InferModelAction.Request.TIMEOUT;
public class TextExpansionQueryBuilder extends AbstractQueryBuilder<TextExpansionQueryBuilder> {
public static final String NAME = "text_expansion";
public static final ParseField PRUNING_CONFIG = new ParseField("pruning_config");
public static final ParseField MODEL_TEXT = new ParseField("model_text");
public static final ParseField MODEL_ID = new ParseField("model_id");
private final String fieldName;
private final String modelText;
private final String modelId;
private SetOnce<TextExpansionResults> weightedTokensSupplier;
private final TokenPruningConfig tokenPruningConfig;
public enum AllowedFieldType {
RANK_FEATURES("rank_features"),
SPARSE_VECTOR("sparse_vector");
private final String typeName;
AllowedFieldType(String typeName) {
this.typeName = typeName;
}
public String getTypeName() {
return typeName;
}
public static boolean isFieldTypeAllowed(String typeName) {
return Arrays.stream(values()).anyMatch(value -> value.typeName.equals(typeName));
}
public static String getAllowedFieldTypesAsString() {
return Arrays.stream(values()).map(value -> value.typeName).collect(Collectors.joining(", "));
}
}
public TextExpansionQueryBuilder(String fieldName, String modelText, String modelId) {
this(fieldName, modelText, modelId, null);
}
public TextExpansionQueryBuilder(String fieldName, String modelText, String modelId, @Nullable TokenPruningConfig tokenPruningConfig) {
if (fieldName == null) {
throw new IllegalArgumentException("[" + NAME + "] requires a fieldName");
}
if (modelText == null) {
throw new IllegalArgumentException("[" + NAME + "] requires a " + MODEL_TEXT.getPreferredName() + " value");
}
if (modelId == null) {
throw new IllegalArgumentException("[" + NAME + "] requires a " + MODEL_ID.getPreferredName() + " value");
}
this.fieldName = fieldName;
this.modelText = modelText;
this.modelId = modelId;
this.tokenPruningConfig = tokenPruningConfig;
}
public TextExpansionQueryBuilder(StreamInput in) throws IOException {
super(in);
this.fieldName = in.readString();
this.modelText = in.readString();
this.modelId = in.readString();
if (in.getTransportVersion().onOrAfter(TransportVersions.TEXT_EXPANSION_TOKEN_PRUNING_CONFIG_ADDED)) {
this.tokenPruningConfig = in.readOptionalWriteable(TokenPruningConfig::new);
} else {
this.tokenPruningConfig = null;
}
}
private TextExpansionQueryBuilder(TextExpansionQueryBuilder other, SetOnce<TextExpansionResults> weightedTokensSupplier) {
this.fieldName = other.fieldName;
this.modelText = other.modelText;
this.modelId = other.modelId;
this.tokenPruningConfig = other.tokenPruningConfig;
this.boost = other.boost;
this.queryName = other.queryName;
this.weightedTokensSupplier = weightedTokensSupplier;
}
String getFieldName() {
return fieldName;
}
public TokenPruningConfig getTokenPruningConfig() {
return tokenPruningConfig;
}
@Override
public String getWriteableName() {
return NAME;
}
@Override
public TransportVersion getMinimalSupportedVersion() {
return TransportVersions.V_8_8_0;
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
if (weightedTokensSupplier != null) {
throw new IllegalStateException("token supplier must be null, can't serialize suppliers, missing a rewriteAndFetch?");
}
out.writeString(fieldName);
out.writeString(modelText);
out.writeString(modelId);
if (out.getTransportVersion().onOrAfter(TransportVersions.TEXT_EXPANSION_TOKEN_PRUNING_CONFIG_ADDED)) {
out.writeOptionalWriteable(tokenPruningConfig);
}
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
builder.startObject(fieldName);
builder.field(MODEL_TEXT.getPreferredName(), modelText);
builder.field(MODEL_ID.getPreferredName(), modelId);
if (tokenPruningConfig != null) {
builder.field(PRUNING_CONFIG.getPreferredName(), tokenPruningConfig);
}
boostAndQueryNameToXContent(builder);
builder.endObject();
builder.endObject();
}
@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) {
if (weightedTokensSupplier != null) {
if (weightedTokensSupplier.get() == null) {
return this;
}
return weightedTokensToQuery(fieldName, weightedTokensSupplier.get());
}
CoordinatedInferenceAction.Request inferRequest = CoordinatedInferenceAction.Request.forTextInput(
modelId,
List.of(modelText),
TextExpansionConfigUpdate.EMPTY_UPDATE,
false,
queryRewriteContext.timeout().orElse(InferModelAction.Request.DEFAULT_TIMEOUT_FOR_API)
);
inferRequest.setHighPriority(true);
inferRequest.setPrefixType(TrainedModelPrefixStrings.PrefixType.SEARCH);
SetOnce<TextExpansionResults> textExpansionResultsSupplier = new SetOnce<>();
queryRewriteContext.registerAsyncAction(
(client, listener) -> executeAsyncWithOrigin(
client,
ML_ORIGIN,
CoordinatedInferenceAction.INSTANCE,
inferRequest,
ActionListener.wrap(inferenceResponse -> {
if (inferenceResponse.getInferenceResults().isEmpty()) {
listener.onFailure(new IllegalStateException("inference response contain no results"));
return;
}
if (inferenceResponse.getInferenceResults().get(0) instanceof TextExpansionResults textExpansionResults) {
textExpansionResultsSupplier.set(textExpansionResults);
listener.onResponse(null);
} else if (inferenceResponse.getInferenceResults().get(0) instanceof WarningInferenceResults warning) {
listener.onFailure(new IllegalStateException(warning.getWarning()));
} else {
listener.onFailure(
new IllegalStateException(
"expected a result of type ["
+ TextExpansionResults.NAME
+ "] received ["
+ inferenceResponse.getInferenceResults().get(0).getWriteableName()
+ "]. Is ["
+ modelId
+ "] a compatible model?"
)
);
}
}, listener::onFailure)
)
);
return new TextExpansionQueryBuilder(this, textExpansionResultsSupplier);
}
private QueryBuilder weightedTokensToQuery(String fieldName, TextExpansionResults textExpansionResults) {
if (tokenPruningConfig != null) {
WeightedTokensQueryBuilder weightedTokensQueryBuilder = new WeightedTokensQueryBuilder(
fieldName,
textExpansionResults.getWeightedTokens(),
tokenPruningConfig
);
weightedTokensQueryBuilder.queryName(queryName);
weightedTokensQueryBuilder.boost(boost);
return weightedTokensQueryBuilder;
}
// Note: Weighted tokens queries were introduced in 8.13.0. To support mixed version clusters prior to 8.13.0,
// if no token pruning configuration is specified we fall back to a boolean query.
// TODO this should be updated to always use a WeightedTokensQueryBuilder once it's in all supported versions.
var boolQuery = QueryBuilders.boolQuery();
for (var weightedToken : textExpansionResults.getWeightedTokens()) {
boolQuery.should(QueryBuilders.termQuery(fieldName, weightedToken.token()).boost(weightedToken.weight()));
}
boolQuery.minimumShouldMatch(1);
boolQuery.boost(boost);
boolQuery.queryName(queryName);
return boolQuery;
}
@Override
protected Query doToQuery(SearchExecutionContext context) {
throw new IllegalStateException("text_expansion should have been rewritten to another query type");
}
@Override
protected boolean doEquals(TextExpansionQueryBuilder other) {
return Objects.equals(fieldName, other.fieldName)
&& Objects.equals(modelText, other.modelText)
&& Objects.equals(modelId, other.modelId)
&& Objects.equals(tokenPruningConfig, other.tokenPruningConfig)
&& Objects.equals(weightedTokensSupplier, other.weightedTokensSupplier);
}
@Override
protected int doHashCode() {
return Objects.hash(fieldName, modelText, modelId, tokenPruningConfig, weightedTokensSupplier);
}
public static TextExpansionQueryBuilder fromXContent(XContentParser parser) throws IOException {
String fieldName = null;
String modelText = null;
String modelId = null;
TokenPruningConfig tokenPruningConfig = null;
float boost = AbstractQueryBuilder.DEFAULT_BOOST;
String queryName = null;
String currentFieldName = null;
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
fieldName = currentFieldName;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token == XContentParser.Token.START_OBJECT) {
if (PRUNING_CONFIG.match(currentFieldName, parser.getDeprecationHandler())) {
tokenPruningConfig = TokenPruningConfig.fromXContent(parser);
} else {
throw new ParsingException(
parser.getTokenLocation(),
"[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]"
);
}
} else if (token.isValue()) {
if (MODEL_TEXT.match(currentFieldName, parser.getDeprecationHandler())) {
modelText = parser.text();
} else if (MODEL_ID.match(currentFieldName, parser.getDeprecationHandler())) {
modelId = parser.text();
} else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
boost = parser.floatValue();
} else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
queryName = parser.text();
} else {
throw new ParsingException(
parser.getTokenLocation(),
"[" + NAME + "] query does not support [" + currentFieldName + "]"
);
}
} else {
throw new ParsingException(
parser.getTokenLocation(),
"[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]"
);
}
}
} else {
throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
fieldName = parser.currentName();
modelText = parser.text();
}
}
if (modelText == null) {
throw new ParsingException(parser.getTokenLocation(), "No text specified for text query");
}
if (fieldName == null) {
throw new ParsingException(parser.getTokenLocation(), "No fieldname specified for query");
}
TextExpansionQueryBuilder queryBuilder = new TextExpansionQueryBuilder(fieldName, modelText, modelId, tokenPruningConfig);
queryBuilder.queryName(queryName);
queryBuilder.boost(boost);
return queryBuilder;
}
}