Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5406c66
ESQL: Push more `==`s on text fields to lucene
nik9000 Apr 10, 2025
c6bb228
Update docs/changelog/126641.yaml
nik9000 Apr 10, 2025
523321a
Merge branch 'main' into esql_push_text_sub
nik9000 Apr 11, 2025
a5e2206
Fix off by one
nik9000 Apr 11, 2025
db25092
Merge remote-tracking branch 'nik9000/esql_push_text_sub' into esql_p…
nik9000 Apr 11, 2025
f5ce702
Merge branch 'main' into esql_push_text_sub
nik9000 Apr 14, 2025
9bd4b89
Proper delegate
nik9000 Apr 14, 2025
42b7a20
Fix delegate for ignored fields
nik9000 Apr 14, 2025
a2455e9
fmt
nik9000 Apr 14, 2025
267632b
[CI] Auto commit changes from spotless
Apr 14, 2025
254158b
test!
nik9000 Apr 14, 2025
f872334
Merge remote-tracking branch 'nik9000/esql_push_text_sub' into esql_p…
nik9000 Apr 14, 2025
1b34eb2
[CI] Auto commit changes from spotless
Apr 14, 2025
600257a
Explain
nik9000 Apr 14, 2025
3910ae9
Merge remote-tracking branch 'nik9000/esql_push_text_sub' into esql_p…
nik9000 Apr 14, 2025
885598f
Fix test
nik9000 Apr 14, 2025
a6c7596
Merge branch 'main' into esql_push_text_sub
nik9000 Apr 15, 2025
c85680a
fixup
nik9000 Apr 15, 2025
d76b174
Merge branch 'main' into esql_push_text_sub
nik9000 Apr 15, 2025
28c3b5b
Update
nik9000 Apr 15, 2025
fb84d6b
Merge branch 'main' into esql_push_text_sub
nik9000 Apr 16, 2025
00794c8
Merge branch 'main' into esql_push_text_sub
nik9000 Apr 16, 2025
5ddb10c
WIP
nik9000 Apr 16, 2025
dc26271
Merge branch 'main' into esql_push_text_sub
nik9000 Apr 18, 2025
4c082e9
Move test
nik9000 Apr 18, 2025
6f4e5eb
Merge branch 'main' into esql_push_text_sub
nik9000 Apr 18, 2025
deab4be
Merge branch 'main' into esql_push_text_sub
nik9000 Apr 18, 2025
faa245d
Merge branch 'main' into esql_push_text_sub
nik9000 Apr 21, 2025
4a02fe1
Merge remote-tracking branch 'nik9000/esql_push_text_sub' into esql_p…
nik9000 Apr 21, 2025
0f9b54f
Revert things we don't need
nik9000 Apr 21, 2025
4125263
Merge branch 'main' into esql_push_text_sub
nik9000 Apr 21, 2025
ac558f3
Updates
nik9000 Apr 21, 2025
5e8430b
Merge branch 'main' into esql_push_text_sub
nik9000 Apr 22, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/126641.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 126641
summary: Push more `==`s on text fields to lucene
area: ES|QL
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -983,6 +983,21 @@ public boolean canUseSyntheticSourceDelegateForQuerying() {
&& syntheticSourceDelegate.isIndexed();
}

/**
* Returns true if the delegate sub-field can be used for querying only (ie. isIndexed must be true)
*/
public boolean canUseSyntheticSourceDelegateForQueryingEquality(String str) {
if (syntheticSourceDelegate == null
// Can't push equality to an index if there isn't an index
|| syntheticSourceDelegate.isIndexed() == false
// ESQL needs docs values to push equality
|| syntheticSourceDelegate.hasDocValues() == false) {
return false;
}
// Can't push equality if the field we're checking for is so big we'd ignore it.
return str.length() <= syntheticSourceDelegate.ignoreAbove();
}

