|  | 
|  | 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.plugin; | 
|  | 9 | + | 
|  | 10 | +import org.elasticsearch.action.admin.indices.template.put.TransportPutComposableIndexTemplateAction; | 
|  | 11 | +import org.elasticsearch.action.datastreams.CreateDataStreamAction; | 
|  | 12 | +import org.elasticsearch.cluster.block.ClusterBlockException; | 
|  | 13 | +import org.elasticsearch.cluster.metadata.ComposableIndexTemplate; | 
|  | 14 | +import org.elasticsearch.common.util.CollectionUtils; | 
|  | 15 | +import org.elasticsearch.datastreams.DataStreamsPlugin; | 
|  | 16 | +import org.elasticsearch.index.IndexNotFoundException; | 
|  | 17 | +import org.elasticsearch.plugins.Plugin; | 
|  | 18 | +import org.elasticsearch.xpack.esql.VerificationException; | 
|  | 19 | +import org.elasticsearch.xpack.esql.action.AbstractEsqlIntegTestCase; | 
|  | 20 | +import org.elasticsearch.xpack.esql.action.EsqlQueryResponse; | 
|  | 21 | + | 
|  | 22 | +import java.util.Collection; | 
|  | 23 | +import java.util.List; | 
|  | 24 | + | 
|  | 25 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; | 
|  | 26 | +import static org.elasticsearch.xpack.esql.action.EsqlQueryRequest.syncEsqlQueryRequest; | 
|  | 27 | +import static org.hamcrest.Matchers.containsString; | 
|  | 28 | +import static org.hamcrest.Matchers.equalTo; | 
|  | 29 | + | 
|  | 30 | +public class IndexResolutionIT extends AbstractEsqlIntegTestCase { | 
|  | 31 | + | 
|  | 32 | +    @Override | 
|  | 33 | +    protected Collection<Class<? extends Plugin>> nodePlugins() { | 
|  | 34 | +        return CollectionUtils.appendToCopy(super.nodePlugins(), DataStreamsPlugin.class); | 
|  | 35 | +    } | 
|  | 36 | + | 
|  | 37 | +    public void testResolvesConcreteIndex() { | 
|  | 38 | +        assertAcked(client().admin().indices().prepareCreate("index-1")); | 
|  | 39 | +        indexRandom(true, "index-1", 10); | 
|  | 40 | + | 
|  | 41 | +        try (var response = run(syncEsqlQueryRequest().query("FROM index-1"))) { | 
|  | 42 | +            assertOk(response); | 
|  | 43 | +        } | 
|  | 44 | +    } | 
|  | 45 | + | 
|  | 46 | +    public void testResolvesAlias() { | 
|  | 47 | +        assertAcked(client().admin().indices().prepareCreate("index-1")); | 
|  | 48 | +        indexRandom(true, "index-1", 10); | 
|  | 49 | +        assertAcked(client().admin().indices().prepareAliases(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT).addAlias("index-1", "alias-1")); | 
|  | 50 | + | 
|  | 51 | +        try (var response = run(syncEsqlQueryRequest().query("FROM alias-1"))) { | 
|  | 52 | +            assertOk(response); | 
|  | 53 | +        } | 
|  | 54 | +    } | 
|  | 55 | + | 
|  | 56 | +    public void testResolvesDataStream() { | 
|  | 57 | +        assertAcked( | 
|  | 58 | +            client().execute( | 
|  | 59 | +                TransportPutComposableIndexTemplateAction.TYPE, | 
|  | 60 | +                new TransportPutComposableIndexTemplateAction.Request("data-stream-1-template").indexTemplate( | 
|  | 61 | +                    ComposableIndexTemplate.builder() | 
|  | 62 | +                        .indexPatterns(List.of("data-stream-1*")) | 
|  | 63 | +                        .dataStreamTemplate(new ComposableIndexTemplate.DataStreamTemplate()) | 
|  | 64 | +                        .build() | 
|  | 65 | +                ) | 
|  | 66 | +            ) | 
|  | 67 | +        ); | 
|  | 68 | +        assertAcked( | 
|  | 69 | +            client().execute( | 
|  | 70 | +                CreateDataStreamAction.INSTANCE, | 
|  | 71 | +                new CreateDataStreamAction.Request(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT, "data-stream-1") | 
|  | 72 | +            ) | 
|  | 73 | +        ); | 
|  | 74 | + | 
|  | 75 | +        try (var response = run(syncEsqlQueryRequest().query("FROM data-stream-1"))) { | 
|  | 76 | +            assertOk(response); | 
|  | 77 | +        } | 
|  | 78 | +    } | 
|  | 79 | + | 
|  | 80 | +    public void testResolvesPattern() { | 
|  | 81 | +        assertAcked(client().admin().indices().prepareCreate("index-1")); | 
|  | 82 | +        indexRandom(true, "index-1", 10); | 
|  | 83 | +        assertAcked(client().admin().indices().prepareCreate("index-2")); | 
|  | 84 | +        indexRandom(true, "index-2", 10); | 
|  | 85 | + | 
|  | 86 | +        try (var response = run(syncEsqlQueryRequest().query("FROM index-*"))) { | 
|  | 87 | +            assertOk(response); | 
|  | 88 | +        } | 
|  | 89 | +    } | 
|  | 90 | + | 
|  | 91 | +    public void testDoesNotResolveMissingIndex() { | 
|  | 92 | +        expectThrows( | 
|  | 93 | +            VerificationException.class, | 
|  | 94 | +            containsString("Unknown index [no-such-index]"), | 
|  | 95 | +            () -> run(syncEsqlQueryRequest().query("FROM no-such-index")) | 
|  | 96 | +        ); | 
|  | 97 | +    } | 
|  | 98 | + | 
|  | 99 | +    public void testDoesNotResolveEmptyPattern() { | 
|  | 100 | +        expectThrows( | 
|  | 101 | +            VerificationException.class, | 
|  | 102 | +            containsString("Unknown index [index-*]"), | 
|  | 103 | +            () -> run(syncEsqlQueryRequest().query("FROM index-*")) | 
|  | 104 | +        ); | 
|  | 105 | +    } | 
|  | 106 | + | 
|  | 107 | +    public void testDoesNotResolveClosedIndex() { | 
|  | 108 | +        assertAcked(client().admin().indices().prepareCreate("index-1")); | 
|  | 109 | +        indexRandom(true, "index-1", 10); | 
|  | 110 | +        assertAcked(client().admin().indices().prepareClose("index-1")); | 
|  | 111 | + | 
|  | 112 | +        expectThrows( | 
|  | 113 | +            ClusterBlockException.class, | 
|  | 114 | +            containsString("index [index-1] blocked by: [FORBIDDEN/4/index closed]"), | 
|  | 115 | +            () -> run(syncEsqlQueryRequest().query("FROM index-1")) | 
|  | 116 | +        ); | 
|  | 117 | +    } | 
|  | 118 | + | 
|  | 119 | +    public void testPartialResolution() { | 
|  | 120 | +        assertAcked(client().admin().indices().prepareCreate("index-1")); | 
|  | 121 | +        assertAcked(client().admin().indices().prepareCreate("index-2")); | 
|  | 122 | +        indexRandom(true, "index-2", 10); | 
|  | 123 | + | 
|  | 124 | +        try (var response = run(syncEsqlQueryRequest().query("FROM index-1,nonexisting"))) { | 
|  | 125 | +            assertOk(response); | 
|  | 126 | +        } | 
|  | 127 | +        expectThrows( | 
|  | 128 | +            IndexNotFoundException.class, | 
|  | 129 | +            equalTo("no such index [nonexisting]"), | 
|  | 130 | +            () -> run(syncEsqlQueryRequest().query("FROM index-2,nonexisting")) | 
|  | 131 | +        ); | 
|  | 132 | +    } | 
|  | 133 | + | 
|  | 134 | +    private static void assertOk(EsqlQueryResponse response) { | 
|  | 135 | +        assertThat(response.isPartial(), equalTo(false)); | 
|  | 136 | +    } | 
|  | 137 | +} | 
0 commit comments