Skip to content

Commit 541fc6d

Browse files
Clean up and comments
1 parent 77c541d commit 541fc6d

File tree

12 files changed

+67
-49
lines changed

12 files changed

+67
-49
lines changed

server/src/main/java/org/elasticsearch/index/mapper/ConstantFieldType.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,11 @@ public final Query wildcardQuery(String value, boolean caseInsensitive, QueryRew
137137
}
138138
}
139139

140+
/**
141+
* Returns a query that matches all documents or no documents
142+
* It usually calls {@link #wildcardQuery(String, boolean, QueryRewriteContext)}
143+
* except for IndexFieldType which overrides this method to use its own matching logic.
144+
*/
140145
public Query wildcardLikeQuery(String value, boolean caseInsensitive, QueryRewriteContext context) {
141146
return wildcardQuery(value, caseInsensitive, context);
142147
}
@@ -147,8 +152,16 @@ public final boolean fieldHasValue(FieldInfos fieldInfos) {
147152
return true;
148153
}
149154

155+
/**
156+
* Returns the constant value of this field as a string.
157+
* Based on the field type, we need to get it in a different way.
158+
*/
150159
public abstract String getConstantFieldValue(SearchExecutionContext context);
151160

161+
/**
162+
* Returns a query that matches all documents or no documents
163+
* depending on whether the constant value of this field matches or not
164+
*/
152165
@Override
153166
public Query automatonQuery(
154167
Automaton automaton,

server/src/main/java/org/elasticsearch/index/mapper/KeywordFieldMapper.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,6 +1045,7 @@ public boolean hasDocValuesSkipper() {
10451045
return hasDocValuesSkipper;
10461046
}
10471047

1048+
@Override
10481049
public Query automatonQuery(
10491050
Automaton automaton,
10501051
@Nullable MultiTermQuery.RewriteMethod method,

server/src/main/java/org/elasticsearch/index/mapper/MappedFieldType.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,10 @@ public final Query wildcardQuery(String value, @Nullable MultiTermQuery.RewriteM
330330
return wildcardQuery(value, method, false, context);
331331
}
332332

333+
/**
334+
* Similar to wildcardQuery, except that we change the behavior for
335+
* this method for IndexFieldMapper
336+
*/
333337
public Query wildcardLikeQuery(
334338
String value,
335339
@Nullable MultiTermQuery.RewriteMethod method,
@@ -380,6 +384,10 @@ public Query regexpQuery(
380384
);
381385
}
382386

387+
/**
388+
* Returns a Lucine pushable Query for the current field
389+
* For now can only be AutomatonQuery or MatchAllDocsQuery() or MatchNoDocsQuery()
390+
*/
383391
public Query automatonQuery(
384392
Automaton automaton,
385393
@Nullable MultiTermQuery.RewriteMethod method,

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
import org.apache.lucene.search.AutomatonQuery;
1414
import org.apache.lucene.util.automaton.Automaton;
1515

16+
/**
17+
* A specialized {@link AutomatonQuery} that includes a description of the query.
18+
* This can be useful for debugging or logging purposes, providing more context
19+
* about the query being executed.
20+
*/
1621
public class AutomatonQueryWithDescription extends AutomatonQuery {
1722
private final String description;
1823

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.elasticsearch.TransportVersion;
1717
import org.elasticsearch.TransportVersions;
1818
import org.elasticsearch.common.Strings;
19+
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1920
import org.elasticsearch.common.io.stream.StreamInput;
2021
import org.elasticsearch.common.io.stream.StreamOutput;
2122
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
@@ -39,10 +40,13 @@
3940
* {@code ?}.
4041
*/
4142
public class WildcardLikeQueryBuilder extends AbstractQueryBuilder<WildcardLikeQueryBuilder> implements MultiTermQueryBuilder {
42-
public static final String NAME = "wildcardlike";
43+
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
44+
QueryBuilder.class,
45+
"wildcardLikeQueryBuilder",
46+
WildcardLikeQueryBuilder::new
47+
);
4348

4449
private static final ParseField WILDCARD_FIELD = new ParseField("wildcard");
45-
private static final ParseField VALUE_FIELD = new ParseField("value");
4650
private static final ParseField REWRITE_FIELD = new ParseField("rewrite");
4751

4852
private final String fieldName;
@@ -125,12 +129,12 @@ public boolean caseInsensitive() {
125129

126130
@Override
127131
public String getWriteableName() {
128-
return NAME;
132+
return ENTRY.name;
129133
}
130134

131135
@Override
132136
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
133-
builder.startObject(NAME);
137+
builder.startObject(ENTRY.name);
134138
builder.startObject(fieldName);
135139
builder.field(WILDCARD_FIELD.getPreferredName(), value);
136140
if (rewrite != null) {

server/src/main/java/org/elasticsearch/search/SearchModule.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@
6666
import org.elasticsearch.index.query.TermQueryBuilder;
6767
import org.elasticsearch.index.query.TermsQueryBuilder;
6868
import org.elasticsearch.index.query.TermsSetQueryBuilder;
69-
import org.elasticsearch.index.query.WildcardLikeQueryBuilder;
7069
import org.elasticsearch.index.query.WildcardQueryBuilder;
7170
import org.elasticsearch.index.query.WrapperQueryBuilder;
7271
import org.elasticsearch.index.query.functionscore.ExponentialDecayFunctionBuilder;
@@ -1118,9 +1117,6 @@ private void registerQueryParsers(List<SearchPlugin> plugins) {
11181117
registerQuery(new QuerySpec<>(RangeQueryBuilder.NAME, RangeQueryBuilder::new, RangeQueryBuilder::fromXContent));
11191118
registerQuery(new QuerySpec<>(PrefixQueryBuilder.NAME, PrefixQueryBuilder::new, PrefixQueryBuilder::fromXContent));
11201119
registerQuery(new QuerySpec<>(WildcardQueryBuilder.NAME, WildcardQueryBuilder::new, WildcardQueryBuilder::fromXContent));
1121-
registerQuery(
1122-
new QuerySpec<>(WildcardLikeQueryBuilder.NAME, WildcardLikeQueryBuilder::new, WildcardLikeQueryBuilder::fromXContent)
1123-
);
11241120
registerQuery(
11251121
new QuerySpec<>(ConstantScoreQueryBuilder.NAME, ConstantScoreQueryBuilder::new, ConstantScoreQueryBuilder::fromXContent)
11261122
);

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/capabilities/TranslationAware.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,17 @@ static TranslationAware.Translatable translatable(Expression exp, LucenePushdown
4848
*/
4949
Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHandler handler);
5050

51+
/**
52+
* Translates the implementing expression into a Lucene {@link Automaton}.
53+
*/
5154
default Automaton asLuceneQuery() {
5255
throw new UnsupportedOperationException("asLuceneQuery is not implemented for " + getClass().getName());
5356
};
5457

58+
/**
59+
* Returns a description of the Lucene query that this expression translates to.
60+
* This is used for debugging and logging purposes.
61+
*/
5562
default String getLuceneQueryDescription() {
5663
throw new UnsupportedOperationException("getLuceneQueryDescription is not implemented for " + getClass().getName());
5764
};

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

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,9 @@
88
package org.elasticsearch.xpack.esql.expression.function.scalar.string.regex;
99

1010
import org.apache.lucene.util.automaton.Automaton;
11-
import org.elasticsearch.common.breaker.CircuitBreaker;
12-
import org.elasticsearch.common.breaker.NoopCircuitBreaker;
1311
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
1412
import org.elasticsearch.common.io.stream.StreamInput;
1513
import org.elasticsearch.common.io.stream.StreamOutput;
16-
import org.elasticsearch.common.util.BigArrays;
17-
import org.elasticsearch.compute.data.BlockFactory;
18-
import org.elasticsearch.compute.data.BlockStreamInput;
1914
import org.elasticsearch.xpack.esql.core.expression.Expression;
2015
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
2116
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.WildcardPattern;
@@ -71,11 +66,8 @@ public WildcardLikeList(StreamInput in) throws IOException {
7166
in.readNamedWriteable(Expression.class),
7267
new WildcardPatternList(in),
7368
deserializeCaseInsensitivity(in),
74-
null
69+
Configuration.readFrom(in)
7570
);
76-
BlockFactory blockFactory = new BlockFactory(new NoopCircuitBreaker(CircuitBreaker.REQUEST), BigArrays.NON_RECYCLING_INSTANCE);
77-
BlockStreamInput blockStreamInput = new BlockStreamInput(in, blockFactory);
78-
this.configuration = new Configuration(blockStreamInput);
7971
}
8072

8173
@Override

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/io/stream/ExpressionQuery.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import java.util.Objects;
1616

1717
/**
18-
* Query that matches documents based on a Lucene Automaton.
18+
* Implements an Expression query, which matches documents based on a given expression.
1919
*/
2020
public class ExpressionQuery extends Query {
2121

@@ -60,6 +60,6 @@ public boolean equals(Object obj) {
6060

6161
@Override
6262
protected String innerToString() {
63-
return "AutomatonQuery{" + "field='" + targetFieldName + '\'' + '}';
63+
return "ExpressionQuery{" + "field='" + targetFieldName + '\'' + '}';
6464
}
6565
}

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/io/stream/ExpressionQueryBuilder.java

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,14 @@
77

88
package org.elasticsearch.xpack.esql.io.stream;
99

10-
import org.apache.lucene.index.Term;
11-
import org.apache.lucene.search.AutomatonQuery;
1210
import org.apache.lucene.search.MatchNoDocsQuery;
1311
import org.apache.lucene.search.Query;
1412
import org.apache.lucene.util.automaton.Automaton;
1513
import org.elasticsearch.TransportVersion;
1614
import org.elasticsearch.common.Strings;
17-
import org.elasticsearch.common.breaker.CircuitBreaker;
18-
import org.elasticsearch.common.breaker.NoopCircuitBreaker;
1915
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
2016
import org.elasticsearch.common.io.stream.StreamInput;
2117
import org.elasticsearch.common.io.stream.StreamOutput;
22-
import org.elasticsearch.common.util.BigArrays;
23-
import org.elasticsearch.compute.data.BlockFactory;
24-
import org.elasticsearch.compute.data.BlockStreamInput;
2518
import org.elasticsearch.index.mapper.MappedFieldType;
2619
import org.elasticsearch.index.query.AbstractQueryBuilder;
2720
import org.elasticsearch.index.query.MultiTermQueryBuilder;
@@ -39,8 +32,10 @@
3932
import static org.apache.lucene.search.MultiTermQuery.CONSTANT_SCORE_REWRITE;
4033

4134
/**
42-
* Implements an Automaton query, which matches documents based on a Lucene Automaton.
43-
* It does not support serialization or XContent representation.
35+
* Implements an Expression query builder, which matches documents based on a given expression.
36+
* The expression itself must provide the asLuceneQuery and getLuceneQueryDescription methods
37+
* It allows for serialization of the expression and generate an AutomatonQuery on the data node
38+
* as Automaton does not support serialization.
4439
*/
4540
public class ExpressionQueryBuilder extends AbstractQueryBuilder<ExpressionQueryBuilder> implements MultiTermQueryBuilder {
4641
public static final NamedWriteableRegistry.Entry ENTRY = new NamedWriteableRegistry.Entry(
@@ -70,10 +65,8 @@ public ExpressionQueryBuilder(String fieldName, Expression expression, Configura
7065
public ExpressionQueryBuilder(StreamInput in) throws IOException {
7166
super(in);
7267
fieldName = in.readString();
73-
BlockFactory blockFactory = new BlockFactory(new NoopCircuitBreaker(CircuitBreaker.REQUEST), BigArrays.NON_RECYCLING_INSTANCE);
74-
BlockStreamInput blockStreamInput = new BlockStreamInput(in, blockFactory);
75-
this.config = new Configuration(blockStreamInput);
76-
PlanStreamInput planStreamInput = new PlanStreamInput(blockStreamInput, in.namedWriteableRegistry(), config);
68+
this.config = Configuration.readFrom(in);
69+
PlanStreamInput planStreamInput = new PlanStreamInput(in, in.namedWriteableRegistry(), config);
7770
this.expression = planStreamInput.readNamedWriteable(Expression.class);
7871
}
7972

@@ -129,21 +122,4 @@ protected boolean doEquals(ExpressionQueryBuilder other) {
129122
public TransportVersion getMinimalSupportedVersion() {
130123
throw new UnsupportedOperationException("AutomatonQueryBuilder does not support getMinimalSupportedVersion");
131124
}
132-
133-
static class AutomatonQueryWithDescription extends AutomatonQuery {
134-
private final String description;
135-
136-
AutomatonQueryWithDescription(Term term, Automaton automaton, String description) {
137-
super(term, automaton);
138-
this.description = description;
139-
}
140-
141-
@Override
142-
public String toString(String field) {
143-
if (this.field.equals(field)) {
144-
return description;
145-
}
146-
return this.field + ":" + description;
147-
}
148-
}
149125
}

0 commit comments

Comments
 (0)