Search in sources :

Example 1 with AliasFilterParsingException

use of org.opensearch.indices.AliasFilterParsingException 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)

Aggregations

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 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 IndexMetadata (org.opensearch.cluster.metadata.IndexMetadata)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 ImmutableOpenMap (org.opensearch.common.collect.ImmutableOpenMap)1 BytesStreamOutput (org.opensearch.common.io.stream.BytesStreamOutput)1