Skip to content

Commit 095e63d

Browse files
Add printable discription for push down Automata
1 parent b597a54 commit 095e63d

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

server/src/main/java/org/elasticsearch/index/query/AutomatonQueryBuilder.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
public class AutomatonQueryBuilder extends AbstractQueryBuilder<AutomatonQueryBuilder> implements MultiTermQueryBuilder {
3030
private final String fieldName;
3131
private final Automaton automaton;
32+
private final String description;
3233

33-
public AutomatonQueryBuilder(String fieldName, Automaton automaton) {
34+
public AutomatonQueryBuilder(String fieldName, Automaton automaton, String description) {
35+
this.description = description;
3436
if (Strings.isEmpty(fieldName)) {
3537
throw new IllegalArgumentException("field name is null or empty");
3638
}
@@ -63,7 +65,7 @@ protected void doXContent(XContentBuilder builder, Params params) throws IOExcep
6365

6466
@Override
6567
protected Query doToQuery(SearchExecutionContext context) throws IOException {
66-
return new AutomatonQuery(new Term(fieldName), automaton);
68+
return new AutomatonQueryWithDescription(new Term(fieldName), automaton, description);
6769
}
6870

6971
@Override
@@ -80,4 +82,21 @@ protected boolean doEquals(AutomatonQueryBuilder other) {
8082
public TransportVersion getMinimalSupportedVersion() {
8183
throw new UnsupportedOperationException("AutomatonQueryBuilder does not support getMinimalSupportedVersion");
8284
}
85+
86+
static class AutomatonQueryWithDescription extends AutomatonQuery {
87+
private final String description;
88+
89+
AutomatonQueryWithDescription(Term term, Automaton automaton, String description) {
90+
super(term, automaton);
91+
this.description = description;
92+
}
93+
94+
@Override
95+
public String toString(String field) {
96+
if(description.isEmpty()) {
97+
return super.toString(field);
98+
}
99+
return description;
100+
}
101+
}
83102
}

x-pack/plugin/esql-core/src/main/java/org/elasticsearch/xpack/esql/core/querydsl/query/AutomatonQuery.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ public class AutomatonQuery extends Query {
2020

2121
private final String field;
2222
private final Automaton automaton;
23+
private final String automatonDescription;
2324

24-
public AutomatonQuery(Source source, String field, Automaton automaton) {
25+
public AutomatonQuery(Source source, String field, Automaton automaton, String automatonDescription) {
2526
super(source);
2627
this.field = field;
2728
this.automaton = automaton;
29+
this.automatonDescription = automatonDescription;
2830
}
2931

3032
public String field() {
@@ -33,7 +35,7 @@ public String field() {
3335

3436
@Override
3537
protected QueryBuilder asBuilder() {
36-
return new AutomatonQueryBuilder(field, automaton);
38+
return new AutomatonQueryBuilder(field, automaton, automatonDescription);
3739
}
3840

3941
@Override

x-pack/plugin/esql/qa/server/single-node/src/javaRestTest/java/org/elasticsearch/xpack/esql/qa/single_node/PushQueriesIT.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ public void testLikeList() throws IOException {
266266
String luceneQuery = switch (type) {
267267
case CONSTANT_KEYWORD, MATCH_ONLY_TEXT_WITH_KEYWORD, AUTO, TEXT_WITH_KEYWORD -> "*:*";
268268
case SEMANTIC_TEXT_WITH_KEYWORD -> "FieldExistsQuery [field=_primary_term]";
269-
case KEYWORD -> "test:AutomatonQuery";
269+
case KEYWORD -> "Automaton for test LIKE (\"%value*\", \"abc*\"), caseInsensitive=false";
270270
};
271271
ComputeSignature dataNodeSignature = switch (type) {
272272
case CONSTANT_KEYWORD, KEYWORD -> ComputeSignature.FILTER_IN_QUERY;
@@ -325,17 +325,12 @@ private void testPushQuery(
325325
equalTo(found ? List.of(List.of(value)) : List.of())
326326
);
327327

328-
Matcher<String> luceneQueryMatcher;
329-
if (luceneQueryOptions.size() == 1 && luceneQueryOptions.get(0).equals("test:AutomatonQuery")) {
330-
luceneQueryMatcher = anyOf(startsWith("test:AutomatonQuery"));
331-
} else {
332-
luceneQueryMatcher = anyOf(
333-
() -> Iterators.map(
334-
luceneQueryOptions.iterator(),
335-
(String s) -> equalTo(s.replaceAll("%value", value).replaceAll("%different_value", differentValue))
336-
)
337-
);
338-
}
328+
Matcher<String> luceneQueryMatcher = anyOf(
329+
() -> Iterators.map(
330+
luceneQueryOptions.iterator(),
331+
(String s) -> equalTo(s.replaceAll("%value", value).replaceAll("%different_value", differentValue))
332+
)
333+
);
339334

340335
@SuppressWarnings("unchecked")
341336
List<Map<String, Object>> profiles = (List<Map<String, Object>>) ((Map<String, Object>) result.get("profile")).get("drivers");

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/expression/function/scalar/string/regex/WildcardLikeList.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.common.io.stream.StreamOutput;
1313
import org.elasticsearch.xpack.esql.core.expression.Expression;
1414
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
15+
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.WildcardPattern;
1516
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.WildcardPatternList;
1617
import org.elasticsearch.xpack.esql.core.querydsl.query.AutomatonQuery;
1718
import org.elasticsearch.xpack.esql.core.querydsl.query.Query;
@@ -24,6 +25,7 @@
2425
import org.elasticsearch.xpack.esql.planner.TranslatorHandler;
2526

2627
import java.io.IOException;
28+
import java.util.stream.Collectors;
2729

2830
public class WildcardLikeList extends RegexMatch<WildcardPatternList> {
2931
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
@@ -110,6 +112,17 @@ public Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHand
110112
* Throws an {@link IllegalArgumentException} if the pattern list contains more than one pattern.
111113
*/
112114
private Query translateField(String targetFieldName) {
113-
return new AutomatonQuery(source(), targetFieldName, pattern().createAutomaton(caseInsensitive()));
115+
return new AutomatonQuery(
116+
source(),
117+
targetFieldName,
118+
pattern().createAutomaton(caseInsensitive()),
119+
getAutomatonDescription(targetFieldName)
120+
);
121+
}
122+
123+
private String getAutomatonDescription(String targetFieldName) {
124+
// we use all the information used the create the automaton to describe the query here
125+
String patternDesc = pattern().patternList().stream().map(WildcardPattern::pattern).collect(Collectors.joining("\", \""));
126+
return "Automaton for " + targetFieldName + " LIKE (\"" + patternDesc + "\"), caseInsensitive=" + caseInsensitive();
114127
}
115128
}

0 commit comments

Comments
 (0)