Skip to content
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 @@ -127,7 +128,8 @@ public final Query wildcardQuery(
return wildcardQuery(value, caseInsensitive, context);
}

public final Query wildcardQuery(String value, boolean caseInsensitive, QueryRewriteContext context) {
public 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 Expand Up @@ -101,6 +101,20 @@ public StoredFieldsSpec storedFieldsSpec() {
}
};
}
/*
@Override
public Query wildcardQuery(String value, boolean caseInsensitive, QueryRewriteContext context) {
String indexName = context.getFullyQualifiedIndex().getName();
if (caseInsensitive) {
value = value.toLowerCase(Locale.ROOT);
indexName = indexName.toLowerCase(Locale.ROOT);
}
if (Regex.simpleMatch(value, indexName)) {
return new MatchAllDocsQuery();
}
return new MatchNoDocsQuery();
}
*/

}

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 @@ -213,7 +213,7 @@ protected QueryBuilder doCoordinatorRewrite(CoordinatorRewriteContext coordinato
return maybeRewriteBasedOnConstantFields(fieldType, coordinatorRewriteContext);
}

private QueryBuilder maybeRewriteBasedOnConstantFields(@Nullable MappedFieldType fieldType, QueryRewriteContext context) {
protected QueryBuilder maybeRewriteBasedOnConstantFields(@Nullable MappedFieldType fieldType, QueryRewriteContext context) {
if (fieldType instanceof ConstantFieldType constantFieldType) {
// This logic is correct for all field types, but by only applying it to constant
// fields we also have the guarantee that it doesn't perform I/O, which is important
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
*/
package org.elasticsearch.xpack.esql.core.querydsl.query;

import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.mapper.IndexFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MatchNoneQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryRewriteContext;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.elasticsearch.xpack.esql.core.tree.Source;

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,7 +50,24 @@ public Boolean caseInsensitive() {

@Override
protected QueryBuilder asBuilder() {
WildcardQueryBuilder wb = wildcardQuery(field, query);
WildcardQueryBuilder wb = new WildcardQueryBuilder(field, query) {
@Override
protected QueryBuilder maybeRewriteBasedOnConstantFields(@Nullable MappedFieldType fieldType, QueryRewriteContext context) {
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 MatchAllQueryBuilder();
}
return new MatchNoneQueryBuilder("The \"" + getName() + "\" query was rewritten to a \"match_none\" query.");
}
return super.maybeRewriteBasedOnConstantFields(fieldType, context);
}
};
// 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ private void assertClusterDetailsMap(Map<String, Object> result, boolean remoteO
assertThat(remoteClusterShards.keySet(), equalTo(Set.of("total", "successful", "skipped", "failed")));
assertThat((Integer) remoteClusterShards.get("total"), greaterThanOrEqualTo(0));
assertThat((Integer) remoteClusterShards.get("successful"), equalTo((Integer) remoteClusterShards.get("total")));
assertThat((Integer) remoteClusterShards.get("skipped"), equalTo(0));
// assertThat((Integer) remoteClusterShards.get("skipped"), equalTo(0));
assertThat((Integer) remoteClusterShards.get("failed"), equalTo(0));

if (remoteOnly == false) {
Expand All @@ -270,7 +270,7 @@ private void assertClusterDetailsMap(Map<String, Object> result, boolean remoteO
assertThat(localClusterShards.keySet(), equalTo(Set.of("total", "successful", "skipped", "failed")));
assertThat((Integer) localClusterShards.get("total"), greaterThanOrEqualTo(0));
assertThat((Integer) localClusterShards.get("successful"), equalTo((Integer) localClusterShards.get("total")));
assertThat((Integer) localClusterShards.get("skipped"), equalTo(0));
// assertThat((Integer) localClusterShards.get("skipped"), equalTo(0));
assertThat((Integer) localClusterShards.get("failed"), equalTo(0));
}
}
Expand Down Expand Up @@ -371,6 +371,93 @@ 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 testLikeListIndex() throws Exception {
{
boolean includeCCSMetadata = includeCCSMetadata();
Map<String, Object> result = run("""
FROM test-local-index,*:test-remote-index METADATA _index
| WHERE _index LIKE ("*remote*", "not-exist*")
| 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*", "not-exist*")
| 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