Skip to content

ESQL: Fix wildcard for _index field #129650

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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/129650.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 129650
summary: Fix wildcard for `_index` field
area: ES|QL
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.query.QueryRewriteContext;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.logging.LogManager;

import java.util.Collection;
import java.util.Map;
Expand Down Expand Up @@ -128,6 +129,7 @@ public final Query wildcardQuery(
}

public final Query wildcardQuery(String value, boolean caseInsensitive, QueryRewriteContext context) {
LogManager.getLogger(ConstantFieldType.class).error("ADSFA const eval {} {}", value, matches(value, caseInsensitive, context));
if (matches(value, caseInsensitive, context)) {
return Queries.newMatchAllQuery();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class IndexFieldMapper extends MetadataFieldMapper {

public static final TypeParser PARSER = new FixedTypeParser(c -> INSTANCE);

static final class IndexFieldType extends ConstantFieldType {
public static final class IndexFieldType extends ConstantFieldType {

static final IndexFieldType INSTANCE = new IndexFieldType();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
import org.elasticsearch.cluster.service.ClusterService;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.index.mapper.ConstantFieldType;
import org.elasticsearch.logging.LogManager;
import org.elasticsearch.transport.RemoteClusterAware;

import java.util.function.Predicate;
Expand Down Expand Up @@ -53,7 +55,9 @@ public SearchIndexNameMatcher(
* the separator ':', and must match on both the cluster alias and index name.
*/
public boolean test(String pattern) {

String[] splitIndex = RemoteClusterAware.splitIndexName(pattern);
LogManager.getLogger(ConstantFieldType.class).error("ADSFA {}", (Object) splitIndex);

if (splitIndex[0] == null) {
return clusterAlias == null && matchesIndex(pattern);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,24 @@
*/
package org.elasticsearch.xpack.esql.core.querydsl.query;

import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.MultiTermQuery;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.xcontent.LoggingDeprecationHandler;
import org.elasticsearch.index.mapper.IndexFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.elasticsearch.index.query.support.QueryParsers;
import org.elasticsearch.logging.LogManager;
import org.elasticsearch.xpack.esql.core.tree.Source;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;

import static org.elasticsearch.index.query.QueryBuilders.wildcardQuery;

public class WildcardQuery extends Query {

private final String field, query;
Expand Down Expand Up @@ -44,9 +54,34 @@ public Boolean caseInsensitive() {

@Override
protected QueryBuilder asBuilder() {
WildcardQueryBuilder wb = wildcardQuery(field, query);
// ES does not allow case_insensitive to be set to "false", it should be either "true" or not specified
return caseInsensitive == false ? wb : wb.caseInsensitive(caseInsensitive);
/*
* Builds WildcardQueryBuilder with simple text matching semantics for
* all fields, including the `_index` field which insists on implementing
* some fairly unexpected matching rules.
*
* Note that
*/
return new WildcardQueryBuilder(field, query) {
@Override
protected org.apache.lucene.search.Query doToQuery(SearchExecutionContext context) throws IOException {
MappedFieldType fieldType = context.getFieldType(fieldName());
MultiTermQuery.RewriteMethod method = QueryParsers.parseRewriteMethod(rewrite(), null, LoggingDeprecationHandler.INSTANCE);
LogManager.getLogger(WildcardQuery.class).error("ADSFA special query {}", fieldType);
if (fieldType instanceof IndexFieldMapper.IndexFieldType) {
String value = value();
String indexName = context.getFullyQualifiedIndex().getName();
if (WildcardQuery.this.caseInsensitive) {
value = value.toLowerCase(Locale.ROOT);
indexName = indexName.toLowerCase(Locale.ROOT);
}
if (Regex.simpleMatch(value, indexName)) {
return new MatchAllDocsQuery();
}
return new MatchNoDocsQuery();
}
return fieldType.wildcardQuery(value(), method, WildcardQuery.this.caseInsensitive, context);
}
};
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,64 @@ public void testStats() throws IOException {
assertThat(clusterData, hasKey("took"));
}

public void testLikeIndex() throws Exception {
{
boolean includeCCSMetadata = includeCCSMetadata();
Map<String, Object> result = run("""
FROM test-local-index,*:test-remote-index METADATA _index
| WHERE _index LIKE "*remote*"
| STATS c = COUNT(*) BY _index
| SORT _index ASC
""", includeCCSMetadata);
var columns = List.of(Map.of("name", "c", "type", "long"), Map.of("name", "_index", "type", "keyword"));
var values = List.of(List.of(remoteDocs.size(), REMOTE_CLUSTER_NAME + ":" + remoteIndex));

assertResultMap(includeCCSMetadata, result, columns, values, false);
}
{
boolean includeCCSMetadata = includeCCSMetadata();
Map<String, Object> result = run("""
FROM test-local-index,*:test-remote-index METADATA _index
| WHERE _index NOT LIKE "*remote*"
| STATS c = COUNT(*) BY _index
| SORT _index ASC
""", includeCCSMetadata);
var columns = List.of(Map.of("name", "c", "type", "long"), Map.of("name", "_index", "type", "keyword"));
var values = List.of(List.of(localDocs.size(), localIndex));

assertResultMap(includeCCSMetadata, result, columns, values, false);
}
}

public void testRLikeIndex() throws Exception {
{
boolean includeCCSMetadata = includeCCSMetadata();
Map<String, Object> result = run("""
FROM test-local-index,*:test-remote-index METADATA _index
| WHERE _index RLIKE ".*remote.*"
| STATS c = COUNT(*) BY _index
| SORT _index ASC
""", includeCCSMetadata);
var columns = List.of(Map.of("name", "c", "type", "long"), Map.of("name", "_index", "type", "keyword"));
var values = List.of(List.of(remoteDocs.size(), REMOTE_CLUSTER_NAME + ":" + remoteIndex));

assertResultMap(includeCCSMetadata, result, columns, values, false);
}
{
boolean includeCCSMetadata = includeCCSMetadata();
Map<String, Object> result = run("""
FROM test-local-index,*:test-remote-index METADATA _index
| WHERE _index NOT RLIKE ".*remote.*"
| STATS c = COUNT(*) BY _index
| SORT _index ASC
""", includeCCSMetadata);
var columns = List.of(Map.of("name", "c", "type", "long"), Map.of("name", "_index", "type", "keyword"));
var values = List.of(List.of(localDocs.size(), localIndex));

assertResultMap(includeCCSMetadata, result, columns, values, false);
}
}

private RestClient remoteClusterClient() throws IOException {
var clusterHosts = parseClusterHosts(remoteCluster.getHttpAddresses());
return buildClient(restClientSettings(), clusterHosts.toArray(new HttpHost[0]));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.compute.operator.EvalOperator;
import org.elasticsearch.logging.LogManager;
import org.elasticsearch.xpack.esql.EsqlIllegalArgumentException;
import org.elasticsearch.xpack.esql.capabilities.TranslationAware;
import org.elasticsearch.xpack.esql.core.expression.Expression;
Expand Down Expand Up @@ -48,6 +49,7 @@ public Boolean fold(FoldContext ctx) {

@Override
public EvalOperator.ExpressionEvaluator.Factory toEvaluator(ToEvaluator toEvaluator) {
LogManager.getLogger(WildcardLike.class).error("ADSFA toEvaluator");
return AutomataMatch.toEvaluator(
source(),
toEvaluator.apply(field()),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.elasticsearch.common.io.stream.NamedWriteableRegistry;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.logging.LogManager;
import org.elasticsearch.xpack.esql.core.expression.Expression;
import org.elasticsearch.xpack.esql.core.expression.FieldAttribute;
import org.elasticsearch.xpack.esql.core.expression.predicate.regex.WildcardPattern;
Expand Down Expand Up @@ -108,13 +109,21 @@ protected WildcardLike replaceChild(Expression newLeft) {

@Override
public Translatable translatable(LucenePushdownPredicates pushdownPredicates) {
LogManager.getLogger(WildcardLike.class).error("ADSFA translatable", new Exception());
return pushdownPredicates.isPushableAttribute(field()) ? Translatable.YES : Translatable.NO;
}

@Override
public Query asQuery(LucenePushdownPredicates pushdownPredicates, TranslatorHandler handler) {
var field = field();
LucenePushdownPredicates.checkIsPushableAttribute(field);
LogManager.getLogger(WildcardLike.class)
.error(
"ADSFA asQuery {} {}",
field,
translateField(handler.nameOf(field instanceof FieldAttribute fa ? fa.exactAttribute() : field)),
new Exception()
);
return translateField(handler.nameOf(field instanceof FieldAttribute fa ? fa.exactAttribute() : field));
}

Expand Down
Loading