Search in sources :

Example 1 with InvalidAliasNameException

use of org.opensearch.indices.InvalidAliasNameException in project OpenSearch by opensearch-project.

the class SimpleIndexTemplateIT method testAliasNameExistingIndex.

public void testAliasNameExistingIndex() throws Exception {
    createIndex("index");
    client().admin().indices().preparePutTemplate("template_1").setPatterns(Collections.singletonList("te*")).addAlias(new Alias("index")).get();
    InvalidAliasNameException e = expectThrows(InvalidAliasNameException.class, () -> createIndex("test"));
    assertThat(e.getMessage(), equalTo("Invalid alias name [index], an index exists with the same name as the alias"));
}
Also used : Alias(org.opensearch.action.admin.indices.alias.Alias) InvalidAliasNameException(org.opensearch.indices.InvalidAliasNameException)

Example 2 with InvalidAliasNameException

use of org.opensearch.indices.InvalidAliasNameException in project OpenSearch by opensearch-project.

the class ShardSearchRequest method parseAliasFilter.

/**
 * Returns the filter associated with listed filtering aliases.
 * <p>
 * The list of filtering aliases should be obtained by calling Metadata.filteringAliases.
 * Returns {@code null} if no filtering is required.</p>
 */
public static QueryBuilder parseAliasFilter(CheckedFunction<BytesReference, QueryBuilder, IOException> filterParser, IndexMetadata metadata, String... aliasNames) {
    if (aliasNames == null || aliasNames.length == 0) {
        return null;
    }
    Index index = metadata.getIndex();
    ImmutableOpenMap<String, AliasMetadata> aliases = metadata.getAliases();
    Function<AliasMetadata, QueryBuilder> parserFunction = (alias) -> {
        if (alias.filter() == null) {
            return null;
        }
        try {
            return filterParser.apply(alias.filter().uncompressed());
        } catch (IOException ex) {
            throw new AliasFilterParsingException(index, alias.getAlias(), "Invalid alias filter", ex);
        }
    };
    if (aliasNames.length == 1) {
        AliasMetadata alias = aliases.get(aliasNames[0]);
        if (alias == null) {
            // This shouldn't happen unless alias disappeared after filteringAliases was called.
            throw new InvalidAliasNameException(index, aliasNames[0], "Unknown alias name was passed to alias Filter");
        }
        return parserFunction.apply(alias);
    } else {
        // we need to bench here a bit, to see maybe it makes sense to use OrFilter
        BoolQueryBuilder combined = new BoolQueryBuilder();
        for (String aliasName : aliasNames) {
            AliasMetadata alias = aliases.get(aliasName);
            if (alias == null) {
                // This shouldn't happen unless alias disappeared after filteringAliases was called.
                throw new InvalidAliasNameException(index, aliasNames[0], "Unknown alias name was passed to alias Filter");
            }
            QueryBuilder parsedFilter = parserFunction.apply(alias);
            if (parsedFilter != null) {
                combined.should(parsedFilter);
            } else {
                // The filter might be null only if filter was removed after filteringAliases was called
                return null;
            }
        }
        return combined;
    }
}
Also used : ImmutableOpenMap(org.opensearch.common.collect.ImmutableOpenMap) Arrays(java.util.Arrays) BytesReference(org.opensearch.common.bytes.BytesReference) InvalidAliasNameException(org.opensearch.indices.InvalidAliasNameException) IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) CheckedFunction(org.opensearch.common.CheckedFunction) Version(org.opensearch.Version) StreamOutput(org.opensearch.common.io.stream.StreamOutput) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) IndicesOptions(org.opensearch.action.support.IndicesOptions) QuerySearchResult(org.opensearch.search.query.QuerySearchResult) Function(java.util.function.Function) Strings(org.opensearch.common.Strings) SearchShardTask(org.opensearch.action.search.SearchShardTask) IndicesRequest(org.opensearch.action.IndicesRequest) LegacyESVersion(org.opensearch.LegacyESVersion) FieldSortBuilder(org.opensearch.search.sort.FieldSortBuilder) Map(java.util.Map) SearchRequest(org.opensearch.action.search.SearchRequest) AliasFilterParsingException(org.opensearch.indices.AliasFilterParsingException) TRACK_TOTAL_HITS_DISABLED(org.opensearch.search.internal.SearchContext.TRACK_TOTAL_HITS_DISABLED) Scroll(org.opensearch.search.Scroll) StreamInput(org.opensearch.common.io.stream.StreamInput) TimeValue(org.opensearch.common.unit.TimeValue) TransportRequest(org.opensearch.transport.TransportRequest) Index(org.opensearch.index.Index) SearchType(org.opensearch.action.search.SearchType) Rewriteable(org.opensearch.index.query.Rewriteable) TaskId(org.opensearch.tasks.TaskId) IOException(java.io.IOException) Task(org.opensearch.tasks.Task) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) OriginalIndices(org.opensearch.action.OriginalIndices) Nullable(org.opensearch.common.Nullable) ShardId(org.opensearch.index.shard.ShardId) QueryBuilder(org.opensearch.index.query.QueryBuilder) QueryRewriteContext(org.opensearch.index.query.QueryRewriteContext) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) QueryShardContext(org.opensearch.index.query.QueryShardContext) SearchSortValuesAndFormats(org.opensearch.search.SearchSortValuesAndFormats) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) AliasFilterParsingException(org.opensearch.indices.AliasFilterParsingException) AliasMetadata(org.opensearch.cluster.metadata.AliasMetadata) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) Index(org.opensearch.index.Index) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) IOException(java.io.IOException) InvalidAliasNameException(org.opensearch.indices.InvalidAliasNameException)

Example 3 with InvalidAliasNameException

use of org.opensearch.indices.InvalidAliasNameException in project OpenSearch by opensearch-project.

the class ShardSearchRequestTests method testRemovedAliasFilter.

public void testRemovedAliasFilter() throws Exception {
    IndexMetadata indexMetadata = BASE_METADATA;
    indexMetadata = add(indexMetadata, "cats", filter(termQuery("animal", "cat")));
    indexMetadata = remove(indexMetadata, "cats");
    try {
        aliasFilter(indexMetadata, "cats");
        fail("Expected InvalidAliasNameException");
    } catch (InvalidAliasNameException e) {
        assertThat(e.getMessage(), containsString("Invalid alias name [cats]"));
    }
}
Also used : IndexMetadata(org.opensearch.cluster.metadata.IndexMetadata) InvalidAliasNameException(org.opensearch.indices.InvalidAliasNameException)

Aggregations

InvalidAliasNameException (org.opensearch.indices.InvalidAliasNameException)3 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)2 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Map (java.util.Map)1 Function (java.util.function.Function)1 LegacyESVersion (org.opensearch.LegacyESVersion)1 Version (org.opensearch.Version)1 IndicesRequest (org.opensearch.action.IndicesRequest)1 OriginalIndices (org.opensearch.action.OriginalIndices)1 Alias (org.opensearch.action.admin.indices.alias.Alias)1 SearchRequest (org.opensearch.action.search.SearchRequest)1 SearchShardTask (org.opensearch.action.search.SearchShardTask)1 SearchType (org.opensearch.action.search.SearchType)1 IndicesOptions (org.opensearch.action.support.IndicesOptions)1 AliasMetadata (org.opensearch.cluster.metadata.AliasMetadata)1 CheckedFunction (org.opensearch.common.CheckedFunction)1 Nullable (org.opensearch.common.Nullable)1 Strings (org.opensearch.common.Strings)1 BytesReference (org.opensearch.common.bytes.BytesReference)1