diff --git a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java index e28fbead29abd..f18efe4585bc9 100644 --- a/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java +++ b/modules/percolator/src/main/java/org/elasticsearch/percolator/PercolateQueryBuilder.java @@ -756,7 +756,7 @@ public BitSetProducer bitsetFilter(Query query) { @Override @SuppressWarnings("unchecked") public > IFD getForField(MappedFieldType fieldType) { - IndexFieldData.Builder builder = fieldType.fielddataBuilder(shardContext.getFullyQualifiedIndexName()); + IndexFieldData.Builder builder = fieldType.fielddataBuilder(shardContext.getFullyQualifiedIndex().getName()); IndexFieldDataCache cache = new IndexFieldDataCache.None(); CircuitBreakerService circuitBreaker = new NoneCircuitBreakerService(); return (IFD) builder.build(shardContext.getIndexSettings(), fieldType, cache, circuitBreaker, @@ -764,5 +764,4 @@ public > IFD getForField(MappedFieldType fieldType } }; } - } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java index 4061303416b7c..bb048ab9afac4 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/IndexFieldMapper.java @@ -126,7 +126,7 @@ public Query existsQuery(QueryShardContext context) { */ @Override public Query termQuery(Object value, @Nullable QueryShardContext context) { - if (isSameIndex(value, context.getFullyQualifiedIndexName())) { + if (isSameIndex(value, context.getFullyQualifiedIndex().getName())) { return Queries.newMatchAllQuery(); } else { return Queries.newMatchNoDocsQuery("Index didn't match. Index queried: " + context.index().getName() + " vs. " + value); @@ -139,14 +139,14 @@ public Query termsQuery(List values, QueryShardContext context) { return super.termsQuery(values, context); } for (Object value : values) { - if (isSameIndex(value, context.getFullyQualifiedIndexName())) { + if (isSameIndex(value, context.getFullyQualifiedIndex().getName())) { // No need to OR these clauses - we can only logically be // running in the context of just one of these index names. return Queries.newMatchAllQuery(); } } // None of the listed index names are this one - return Queries.newMatchNoDocsQuery("Index didn't match. Index queried: " + context.getFullyQualifiedIndexName() + return Queries.newMatchNoDocsQuery("Index didn't match. Index queried: " + context.getFullyQualifiedIndex().getName() + " vs. " + values); } @@ -189,5 +189,4 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws protected void doMerge(Mapper mergeWith) { // nothing to do } - } diff --git a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java index 598a6f38a2ef8..ac19298ae322d 100644 --- a/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java +++ b/server/src/main/java/org/elasticsearch/index/query/QueryShardContext.java @@ -83,7 +83,7 @@ public class QueryShardContext extends QueryRewriteContext { private String[] types = Strings.EMPTY_ARRAY; private boolean cachable = true; private final SetOnce frozen = new SetOnce<>(); - private final String fullyQualifiedIndexName; + private final Index fullyQualifiedIndex; public void setTypes(String... types) { this.types = types; @@ -116,7 +116,8 @@ public QueryShardContext(int shardId, IndexSettings indexSettings, BitsetFilterC this.indexSettings = indexSettings; this.reader = reader; this.clusterAlias = clusterAlias; - this.fullyQualifiedIndexName = RemoteClusterAware.buildRemoteIndexName(clusterAlias, indexSettings.getIndex().getName()); + this.fullyQualifiedIndex = new Index(RemoteClusterAware.buildRemoteIndexName(clusterAlias, indexSettings.getIndex().getName()), + indexSettings.getIndex().getUUID()); } public QueryShardContext(QueryShardContext source) { @@ -163,7 +164,7 @@ public BitSetProducer bitsetFilter(Query filter) { } public > IFD getForField(MappedFieldType fieldType) { - return (IFD) indexFieldDataService.apply(fieldType, fullyQualifiedIndexName); + return (IFD) indexFieldDataService.apply(fieldType, fullyQualifiedIndex.getName()); } public void addNamedQuery(String name, Query query) { @@ -275,7 +276,7 @@ public Collection queryTypes() { public SearchLookup lookup() { if (lookup == null) { lookup = new SearchLookup(getMapperService(), - mappedFieldType -> indexFieldDataService.apply(mappedFieldType, fullyQualifiedIndexName), types); + mappedFieldType -> indexFieldDataService.apply(mappedFieldType, fullyQualifiedIndex.getName()), types); } return lookup; } @@ -426,9 +427,9 @@ public IndexReader getIndexReader() { } /** - * Returns the fully qualified index name including a remote cluster alias if applicable + * Returns the fully qualified index including a remote cluster alias if applicable, and the index uuid */ - public String getFullyQualifiedIndexName() { - return fullyQualifiedIndexName; + public Index getFullyQualifiedIndex() { + return fullyQualifiedIndex; } } diff --git a/server/src/main/java/org/elasticsearch/index/query/QueryShardException.java b/server/src/main/java/org/elasticsearch/index/query/QueryShardException.java index b52bc07ca78a7..843ff931d4fc2 100644 --- a/server/src/main/java/org/elasticsearch/index/query/QueryShardException.java +++ b/server/src/main/java/org/elasticsearch/index/query/QueryShardException.java @@ -37,16 +37,15 @@ public QueryShardException(QueryShardContext context, String msg, Object... args } public QueryShardException(QueryShardContext context, String msg, Throwable cause, Object... args) { - super(msg, cause, args); - setIndex(context.getFullyQualifiedIndexName()); + this(context.getFullyQualifiedIndex(), msg, cause, args); } /** * This constructor is provided for use in unit tests where a * {@link QueryShardContext} may not be available */ - public QueryShardException(Index index, String msg, Throwable cause) { - super(msg, cause); + public QueryShardException(Index index, String msg, Throwable cause, Object... args) { + super(msg, cause, args); setIndex(index); } diff --git a/server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java b/server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java index 39cc3c8219ceb..b27b9e26ab7dc 100644 --- a/server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/QueryShardContextTests.java @@ -22,8 +22,10 @@ import org.apache.lucene.search.Query; import org.elasticsearch.Version; import org.elasticsearch.cluster.metadata.IndexMetaData; +import org.elasticsearch.common.io.stream.NamedWriteableRegistry; import org.elasticsearch.common.lucene.search.Queries; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.NamedXContentRegistry; import org.elasticsearch.index.IndexSettings; import org.elasticsearch.index.fielddata.IndexFieldData; import org.elasticsearch.index.fielddata.plain.AbstractAtomicOrdinalsFieldData; @@ -37,6 +39,7 @@ import org.hamcrest.Matchers; import java.io.IOException; +import java.util.Collections; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -49,24 +52,7 @@ public class QueryShardContextTests extends ESTestCase { public void testFailIfFieldMappingNotFound() { - IndexMetaData.Builder indexMetadataBuilder = new IndexMetaData.Builder("index"); - indexMetadataBuilder.settings(Settings.builder().put("index.version.created", Version.CURRENT) - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 1) - ); - IndexMetaData indexMetaData = indexMetadataBuilder.build(); - IndexSettings indexSettings = new IndexSettings(indexMetaData, Settings.EMPTY); - MapperService mapperService = mock(MapperService.class); - when(mapperService.getIndexSettings()).thenReturn(indexSettings); - when(mapperService.index()).thenReturn(indexMetaData.getIndex()); - final long nowInMillis = randomNonNegativeLong(); - - QueryShardContext context = new QueryShardContext( - 0, indexSettings, null, (mappedFieldType, idxName) -> - mappedFieldType.fielddataBuilder(idxName).build(indexSettings, mappedFieldType, null, null, null) - , mapperService, null, null, xContentRegistry(), writableRegistry(), null, null, - () -> nowInMillis, null); - + QueryShardContext context = createQueryShardContext(IndexMetaData.INDEX_UUID_NA_VALUE, null); context.setAllowUnmappedFields(false); MappedFieldType fieldType = new TextFieldMapper.TextFieldType(); MappedFieldType result = context.failIfFieldMappingNotFound("name", fieldType); @@ -91,30 +77,16 @@ public void testFailIfFieldMappingNotFound() { } public void testClusterAlias() throws IOException { - IndexMetaData.Builder indexMetadataBuilder = new IndexMetaData.Builder("index"); - indexMetadataBuilder.settings(Settings.builder().put("index.version.created", Version.CURRENT) - .put("index.number_of_shards", 1) - .put("index.number_of_replicas", 1) - ); - IndexMetaData indexMetaData = indexMetadataBuilder.build(); - IndexSettings indexSettings = new IndexSettings(indexMetaData, Settings.EMPTY); - MapperService mapperService = mock(MapperService.class); - when(mapperService.getIndexSettings()).thenReturn(indexSettings); - when(mapperService.index()).thenReturn(indexMetaData.getIndex()); - final long nowInMillis = randomNonNegativeLong(); + final String clusterAlias = randomBoolean() ? null : "remote_cluster"; + QueryShardContext context = createQueryShardContext(IndexMetaData.INDEX_UUID_NA_VALUE, clusterAlias); - Mapper.BuilderContext ctx = new Mapper.BuilderContext(indexSettings.getSettings(), new ContentPath()); + + Mapper.BuilderContext ctx = new Mapper.BuilderContext(context.getIndexSettings().getSettings(), new ContentPath()); IndexFieldMapper mapper = new IndexFieldMapper.Builder(null).build(ctx); - final String clusterAlias = randomBoolean() ? null : "remote_cluster"; - QueryShardContext context = new QueryShardContext( - 0, indexSettings, null, (mappedFieldType, indexname) -> - mappedFieldType.fielddataBuilder(indexname).build(indexSettings, mappedFieldType, null, null, mapperService) - , mapperService, null, null, xContentRegistry(), writableRegistry(), null, null, - () -> nowInMillis, clusterAlias); IndexFieldData forField = context.getForField(mapper.fieldType()); - String expected = clusterAlias == null ? indexMetaData.getIndex().getName() - : clusterAlias + ":" + indexMetaData.getIndex().getName(); + String expected = clusterAlias == null ? context.getIndexSettings().getIndexMetaData().getIndex().getName() + : clusterAlias + ":" + context.getIndexSettings().getIndex().getName(); assertEquals(expected, ((AbstractAtomicOrdinalsFieldData)forField.load(null)).getOrdinalsValues().lookupOrd(0).utf8ToString()); Query query = mapper.fieldType().termQuery("index", context); if (clusterAlias == null) { @@ -133,4 +105,32 @@ public void testClusterAlias() throws IOException { assertThat(query, Matchers.instanceOf(MatchNoDocsQuery.class)); } + public void testGetFullyQualifiedIndex() { + String clusterAlias = randomAlphaOfLengthBetween(5, 10); + String indexUuid = randomAlphaOfLengthBetween(3, 10); + QueryShardContext shardContext = createQueryShardContext(indexUuid, clusterAlias); + assertThat(shardContext.getFullyQualifiedIndex().getName(), equalTo(clusterAlias + ":index")); + assertThat(shardContext.getFullyQualifiedIndex().getUUID(), equalTo(indexUuid)); + } + + public static QueryShardContext createQueryShardContext(String indexUuid, String clusterAlias) { + IndexMetaData.Builder indexMetadataBuilder = new IndexMetaData.Builder("index"); + indexMetadataBuilder.settings(Settings.builder().put("index.version.created", Version.CURRENT) + .put("index.number_of_shards", 1) + .put("index.number_of_replicas", 1) + .put(IndexMetaData.SETTING_INDEX_UUID, indexUuid) + ); + IndexMetaData indexMetaData = indexMetadataBuilder.build(); + IndexSettings indexSettings = new IndexSettings(indexMetaData, Settings.EMPTY); + MapperService mapperService = mock(MapperService.class); + when(mapperService.getIndexSettings()).thenReturn(indexSettings); + when(mapperService.index()).thenReturn(indexMetaData.getIndex()); + final long nowInMillis = randomNonNegativeLong(); + + return new QueryShardContext( + 0, indexSettings, null, (mappedFieldType, idxName) -> + mappedFieldType.fielddataBuilder(idxName).build(indexSettings, mappedFieldType, null, null, null) + , mapperService, null, null, NamedXContentRegistry.EMPTY, new NamedWriteableRegistry(Collections.emptyList()), null, null, + () -> nowInMillis, clusterAlias); + } } diff --git a/server/src/test/java/org/elasticsearch/index/query/QueryShardExceptionTests.java b/server/src/test/java/org/elasticsearch/index/query/QueryShardExceptionTests.java new file mode 100644 index 0000000000000..8557f568e078f --- /dev/null +++ b/server/src/test/java/org/elasticsearch/index/query/QueryShardExceptionTests.java @@ -0,0 +1,53 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.query; + +import org.elasticsearch.index.Index; +import org.elasticsearch.test.ESTestCase; + +import static org.hamcrest.CoreMatchers.equalTo; + +public class QueryShardExceptionTests extends ESTestCase { + + public void testCreateFromQueryShardContext() { + String indexUuid = randomAlphaOfLengthBetween(5, 10); + String clusterAlias = randomAlphaOfLengthBetween(5, 10); + QueryShardContext queryShardContext = QueryShardContextTests.createQueryShardContext(indexUuid, clusterAlias); + { + QueryShardException queryShardException = new QueryShardException(queryShardContext, "error"); + assertThat(queryShardException.getIndex().getName(), equalTo(clusterAlias + ":index")); + assertThat(queryShardException.getIndex().getUUID(), equalTo(indexUuid)); + } + { + QueryShardException queryShardException = new QueryShardException(queryShardContext, "error", new IllegalArgumentException()); + assertThat(queryShardException.getIndex().getName(), equalTo(clusterAlias + ":index")); + assertThat(queryShardException.getIndex().getUUID(), equalTo(indexUuid)); + } + } + + public void testCreateFromIndex() { + String indexUuid = randomAlphaOfLengthBetween(5, 10); + String indexName = randomAlphaOfLengthBetween(5, 10); + Index index = new Index(indexName, indexUuid); + QueryShardException queryShardException = new QueryShardException(index, "error", new IllegalArgumentException()); + assertThat(queryShardException.getIndex().getName(), equalTo(indexName)); + assertThat(queryShardException.getIndex().getUUID(), equalTo(indexUuid)); + } +} diff --git a/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java b/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java index 54f8536563d9c..73aa1667f30f4 100644 --- a/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/WildcardQueryBuilderTests.java @@ -32,7 +32,6 @@ import java.util.HashMap; import java.util.Map; -import static org.elasticsearch.test.AbstractBuilderTestCase.STRING_ALIAS_FIELD_NAME; import static org.hamcrest.Matchers.equalTo; import static org.hamcrest.Matchers.instanceOf; @@ -143,7 +142,7 @@ public void testWithMetaDataField() throws IOException { public void testIndexWildcard() throws IOException { QueryShardContext context = createShardContext(); - String index = context.getFullyQualifiedIndexName(); + String index = context.getFullyQualifiedIndex().getName(); Query query = new WildcardQueryBuilder("_index", index).doToQuery(context); assertThat(query instanceof MatchAllDocsQuery, equalTo(true));