-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Display error for text_expansion if the queried field does not have the right type #105581
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
eaa3054
496aa0c
3601fa1
d287e38
c29a17e
df7d7ed
023a5c7
fc6f437
454f3c6
b2f7b0d
f7b821c
d256853
762409d
f4c2c33
91f0a73
6113c8c
d536e07
3fb95e7
cb071b6
ee3233b
8969c31
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,7 +18,6 @@ | |
| import org.elasticsearch.core.Nullable; | ||
| 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.ParseField; | ||
|
|
@@ -32,8 +31,10 @@ | |
| import org.elasticsearch.xpack.core.ml.inference.trainedmodel.TextExpansionConfigUpdate; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| import static org.elasticsearch.xpack.core.ClientHelper.ML_ORIGIN; | ||
| import static org.elasticsearch.xpack.core.ClientHelper.executeAsyncWithOrigin; | ||
|
|
@@ -51,6 +52,34 @@ public class TextExpansionQueryBuilder extends AbstractQueryBuilder<TextExpansio | |
| 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) { | ||
| for (AllowedFieldType fieldType : values()) { | ||
| if (fieldType.getTypeName().equals(typeName)) { | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| 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); | ||
| } | ||
|
|
@@ -198,24 +227,14 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws | |
| } | ||
|
|
||
| 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; | ||
| } | ||
| var boolQuery = QueryBuilders.boolQuery(); | ||
| for (var weightedToken : textExpansionResults.getWeightedTokens()) { | ||
| boolQuery.should(QueryBuilders.termQuery(fieldName, weightedToken.token()).boost(weightedToken.weight())); | ||
| } | ||
| boolQuery.minimumShouldMatch(1); | ||
| boolQuery.boost(this.boost); | ||
| boolQuery.queryName(this.queryName); | ||
| return boolQuery; | ||
| WeightedTokensQueryBuilder weightedTokensQueryBuilder = new WeightedTokensQueryBuilder( | ||
|
||
| fieldName, | ||
| textExpansionResults.getWeightedTokens(), | ||
| tokenPruningConfig | ||
| ); | ||
| weightedTokensQueryBuilder.queryName(queryName); | ||
| weightedTokensQueryBuilder.boost(boost); | ||
| return weightedTokensQueryBuilder; | ||
| } | ||
|
|
||
| @Override | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.