|
23 | 23 | import org.elasticsearch.common.util.concurrent.ThreadContext;
|
24 | 24 | import org.elasticsearch.common.util.iterable.Iterables;
|
25 | 25 | import org.elasticsearch.core.Tuple;
|
| 26 | +import org.elasticsearch.index.query.QueryBuilder; |
26 | 27 | import org.elasticsearch.tasks.Task;
|
27 | 28 | import org.elasticsearch.threadpool.ThreadPool;
|
28 | 29 | import org.elasticsearch.transport.AbstractTransportRequest;
|
|
39 | 40 | import org.elasticsearch.xpack.core.enrich.EnrichPolicy;
|
40 | 41 | import org.elasticsearch.xpack.esql.action.EsqlExecutionInfo;
|
41 | 42 | import org.elasticsearch.xpack.esql.analysis.EnrichResolution;
|
| 43 | +import org.elasticsearch.xpack.esql.analysis.PreAnalyzer; |
42 | 44 | import org.elasticsearch.xpack.esql.core.type.DataType;
|
43 | 45 | import org.elasticsearch.xpack.esql.core.type.EsField;
|
44 | 46 | import org.elasticsearch.xpack.esql.core.util.StringUtils;
|
45 | 47 | import org.elasticsearch.xpack.esql.index.EsIndex;
|
| 48 | +import org.elasticsearch.xpack.esql.index.MappingException; |
46 | 49 | import org.elasticsearch.xpack.esql.io.stream.PlanStreamInput;
|
47 | 50 | import org.elasticsearch.xpack.esql.io.stream.PlanStreamOutput;
|
| 51 | +import org.elasticsearch.xpack.esql.plan.IndexPattern; |
48 | 52 | import org.elasticsearch.xpack.esql.plan.logical.Enrich;
|
| 53 | +import org.elasticsearch.xpack.esql.session.EsqlCCSUtils; |
49 | 54 | import org.elasticsearch.xpack.esql.session.IndexResolver;
|
50 | 55 |
|
51 | 56 | import java.io.IOException;
|
52 | 57 | import java.util.ArrayList;
|
53 | 58 | import java.util.Collection;
|
54 | 59 | import java.util.Collections;
|
55 | 60 | import java.util.HashMap;
|
56 |
| -import java.util.HashSet; |
57 | 61 | import java.util.List;
|
58 | 62 | import java.util.Map;
|
59 | 63 | import java.util.Set;
|
@@ -110,22 +114,37 @@ public static UnresolvedPolicy from(Enrich e) {
|
110 | 114 | /**
|
111 | 115 | * Resolves a set of enrich policies
|
112 | 116 | *
|
113 |
| - * @param enriches the unresolved policies |
| 117 | + * @param preAnalysis to retrieve indices and enriches to resolve |
| 118 | + * @param requestFilter to resolve target clusters |
114 | 119 | * @param executionInfo the execution info
|
115 | 120 | * @param listener notified with the enrich resolution
|
116 | 121 | */
|
117 |
| - public void resolvePolicies(List<Enrich> enriches, EsqlExecutionInfo executionInfo, ActionListener<EnrichResolution> listener) { |
118 |
| - if (enriches.isEmpty()) { |
| 122 | + public void resolvePolicies( |
| 123 | + PreAnalyzer.PreAnalysis preAnalysis, |
| 124 | + QueryBuilder requestFilter, |
| 125 | + EsqlExecutionInfo executionInfo, |
| 126 | + ActionListener<EnrichResolution> listener |
| 127 | + ) { |
| 128 | + if (preAnalysis.enriches.isEmpty()) { |
119 | 129 | listener.onResponse(new EnrichResolution());
|
120 | 130 | return;
|
121 | 131 | }
|
122 | 132 |
|
123 |
| - doResolvePolicies( |
124 |
| - new HashSet<>(executionInfo.getClusters().keySet()), |
125 |
| - enriches.stream().map(EnrichPolicyResolver.UnresolvedPolicy::from).toList(), |
126 |
| - executionInfo, |
127 |
| - listener |
128 |
| - ); |
| 133 | + doResolveRemotes(preAnalysis.indices, requestFilter, listener.delegateFailureAndWrap((l, remotes) -> { |
| 134 | + doResolvePolicies(remotes, preAnalysis.enriches.stream().map(UnresolvedPolicy::from).toList(), executionInfo, l); |
| 135 | + })); |
| 136 | + } |
| 137 | + |
| 138 | + private void doResolveRemotes(List<IndexPattern> indexPatterns, QueryBuilder requestFilter, ActionListener<Set<String>> listener) { |
| 139 | + switch (indexPatterns.size()) { |
| 140 | + case 0 -> listener.onResponse(Set.of()); |
| 141 | + case 1 -> indexResolver.resolveConcreteIndices( |
| 142 | + indexPatterns.getFirst().indexPattern(), |
| 143 | + requestFilter, |
| 144 | + listener.map(EsqlCCSUtils::getRemotesOf) |
| 145 | + ); |
| 146 | + default -> listener.onFailure(new MappingException("Queries with multiple indices are not supported")); |
| 147 | + } |
129 | 148 | }
|
130 | 149 |
|
131 | 150 | protected void doResolvePolicies(
|
@@ -442,7 +461,7 @@ public void messageReceived(LookupRequest request, TransportChannel channel, Tas
|
442 | 461 | if (p == null) {
|
443 | 462 | continue;
|
444 | 463 | }
|
445 |
| - try (ThreadContext.StoredContext ignored = threadContext.stashWithOrigin(ClientHelper.ENRICH_ORIGIN)) { |
| 464 | + try (var ignored = threadContext.stashWithOrigin(ClientHelper.ENRICH_ORIGIN)) { |
446 | 465 | String indexName = EnrichPolicy.getBaseName(policyName);
|
447 | 466 | indexResolver.resolveAsMergedMapping(indexName, IndexResolver.ALL_FIELDS, null, false, refs.acquire(indexResult -> {
|
448 | 467 | if (indexResult.isValid() && indexResult.get().concreteIndices().size() == 1) {
|
|
0 commit comments