Skip to content

Commit 2cc34fd

Browse files
authored
Create index mapping from FieldCaps response (#137962)
1 parent 65cbc87 commit 2cc34fd

File tree

2 files changed

+137
-0
lines changed

2 files changed

+137
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.session;
9+
10+
import org.elasticsearch.common.Strings;
11+
import org.elasticsearch.xpack.esql.action.AbstractCrossClusterTestCase;
12+
13+
import java.util.Map;
14+
import java.util.Set;
15+
16+
import static org.hamcrest.Matchers.allOf;
17+
import static org.hamcrest.Matchers.hasEntry;
18+
19+
public class EsqlResolvedIndexExpressionIT extends AbstractCrossClusterTestCase {
20+
21+
public void testLocalIndices() {
22+
createIndex(LOCAL_CLUSTER, "index-1");
23+
24+
assertThat(resolveIndices("index-1"), hasEntry(LOCAL_CLUSTER, resolvedIndexExpression("index-1", "index-1")));
25+
}
26+
27+
public void testLocalAlias() {
28+
createIndex(LOCAL_CLUSTER, "index-1");
29+
createAlias(LOCAL_CLUSTER, "alias-1", "index-1");
30+
31+
assertThat(resolveIndices("alias-1"), hasEntry(LOCAL_CLUSTER, resolvedIndexExpression("alias-1", "index-1")));
32+
}
33+
34+
public void testLocalPattern() {
35+
createIndex(LOCAL_CLUSTER, "index-1");
36+
createIndex(LOCAL_CLUSTER, "index-2");
37+
38+
assertThat(resolveIndices("index-*"), hasEntry(LOCAL_CLUSTER, resolvedIndexExpression("index-*", "index-1,index-2")));
39+
}
40+
41+
public void testLocalMultiple() {
42+
createIndex(LOCAL_CLUSTER, "index-1");
43+
createIndex(LOCAL_CLUSTER, "index-2");
44+
45+
assertThat(
46+
resolveIndices("index-1,index-2"),
47+
hasEntry(LOCAL_CLUSTER, resolvedIndexExpression("index-1,index-2", "index-1,index-2"))
48+
);
49+
}
50+
51+
public void testLocalAndRemote() {
52+
createIndex(LOCAL_CLUSTER, "index-1");
53+
createIndex(REMOTE_CLUSTER_1, "index-2");
54+
createIndex(REMOTE_CLUSTER_2, "index-3");
55+
56+
assertThat(
57+
resolveIndices("index-*,*:index-*"),
58+
allOf(
59+
hasEntry(LOCAL_CLUSTER, resolvedIndexExpression("index-*", "index-1")),
60+
hasEntry(REMOTE_CLUSTER_1, resolvedIndexExpression("index-*", "index-2")),
61+
hasEntry(REMOTE_CLUSTER_2, resolvedIndexExpression("index-*", "index-3"))
62+
)
63+
);
64+
}
65+
66+
private Map<String, EsqlResolvedIndexExpression> resolveIndices(String indices) {
67+
return EsqlResolvedIndexExpression.from(
68+
client(LOCAL_CLUSTER).prepareFieldCaps(Strings.splitStringByCommaToArray(indices))
69+
.setFields("*")
70+
.setIncludeResolvedTo(true)
71+
.get()
72+
);
73+
}
74+
75+
private void createIndex(String clusterAlias, String index) {
76+
client(clusterAlias).admin().indices().prepareCreate(index).get();
77+
}
78+
79+
private void createAlias(String clusterAlias, String alias, String index) {
80+
client(clusterAlias).admin().indices().prepareAliases(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT).addAlias(index, alias).get();
81+
}
82+
83+
private static EsqlResolvedIndexExpression resolvedIndexExpression(String expression, String resolved) {
84+
return new EsqlResolvedIndexExpression(
85+
Set.of(Strings.splitStringByCommaToArray(expression)),
86+
Set.of(Strings.splitStringByCommaToArray(resolved))
87+
);
88+
}
89+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License
4+
* 2.0; you may not use this file except in compliance with the Elastic License
5+
* 2.0.
6+
*/
7+
8+
package org.elasticsearch.xpack.esql.session;
9+
10+
import org.elasticsearch.action.fieldcaps.FieldCapabilitiesResponse;
11+
import org.elasticsearch.common.util.set.Sets;
12+
import org.elasticsearch.transport.RemoteClusterAware;
13+
14+
import java.util.Map;
15+
import java.util.Set;
16+
import java.util.stream.Stream;
17+
18+
import static java.util.stream.Collectors.toMap;
19+
import static org.elasticsearch.action.ResolvedIndexExpression.LocalIndexResolutionResult.SUCCESS;
20+
21+
public record EsqlResolvedIndexExpression(Set<String> expression, Set<String> resolved) {
22+
23+
private static final EsqlResolvedIndexExpression EMPTY = new EsqlResolvedIndexExpression(Set.of(), Set.of());
24+
25+
public static Map<String, EsqlResolvedIndexExpression> from(FieldCapabilitiesResponse response) {
26+
return Stream.concat(
27+
Stream.of(Map.entry(RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY, response.getResolvedLocally())),
28+
response.getResolvedRemotely().entrySet().stream()
29+
)
30+
.map(
31+
entry -> Map.entry(
32+
entry.getKey(),
33+
entry.getValue()
34+
.expressions()
35+
.stream()
36+
.filter(e -> e.localExpressions().indices().isEmpty() == false)
37+
.filter(e -> e.localExpressions().localIndexResolutionResult() == SUCCESS)
38+
.map(e -> new EsqlResolvedIndexExpression(Set.of(e.original()), e.localExpressions().indices()))
39+
.reduce(EMPTY, EsqlResolvedIndexExpression::merge)
40+
)
41+
)
42+
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));
43+
}
44+
45+
private static EsqlResolvedIndexExpression merge(EsqlResolvedIndexExpression a, EsqlResolvedIndexExpression b) {
46+
return new EsqlResolvedIndexExpression(Sets.union(a.expression(), b.expression()), Sets.union(a.resolved(), b.resolved()));
47+
}
48+
}

0 commit comments

Comments
 (0)