Search in sources :

Example 1 with InvalidAliasNameException

use of org.elasticsearch.indices.InvalidAliasNameException in project elasticsearch by elastic.

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 <tt>null</tt> if no filtering is required.</p>
     */
static QueryBuilder parseAliasFilter(CheckedFunction<byte[], 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 : ShardId(org.elasticsearch.index.shard.ShardId) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchType(org.elasticsearch.action.search.SearchType) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) AliasMetaData(org.elasticsearch.cluster.metadata.AliasMetaData) Scroll(org.elasticsearch.search.Scroll) IOException(java.io.IOException) Index(org.elasticsearch.index.Index) BytesReference(org.elasticsearch.common.bytes.BytesReference) Function(java.util.function.Function) CheckedFunction(org.elasticsearch.common.CheckedFunction) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) AliasFilterParsingException(org.elasticsearch.indices.AliasFilterParsingException) InvalidAliasNameException(org.elasticsearch.indices.InvalidAliasNameException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) AliasMetaData(org.elasticsearch.cluster.metadata.AliasMetaData) AliasFilterParsingException(org.elasticsearch.indices.AliasFilterParsingException) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Index(org.elasticsearch.index.Index) IOException(java.io.IOException) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) InvalidAliasNameException(org.elasticsearch.indices.InvalidAliasNameException)

Example 2 with InvalidAliasNameException

use of org.elasticsearch.indices.InvalidAliasNameException in project elasticsearch by elastic.

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.elasticsearch.action.admin.indices.alias.Alias) InvalidAliasNameException(org.elasticsearch.indices.InvalidAliasNameException)

Example 3 with InvalidAliasNameException

use of org.elasticsearch.indices.InvalidAliasNameException in project elasticsearch by elastic.

the class ShardSearchTransportRequestTests method testRemovedAliasFilter.

public void testRemovedAliasFilter() throws Exception {
    IndexMetaData indexMetaData = baseMetaData;
    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.elasticsearch.cluster.metadata.IndexMetaData) InvalidAliasNameException(org.elasticsearch.indices.InvalidAliasNameException)

Aggregations

InvalidAliasNameException (org.elasticsearch.indices.InvalidAliasNameException)3 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)2 IOException (java.io.IOException)1 Function (java.util.function.Function)1 Alias (org.elasticsearch.action.admin.indices.alias.Alias)1 SearchType (org.elasticsearch.action.search.SearchType)1 AliasMetaData (org.elasticsearch.cluster.metadata.AliasMetaData)1 CheckedFunction (org.elasticsearch.common.CheckedFunction)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 ImmutableOpenMap (org.elasticsearch.common.collect.ImmutableOpenMap)1 Index (org.elasticsearch.index.Index)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)1 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)1 ShardId (org.elasticsearch.index.shard.ShardId)1 AliasFilterParsingException (org.elasticsearch.indices.AliasFilterParsingException)1 Scroll (org.elasticsearch.search.Scroll)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1