@Override
public BlockLoader blockLoader(BlockLoaderContext blContext) {
if (canUseSyntheticSourceDelegateForLoading()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

package org.elasticsearch.xpack.esql.qa.single_node;

import com.carrotsearch.randomizedtesting.annotations.ThreadLeakFilters;

import org.elasticsearch.client.Request;
import org.elasticsearch.client.Response;
import org.elasticsearch.test.ListMatcher;
import org.elasticsearch.test.MapMatcher;
import org.elasticsearch.test.TestClustersThreadFilter;
import org.elasticsearch.test.cluster.ElasticsearchCluster;
import org.elasticsearch.test.rest.ESRestTestCase;
import org.elasticsearch.xcontent.XContentType;
import org.elasticsearch.xpack.esql.AssertWarnings;
import org.elasticsearch.xpack.esql.qa.rest.RestEsqlTestCase;
import org.junit.ClassRule;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

import static org.elasticsearch.test.ListMatcher.matchesList;
import static org.elasticsearch.test.MapMatcher.assertMap;
import static org.elasticsearch.test.MapMatcher.matchesMap;
import static org.elasticsearch.xpack.esql.EsqlTestUtils.entityToMap;
import static org.elasticsearch.xpack.esql.qa.rest.RestEsqlTestCase.requestObjectBuilder;
import static org.elasticsearch.xpack.esql.qa.rest.RestEsqlTestCase.runEsql;
import static org.elasticsearch.xpack.esql.qa.single_node.RestEsqlIT.commonProfile;
import static org.elasticsearch.xpack.esql.qa.single_node.RestEsqlIT.fixTypesOnProfile;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.startsWith;

/**
* Tests for pushing queries to lucene.
*/
@ThreadLeakFilters(filters = TestClustersThreadFilter.class)
public class PushQueriesIT extends ESRestTestCase {
@ClassRule
public static ElasticsearchCluster cluster = Clusters.testCluster();

public void testPushEqualityOnDefaults() throws IOException {
String value = "v".repeat(between(0, 256));
testPushQuery(value, """
FROM test
| WHERE test == "%value"
""", "#test.keyword:%value -_ignored:test.keyword", false);
}

public void testPushEqualityOnDefaultsTooBigToPush() throws IOException {
String value = "a".repeat(between(257, 1000));
testPushQuery(value, """
FROM test
| WHERE test == "%value"
""", "*:*", true);
}

public void testPushCaseInsensitiveEqualityOnDefaults() throws IOException {
String value = "a".repeat(between(0, 256));
testPushQuery(value, """
FROM test
| WHERE TO_LOWER(test) == "%value"
""", "*:*", true);
}

private void testPushQuery(String value, String esqlQuery, String luceneQuery, boolean filterInCompute) throws IOException {
indexValue(value);

RestEsqlTestCase.RequestObjectBuilder builder = requestObjectBuilder().query(
esqlQuery.replaceAll("%value", value) + "\n| KEEP test"
);
builder.profile(true);
Map<String, Object> result = runEsql(builder, new AssertWarnings.NoWarnings(), RestEsqlTestCase.Mode.SYNC);
assertResultMap(
result,
getResultMatcher(result).entry(
"profile",
matchesMap().entry("drivers", instanceOf(List.class))
.entry("planning", matchesMap().extraOk())
.entry("query", matchesMap().extraOk())
),
matchesList().item(matchesMap().entry("name", "test").entry("type", "text")),
equalTo(List.of(List.of(value)))
);

@SuppressWarnings("unchecked")
List<Map<String, Object>> profiles = (List<Map<String, Object>>) ((Map<String, Object>) result.get("profile")).get("drivers");
for (Map<String, Object> p : profiles) {
fixTypesOnProfile(p);
assertThat(p, commonProfile());
List<String> sig = new ArrayList<>();
@SuppressWarnings("unchecked")
List<Map<String, Object>> operators = (List<Map<String, Object>>) p.get("operators");
for (Map<String, Object> o : operators) {
sig.add(checkOperatorProfile(o, luceneQuery.replaceAll("%value", value)));
}
String description = p.get("description").toString();
switch (description) {
case "data" -> {
ListMatcher matcher = matchesList().item("LuceneSourceOperator").item("ValuesSourceReaderOperator");
if (filterInCompute) {
matcher = matcher.item("FilterOperator").item("LimitOperator");
}
matcher = matcher.item("ProjectOperator").item("ExchangeSinkOperator");
assertMap(sig, matcher);
}
case "node_reduce" -> assertMap(sig, matchesList().item("ExchangeSourceOperator").item("ExchangeSinkOperator"));
case "final" -> assertMap(
sig,
matchesList().item("ExchangeSourceOperator").item("LimitOperator").item("ProjectOperator").item("OutputOperator")
);
default -> throw new IllegalArgumentException("can't match " + description);
}
}
}

private void indexValue(String value) throws IOException {
Request createIndex = new Request("PUT", "test");
createIndex.setJsonEntity("""
{
"settings": {
"index": {
"number_of_shards": 1
}
}
}""");
Response createResponse = client().performRequest(createIndex);
assertThat(
entityToMap(createResponse.getEntity(), XContentType.JSON),
matchesMap().entry("shards_acknowledged", true).entry("index", "test").entry("acknowledged", true)
);

Request bulk = new Request("POST", "/_bulk");
bulk.addParameter("refresh", "");
bulk.setJsonEntity(String.format("""
{"create":{"_index":"test"}}
{"test":"%s"}
""", value));
Response bulkResponse = client().performRequest(bulk);
assertThat(entityToMap(bulkResponse.getEntity(), XContentType.JSON), matchesMap().entry("errors", false).extraOk());
}

private static final Pattern TO_NAME = Pattern.compile("\\[.+", Pattern.DOTALL);

private static String checkOperatorProfile(Map<String, Object> o, String query) {
String name = (String) o.get("operator");
name = TO_NAME.matcher(name).replaceAll("");
if (name.equals("LuceneSourceOperator")) {
MapMatcher expectedOp = matchesMap().entry("operator", startsWith(name))
.entry("status", matchesMap().entry("processed_queries", List.of(query)).extraOk());
assertMap(o, expectedOp);
}
return name;
}

@Override
protected String getTestRestCluster() {
return cluster.getHttpAddresses();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ public void testForceSleepsProfile() throws IOException {
}
}

private MapMatcher commonProfile() {
static MapMatcher commonProfile() {
return matchesMap() //
.entry("description", any(String.class))
.entry("cluster_name", any(String.class))
Expand All @@ -669,7 +669,7 @@ private MapMatcher commonProfile() {
* come back as integers and sometimes longs. This just promotes
* them to long every time.
*/
private void fixTypesOnProfile(Map<String, Object> profile) {
static void fixTypesOnProfile(Map<String, Object> profile) {
profile.put("iterations", ((Number) profile.get("iterations")).longValue());
profile.put("cpu_nanos", ((Number) profile.get("cpu_nanos")).longValue());
profile.put("took_nanos", ((Number) profile.get("took_nanos")).longValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1235,7 +1235,8 @@ public static Map<String, Object> runEsqlAsync(RequestObjectBuilder requestObjec
return runEsqlAsync(requestObject, randomBoolean(), new AssertWarnings.NoWarnings());
}

static Map<String, Object> runEsql(RequestObjectBuilder requestObject, AssertWarnings assertWarnings, Mode mode) throws IOException {
public static Map<String, Object> runEsql(RequestObjectBuilder requestObject, AssertWarnings assertWarnings, Mode mode)
throws IOException {
if (mode == ASYNC) {
return runEsqlAsync(requestObject, randomBoolean(), assertWarnings);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ public class CsvTestsDataLoader {
private static final TestDataset BOOKS = new TestDataset("books").withSetting("books-settings.json");
private static final TestDataset SEMANTIC_TEXT = new TestDataset("semantic_text").withInferenceEndpoint(true);
private static final TestDataset LOGS = new TestDataset("logs");
private static final TestDataset MV_TEXT = new TestDataset("mv_text");

public static final Map<String, TestDataset> CSV_DATASET_MAP = Map.ofEntries(
Map.entry(EMPLOYEES.indexName, EMPLOYEES),
Expand Down Expand Up @@ -196,7 +197,8 @@ public class CsvTestsDataLoader {
Map.entry(ADDRESSES.indexName, ADDRESSES),
Map.entry(BOOKS.indexName, BOOKS),
Map.entry(SEMANTIC_TEXT.indexName, SEMANTIC_TEXT),
Map.entry(LOGS.indexName, LOGS)
Map.entry(LOGS.indexName, LOGS),
Map.entry(MV_TEXT.indexName, MV_TEXT)
);

private static final EnrichConfig LANGUAGES_ENRICH = new EnrichConfig("languages_policy", "enrich-policy-languages.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ public byte[] max(String field, DataType dataType) {
public boolean isSingleValue(String field) {
return false;
}

@Override
public boolean canUseEqualityOnSyntheticSourceDelegate(String name, String value) {
return false;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@timestamp:date ,message:text
2023-10-23T13:55:01.543Z,[Connected to 10.1.0.1, Banana]
2023-10-23T13:55:01.544Z,Connected to 10.1.0.1
2023-10-23T13:55:01.545Z,[Connected to 10.1.0.1, More than one hundred characters long so it isn't indexed by the sub keyword field with ignore_above:100]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about adding also a single-value over ignore_above? So we have all the cases here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I should do that.

2023-10-23T13:55:01.546Z,More than one hundred characters long so it isn't indexed by the sub keyword field with ignore_above:100
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
{
"properties" : {
"emp_no" : {
"type" : "integer"
},
"first_name" : {
"type" : "keyword"
},
"gender" : {
"type" : "text"
},
"languages" : {
"type" : "byte"
},
"last_name" : {
"type" : "keyword"
},
"salary" : {
"type" : "integer"
},
"_meta_field": {
"type" : "keyword"
},
"hire_date": {
"type": "date"
},
"job": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 4
}
}
},
"long_noidx": {
"type": "long",
"index": false,
"doc_values": false
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"properties": {
"@timestamp": {
"type": "date"
},
"message": {
"type": "text",
"fields": {
"raw": {
"type": "keyword",
"ignore_above": 100
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2308,3 +2308,27 @@ message:keyword
foo ( bar
// end::rlikeEscapingTripleQuotes-result[]
;

mvStringEquals
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have a test for an literal over ignored_above chars + MV?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Literals don't have ignore_above.

FROM mv_text
| WHERE message == "Connected to 10.1.0.1"
| KEEP @timestamp, message
;
warning:Line 2:9: evaluation of [message == \"Connected to 10.1.0.1\"] failed, treating result as null. Only first 20 failures recorded.
warning:Line 2:9: java.lang.IllegalArgumentException: single-value function encountered multi-value

@timestamp:date | message:text
2023-10-23T13:55:01.544Z|Connected to 10.1.0.1
;

mvStringEqualsLongString
FROM mv_text
| WHERE message == "More than one hundred characters long so it isn't indexed by the sub keyword field with ignore_above:100"
| KEEP @timestamp, message
;
warning:Line 2:9: evaluation of [message == \"More than one hundred characters long so it isn't indexed by the sub keyword field with ignore_above:100\"] failed, treating result as null. Only first 20 failures recorded.
warning:Line 2:9: java.lang.IllegalArgumentException: single-value function encountered multi-value

@timestamp:date | message:text
2023-10-23T13:55:01.546Z|More than one hundred characters long so it isn't indexed by the sub keyword field with ignore_above:100
;
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public interface TranslationAware {
* <p>and <b>not</b> this:</p>
* <p>{@code Query childQuery = child.asQuery(handler);}</p>
*/
Query asQuery(TranslatorHandler handler);
Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHandler handler);

/**
* Subinterface for expressions that can only process single values (and null out on MVs).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public boolean translatable(LucenePushdownPredicates pushdownPredicates) {
}

@Override
public Query asQuery(TranslatorHandler handler) {
public Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHandler handler) {
return queryBuilder != null ? new TranslationAwareExpressionQuery(source(), queryBuilder) : translate(handler);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.elasticsearch.index.query.QueryRewriteContext;
import org.elasticsearch.index.query.Rewriteable;
import org.elasticsearch.xpack.esql.core.util.Holder;
import org.elasticsearch.xpack.esql.optimizer.rules.physical.local.LucenePushdownPredicates;
import org.elasticsearch.xpack.esql.plan.logical.EsRelation;
import org.elasticsearch.xpack.esql.plan.logical.LogicalPlan;
import org.elasticsearch.xpack.esql.planner.TranslatorHandler;
Expand Down Expand Up @@ -76,7 +77,9 @@ public FullTextFunctionsRewritable rewrite(QueryRewriteContext ctx) throws IOExc
Holder<Boolean> updated = new Holder<>(false);
LogicalPlan newPlan = plan.transformExpressionsDown(FullTextFunction.class, f -> {
QueryBuilder builder = f.queryBuilder(), initial = builder;
builder = builder == null ? f.asQuery(TranslatorHandler.TRANSLATOR_HANDLER).toQueryBuilder() : builder;
builder = builder == null
? f.asQuery(LucenePushdownPredicates.DEFAULT, TranslatorHandler.TRANSLATOR_HANDLER).toQueryBuilder()
: builder;
try {
builder = builder.rewrite(ctx);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ public boolean translatable(LucenePushdownPredicates pushdownPredicates) {
}

@Override
public Query asQuery(TranslatorHandler handler) {
public Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHandler handler) {
var fa = LucenePushdownPredicates.checkIsFieldAttribute(ipField);
Check.isTrue(Expressions.foldable(matches), "Expected foldable matches, but got [{}]", matches);

Expand Down
Loading
Loading