|
| 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 | +} |
0 commit comments