Skip to content

Commit e1eaf49

Browse files
committed
Merge remote-tracking branch 'julian-elastic/indexLike_v3' into indexLike_v3
2 parents bd1f56c + a251c09 commit e1eaf49

File tree

7 files changed

+66
-12
lines changed

7 files changed

+66
-12
lines changed

benchmarks/src/main/java/org/elasticsearch/benchmark/_nightly/esql/QueryPlanningBenchmark.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public void setup() {
8787
false,
8888
Map.of(),
8989
System.nanoTime(),
90-
false
90+
false,
91+
true
9192
);
9293

9394
var fields = 10_000;

benchmarks/src/main/java/org/elasticsearch/benchmark/compute/operator/EvalBenchmark.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ private static Configuration configuration() {
349349
false,
350350
Map.of(),
351351
0,
352-
false
352+
false,
353+
true
353354
);
354355
}
355356

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import java.util.Collection;
2727
import java.util.Map;
28+
import java.util.function.Supplier;
2829

2930
/**
3031
* A {@link MappedFieldType} that has the same value for all documents.
@@ -164,12 +165,13 @@ public final boolean fieldHasValue(FieldInfos fieldInfos) {
164165
*/
165166
@Override
166167
public Query automatonQuery(
167-
Automaton automaton,
168+
Supplier<Automaton> automatonSupplier,
169+
Supplier<CharacterRunAutomaton> characterRunAutomatonSupplier,
168170
@Nullable MultiTermQuery.RewriteMethod method,
169171
SearchExecutionContext context,
170172
String description
171173
) {
172-
CharacterRunAutomaton compiled = new CharacterRunAutomaton(automaton);
174+
CharacterRunAutomaton compiled = characterRunAutomatonSupplier.get();
173175
boolean matches = compiled.run(getConstantFieldValue(context));
174176
if (matches) {
175177
return new MatchAllDocsQuery();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.apache.lucene.util.BytesRef;
3333
import org.apache.lucene.util.automaton.Automata;
3434
import org.apache.lucene.util.automaton.Automaton;
35+
import org.apache.lucene.util.automaton.CharacterRunAutomaton;
3536
import org.apache.lucene.util.automaton.CompiledAutomaton;
3637
import org.apache.lucene.util.automaton.CompiledAutomaton.AUTOMATON_TYPE;
3738
import org.apache.lucene.util.automaton.Operations;
@@ -84,6 +85,7 @@
8485
import java.util.Map;
8586
import java.util.Objects;
8687
import java.util.Set;
88+
import java.util.function.Supplier;
8789

8890
import static org.apache.lucene.index.IndexWriter.MAX_TERM_LENGTH;
8991
import static org.elasticsearch.core.Strings.format;
@@ -1047,12 +1049,13 @@ public boolean hasDocValuesSkipper() {
10471049

10481050
@Override
10491051
public Query automatonQuery(
1050-
Automaton automaton,
1052+
Supplier<Automaton> automatonSupplier,
1053+
Supplier<CharacterRunAutomaton> characterRunAutomatonSupplier,
10511054
@Nullable MultiTermQuery.RewriteMethod method,
10521055
SearchExecutionContext context,
10531056
String description
10541057
) {
1055-
return new AutomatonQueryWithDescription(new Term(name()), automaton, description);
1058+
return new AutomatonQueryWithDescription(new Term(name()), automatonSupplier.get(), description);
10561059
}
10571060
}
10581061

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.lucene.search.TermQuery;
2727
import org.apache.lucene.util.BytesRef;
2828
import org.apache.lucene.util.automaton.Automaton;
29+
import org.apache.lucene.util.automaton.CharacterRunAutomaton;
2930
import org.elasticsearch.ElasticsearchException;
3031
import org.elasticsearch.ElasticsearchParseException;
3132
import org.elasticsearch.cluster.metadata.IndexMetadata;
@@ -55,6 +56,7 @@
5556
import java.util.Objects;
5657
import java.util.Set;
5758
import java.util.function.Function;
59+
import java.util.function.Supplier;
5860

5961
import static org.elasticsearch.search.SearchService.ALLOW_EXPENSIVE_QUERIES;
6062

@@ -389,7 +391,8 @@ public Query regexpQuery(
389391
* For now can only be AutomatonQuery or MatchAllDocsQuery() or MatchNoDocsQuery()
390392
*/
391393
public Query automatonQuery(
392-
Automaton automaton,
394+
Supplier<Automaton> automatonSupplier,
395+
Supplier<CharacterRunAutomaton> characterRunAutomatonSupplier,
393396
@Nullable MultiTermQuery.RewriteMethod method,
394397
SearchExecutionContext context,
395398
String description

x-pack/plugin/esql/qa/server/multi-clusters/src/javaRestTest/java/org/elasticsearch/xpack/esql/ccq/MultiClustersIT.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,14 @@ public void testLikeIndex() throws Exception {
431431
}
432432

433433
public void testLikeIndexLegacySettingNoResults() throws Exception {
434+
boolean isSupported = clusterHasCapability("POST", "/_query", List.of(), List.of("like_on_index_fields")).orElse(false);
435+
if (isSupported == false) {
436+
// we require that the admin client supports the like_on_index_fields capability
437+
// otherwise we will get an error when trying to toggle the setting
438+
// the remote client does not have to support it
439+
logger.info("--> skipping testLikeIndexLegacySettingNoResults, due to missing capability");
440+
return;
441+
}
434442
try (ClusterSettingToggle ignored = new ClusterSettingToggle(adminClient(), "esql.query.string_like_on_index", false, true)) {
435443
// test code with the setting changed
436444
boolean includeCCSMetadata = includeCCSMetadata();
@@ -448,6 +456,14 @@ public void testLikeIndexLegacySettingNoResults() throws Exception {
448456
}
449457

450458
public void testLikeIndexLegacySettingResults() throws Exception {
459+
boolean isSupported = clusterHasCapability("POST", "/_query", List.of(), List.of("like_on_index_fields")).orElse(false);
460+
if (isSupported == false) {
461+
// we require that the admin client supports the like_on_index_fields capability
462+
// otherwise we will get an error when trying to toggle the setting
463+
// the remote client does not have to support it
464+
logger.info("--> skipping testLikeIndexLegacySettingNoResults, due to missing capability");
465+
return;
466+
}
451467
try (ClusterSettingToggle ignored = new ClusterSettingToggle(adminClient(), "esql.query.string_like_on_index", false, true)) {
452468
boolean includeCCSMetadata = includeCCSMetadata();
453469
Map<String, Object> result = run("""

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

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import org.apache.lucene.search.MultiTermQuery.RewriteMethod;
1111
import org.apache.lucene.util.automaton.Automaton;
12+
import org.apache.lucene.util.automaton.CharacterRunAutomaton;
1213
import org.elasticsearch.TransportVersion;
1314
import org.elasticsearch.TransportVersions;
1415
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
@@ -31,6 +32,7 @@
3132
import org.elasticsearch.xpack.esql.planner.TranslatorHandler;
3233

3334
import java.io.IOException;
35+
import java.util.function.Supplier;
3436
import java.util.stream.Collectors;
3537

3638
public class WildcardLikeList extends RegexMatch<WildcardPatternList> {
@@ -40,6 +42,30 @@ public class WildcardLikeList extends RegexMatch<WildcardPatternList> {
4042
WildcardLikeList::new
4143
);
4244

45+
Supplier<Automaton> automatonSupplier = new Supplier<>() {
46+
Automaton cached;
47+
48+
@Override
49+
public Automaton get() {
50+
if (cached == null) {
51+
cached = pattern().createAutomaton(caseInsensitive());
52+
}
53+
return cached;
54+
}
55+
};
56+
57+
Supplier<CharacterRunAutomaton> characterRunAutomatonSupplier = new Supplier<>() {
58+
CharacterRunAutomaton cached;
59+
60+
@Override
61+
public CharacterRunAutomaton get() {
62+
if (cached == null) {
63+
cached = new CharacterRunAutomaton(automatonSupplier.get());
64+
}
65+
return cached;
66+
}
67+
};
68+
4369
/**
4470
* The documentation for this function is in WildcardLike, and shown to the users `LIKE` in the docs.
4571
*/
@@ -112,9 +138,6 @@ public Translatable translatable(LucenePushdownPredicates pushdownPredicates) {
112138
*/
113139
@Override
114140
public Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHandler handler) {
115-
if (pushdownPredicates.configuration().stringLikeOnIndex() == false) {
116-
throw new IllegalArgumentException("LIKE with LIST cannot be used with string_like_on_index enabled. Use LIKE instead.");
117-
}
118141
var field = field();
119142
LucenePushdownPredicates.checkIsPushableAttribute(field);
120143
String targetFieldName = handler.nameOf(field instanceof FieldAttribute fa ? fa.exactAttribute() : field);
@@ -131,8 +154,13 @@ public org.apache.lucene.search.Query asLuceneQuery(
131154
RewriteMethod constantScoreRewrite,
132155
SearchExecutionContext context
133156
) {
134-
Automaton automaton = pattern().createAutomaton(caseInsensitive());
135-
return fieldType.automatonQuery(automaton, constantScoreRewrite, context, getLuceneQueryDescription());
157+
return fieldType.automatonQuery(
158+
automatonSupplier,
159+
characterRunAutomatonSupplier,
160+
constantScoreRewrite,
161+
context,
162+
getLuceneQueryDescription()
163+
);
136164
}
137165

138166
private String getLuceneQueryDescription() {

0 commit comments

Comments
 (0)