Skip to content

Commit 34c7cbb

Browse files
committed
Add support for match types in match function
1 parent 32aaacb commit 34c7cbb

File tree

4 files changed

+70
-12
lines changed

4 files changed

+70
-12
lines changed

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/FullTextFunction.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ protected final TypeResolution resolveType() {
5555
*
5656
* @return type resolution for query parameter
5757
*/
58-
private TypeResolution resolveQueryParamType() {
58+
protected TypeResolution resolveQueryParamType() {
5959
return isString(query(), sourceText(), queryParamOrdinal()).and(isNotNullAndFoldable(query(), sourceText(), queryParamOrdinal()));
6060
}
6161

@@ -88,6 +88,20 @@ public final String queryAsText() {
8888
);
8989
}
9090

91+
/**
92+
* Returns the resulting query as a String
93+
*
94+
* @return query expression as a string
95+
*/
96+
public final Object queryAsObject() {
97+
Object queryAsObject = query().fold();
98+
if (queryAsObject instanceof BytesRef bytesRef) {
99+
return bytesRef.utf8ToString();
100+
}
101+
102+
return queryAsObject;
103+
}
104+
91105
/**
92106
* Returns the param ordinal for the query parameter so it can be used in error messages
93107
*

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/fulltext/Match.java

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.elasticsearch.xpack.esql.core.querydsl.query.QueryStringQuery;
2020
import org.elasticsearch.xpack.esql.core.tree.NodeInfo;
2121
import org.elasticsearch.xpack.esql.core.tree.Source;
22+
import org.elasticsearch.xpack.esql.core.type.DataType;
2223
import org.elasticsearch.xpack.esql.expression.function.Example;
2324
import org.elasticsearch.xpack.esql.expression.function.FunctionInfo;
2425
import org.elasticsearch.xpack.esql.expression.function.Param;
@@ -27,11 +28,22 @@
2728
import java.io.IOException;
2829
import java.util.List;
2930
import java.util.Locale;
31+
import java.util.Set;
3032

3133
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.FIRST;
3234
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.ParamOrdinal.SECOND;
33-
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isNotNull;
34-
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isString;
35+
import static org.elasticsearch.xpack.esql.core.expression.TypeResolutions.isType;
36+
import static org.elasticsearch.xpack.esql.core.type.DataType.BOOLEAN;
37+
import static org.elasticsearch.xpack.esql.core.type.DataType.DATETIME;
38+
import static org.elasticsearch.xpack.esql.core.type.DataType.DATE_NANOS;
39+
import static org.elasticsearch.xpack.esql.core.type.DataType.DOUBLE;
40+
import static org.elasticsearch.xpack.esql.core.type.DataType.INTEGER;
41+
import static org.elasticsearch.xpack.esql.core.type.DataType.IP;
42+
import static org.elasticsearch.xpack.esql.core.type.DataType.KEYWORD;
43+
import static org.elasticsearch.xpack.esql.core.type.DataType.LONG;
44+
import static org.elasticsearch.xpack.esql.core.type.DataType.TEXT;
45+
import static org.elasticsearch.xpack.esql.core.type.DataType.UNSIGNED_LONG;
46+
import static org.elasticsearch.xpack.esql.core.type.DataType.VERSION;
3547

3648
/**
3749
* Full text function that performs a {@link QueryStringQuery} .
@@ -44,6 +56,19 @@ public class Match extends FullTextFunction implements Validatable {
4456

4557
private transient Boolean isOperator;
4658

59+
public static final Set<DataType> DATA_TYPES = Set.of(
60+
KEYWORD,
61+
TEXT,
62+
BOOLEAN,
63+
DATETIME,
64+
DATE_NANOS,
65+
DOUBLE,
66+
INTEGER,
67+
IP,
68+
LONG,
69+
UNSIGNED_LONG,
70+
VERSION);
71+
4772
@FunctionInfo(
4873
returnType = "boolean",
4974
preview = true,
@@ -52,10 +77,14 @@ public class Match extends FullTextFunction implements Validatable {
5277
)
5378
public Match(
5479
Source source,
55-
@Param(name = "field", type = { "keyword", "text" }, description = "Field that the query will target.") Expression field,
80+
@Param(
81+
name = "field",
82+
type = { "keyword", "text", "boolean", "date", "date_nanos", "double", "integer", "ip", "long", "unsigned_long", "version" },
83+
description = "Field that the query will target."
84+
) Expression field,
5685
@Param(
5786
name = "query",
58-
type = { "keyword", "text" },
87+
type = { "keyword", "text", "boolean", "date", "date_nanos", "double", "integer", "ip", "long", "unsigned_long", "version" },
5988
description = "Text you wish to find in the provided field."
6089
) Expression matchQuery
6190
) {
@@ -84,7 +113,24 @@ public String getWriteableName() {
84113

85114
@Override
86115
protected TypeResolution resolveNonQueryParamTypes() {
87-
return isNotNull(field, sourceText(), FIRST).and(isString(field, sourceText(), FIRST)).and(super.resolveNonQueryParamTypes());
116+
return isType(
117+
field,
118+
DATA_TYPES::contains,
119+
functionName(),
120+
FIRST,
121+
"keyword, text, boolean, date, date_nanos, double, integer, ip, long, unsigned_long, version"
122+
);
123+
}
124+
125+
@Override
126+
protected TypeResolution resolveQueryParamType() {
127+
return isType(
128+
query(),
129+
DATA_TYPES::contains,
130+
functionName(),
131+
queryParamOrdinal(),
132+
"keyword, text, boolean, date, date_nanos, double, integer, ip, long, unsigned_long, version"
133+
);
88134
}
89135

90136
@Override

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/optimizer/rules/physical/local/PushFiltersToSource.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@
3030
import org.elasticsearch.xpack.esql.core.type.DataType;
3131
import org.elasticsearch.xpack.esql.core.util.CollectionUtils;
3232
import org.elasticsearch.xpack.esql.core.util.Queries;
33-
import org.elasticsearch.xpack.esql.expression.function.fulltext.Match;
34-
import org.elasticsearch.xpack.esql.expression.function.fulltext.QueryString;
33+
import org.elasticsearch.xpack.esql.expression.function.fulltext.FullTextFunction;
3534
import org.elasticsearch.xpack.esql.expression.function.scalar.ip.CIDRMatch;
3635
import org.elasticsearch.xpack.esql.expression.function.scalar.spatial.BinarySpatialFunction;
3736
import org.elasticsearch.xpack.esql.expression.function.scalar.spatial.SpatialRelatesFunction;
@@ -252,11 +251,10 @@ static boolean canPushToSource(Expression exp, LucenePushdownPredicates lucenePu
252251
&& Expressions.foldable(cidrMatch.matches());
253252
} else if (exp instanceof SpatialRelatesFunction spatial) {
254253
return canPushSpatialFunctionToSource(spatial, lucenePushdownPredicates);
255-
} else if (exp instanceof QueryString) {
254+
} else if (exp instanceof FullTextFunction) {
256255
return true;
257-
} else if (exp instanceof Match mf) {
258-
return mf.field() instanceof FieldAttribute && DataType.isString(mf.field().dataType());
259256
}
257+
260258
return false;
261259
}
262260

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/planner/EsqlExpressionTranslators.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,7 @@ private static RangeQuery translate(Range r, TranslatorHandler handler) {
528528
public static class MatchFunctionTranslator extends ExpressionTranslator<Match> {
529529
@Override
530530
protected Query asQuery(Match match, TranslatorHandler handler) {
531-
return new MatchQuery(match.source(), ((FieldAttribute) match.field()).name(), match.queryAsText());
531+
return new MatchQuery(match.source(), ((FieldAttribute) match.field()).name(), match.queryAsObject());
532532
}
533533
}
534534

0 commit comments

Comments
 (0)