|
| 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.cluster.metadata.IndexMetadata; |
| 11 | +import org.elasticsearch.common.settings.Settings; |
| 12 | +import org.elasticsearch.index.IndexMode; |
| 13 | +import org.elasticsearch.index.IndexSettings; |
| 14 | +import org.elasticsearch.xpack.esql.VerificationException; |
| 15 | +import org.elasticsearch.xpack.esql.action.AbstractEsqlIntegTestCase; |
| 16 | +import org.elasticsearch.xpack.esql.action.EsqlQueryResponse; |
| 17 | + |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 21 | +import static org.elasticsearch.xpack.esql.action.EsqlQueryRequest.syncEsqlQueryRequest; |
| 22 | +import static org.hamcrest.Matchers.containsString; |
| 23 | +import static org.hamcrest.Matchers.equalTo; |
| 24 | + |
| 25 | +public class LookupIndexResolutionIT extends AbstractEsqlIntegTestCase { |
| 26 | + |
| 27 | + public void testResolvesConcreteIndex() { |
| 28 | + createMainIndex("index-1"); |
| 29 | + createLookupIndex("lookup"); |
| 30 | + |
| 31 | + try (var response = run(syncEsqlQueryRequest().query("FROM index-1 | LOOKUP JOIN lookup ON language_code"))) { |
| 32 | + assertOk(response); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + public void testResolvesAlias() { |
| 37 | + createMainIndex("index-1"); |
| 38 | + createLookupIndex("lookup"); |
| 39 | + assertAcked( |
| 40 | + client().admin().indices().prepareAliases(TEST_REQUEST_TIMEOUT, TEST_REQUEST_TIMEOUT).addAlias("lookup", "lookup-alias") |
| 41 | + ); |
| 42 | + |
| 43 | + try (var response = run(syncEsqlQueryRequest().query("FROM index-1 | LOOKUP JOIN lookup-alias ON language_code"))) { |
| 44 | + assertOk(response); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + public void testDoesNotResolveMissingIndex() { |
| 49 | + createMainIndex("index-1"); |
| 50 | + |
| 51 | + expectThrows( |
| 52 | + VerificationException.class, |
| 53 | + containsString("Unknown index [fake-lookup]"), |
| 54 | + () -> run(syncEsqlQueryRequest().query("FROM index-1 | LOOKUP JOIN fake-lookup ON language_code")) |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + private static void assertOk(EsqlQueryResponse response) { |
| 59 | + assertThat(response.isPartial(), equalTo(false)); |
| 60 | + } |
| 61 | + |
| 62 | + private void createMainIndex(String name) { |
| 63 | + assertAcked(client().admin().indices().prepareCreate(name)); |
| 64 | + indexRandom(true, false, prepareIndex(name).setSource(Map.of("id", randomIdentifier(), "language_code", 1))); |
| 65 | + } |
| 66 | + |
| 67 | + private void createLookupIndex(String name) { |
| 68 | + assertAcked( |
| 69 | + client().admin() |
| 70 | + .indices() |
| 71 | + .prepareCreate(name) |
| 72 | + .setSettings( |
| 73 | + Settings.builder().put(IndexSettings.MODE.getKey(), IndexMode.LOOKUP).put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 74 | + ) |
| 75 | + ); |
| 76 | + indexRandom(true, false, prepareIndex(name).setSource(Map.of("language_code", 1, "language", "english"))); |
| 77 | + } |
| 78 | +} |
0 commit comments