Search in sources :

Example 6 with AliasFilter

use of org.elasticsearch.search.internal.AliasFilter in project elasticsearch by elastic.

the class ClusterSearchShardsResponse method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    builder.startObject("nodes");
    for (DiscoveryNode node : nodes) {
        node.toXContent(builder, params);
    }
    builder.endObject();
    if (indicesAndFilters != null) {
        builder.startObject("indices");
        for (Map.Entry<String, AliasFilter> entry : indicesAndFilters.entrySet()) {
            String index = entry.getKey();
            builder.startObject(index);
            AliasFilter aliasFilter = entry.getValue();
            if (aliasFilter.getAliases().length > 0) {
                builder.array("aliases", aliasFilter.getAliases());
                builder.field("filter");
                aliasFilter.getQueryBuilder().toXContent(builder, params);
            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.startArray("shards");
    for (ClusterSearchShardsGroup group : groups) {
        group.toXContent(builder, params);
    }
    builder.endArray();
    builder.endObject();
    return builder;
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) AliasFilter(org.elasticsearch.search.internal.AliasFilter) Map(java.util.Map) HashMap(java.util.HashMap)

Example 7 with AliasFilter

use of org.elasticsearch.search.internal.AliasFilter in project elasticsearch by elastic.

the class TransportExplainAction method resolveRequest.

@Override
protected void resolveRequest(ClusterState state, InternalRequest request) {
    final AliasFilter aliasFilter = searchService.buildAliasFilter(state, request.concreteIndex(), request.request().index());
    request.request().filteringAlias(aliasFilter);
    // Fail fast on the node that received the request.
    if (request.request().routing() == null && state.getMetaData().routingRequired(request.concreteIndex(), request.request().type())) {
        throw new RoutingMissingException(request.concreteIndex(), request.request().type(), request.request().id());
    }
}
Also used : AliasFilter(org.elasticsearch.search.internal.AliasFilter) RoutingMissingException(org.elasticsearch.action.RoutingMissingException)

Example 8 with AliasFilter

use of org.elasticsearch.search.internal.AliasFilter in project elasticsearch by elastic.

the class IndicesService method buildAliasFilter.

public AliasFilter buildAliasFilter(ClusterState state, String index, String... expressions) {
    /* Being static, parseAliasFilter doesn't have access to whatever guts it needs to parse a query. Instead of passing in a bunch
         * of dependencies we pass in a function that can perform the parsing. */
    CheckedFunction<byte[], QueryBuilder, IOException> filterParser = bytes -> {
        try (XContentParser parser = XContentFactory.xContent(bytes).createParser(xContentRegistry, bytes)) {
            return new QueryParseContext(parser).parseInnerQueryBuilder();
        }
    };
    String[] aliases = indexNameExpressionResolver.filteringAliases(state, index, expressions);
    IndexMetaData indexMetaData = state.metaData().index(index);
    return new AliasFilter(ShardSearchRequest.parseAliasFilter(filterParser, indexMetaData, aliases), aliases);
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) StreamOutput(org.elasticsearch.common.io.stream.StreamOutput) Nullable(org.elasticsearch.common.Nullable) BigArrays(org.elasticsearch.common.util.BigArrays) FlushStats(org.elasticsearch.index.flush.FlushStats) RecoveryStats(org.elasticsearch.index.recovery.RecoveryStats) ClusterState(org.elasticsearch.cluster.ClusterState) QueryPhase(org.elasticsearch.search.query.QueryPhase) IndexingStats(org.elasticsearch.index.shard.IndexingStats) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) Map(java.util.Map) EnumSet(java.util.EnumSet) MergeStats(org.elasticsearch.index.merge.MergeStats) SearchType(org.elasticsearch.action.search.SearchType) PeerRecoveryTargetService(org.elasticsearch.indices.recovery.PeerRecoveryTargetService) Set(java.util.Set) CollectionUtils.arrayAsArrayList(org.elasticsearch.common.util.CollectionUtils.arrayAsArrayList) Executors(java.util.concurrent.Executors) RecoverySource(org.elasticsearch.cluster.routing.RecoverySource) CheckedFunction(org.elasticsearch.common.CheckedFunction) AliasFilter(org.elasticsearch.search.internal.AliasFilter) CountDownLatch(java.util.concurrent.CountDownLatch) Logger(org.apache.logging.log4j.Logger) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) Callback(org.elasticsearch.common.util.Callback) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) IndexShardStats(org.elasticsearch.action.admin.indices.stats.IndexShardStats) ClusterService(org.elasticsearch.cluster.service.ClusterService) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) IndexModule(org.elasticsearch.index.IndexModule) Supplier(java.util.function.Supplier) CircuitBreakerService(org.elasticsearch.indices.breaker.CircuitBreakerService) ArrayList(java.util.ArrayList) BytesArray(org.elasticsearch.common.bytes.BytesArray) ShardRequestCache(org.elasticsearch.index.cache.request.ShardRequestCache) NamedWriteableRegistry(org.elasticsearch.common.io.stream.NamedWriteableRegistry) CircuitBreaker(org.elasticsearch.common.breaker.CircuitBreaker) MetaDataStateFormat(org.elasticsearch.gateway.MetaDataStateFormat) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) FileSystemUtils(org.elasticsearch.common.io.FileSystemUtils) IndexEventListener(org.elasticsearch.index.shard.IndexEventListener) Files(java.nio.file.Files) Client(org.elasticsearch.client.Client) IndexService(org.elasticsearch.index.IndexService) IndexShard(org.elasticsearch.index.shard.IndexShard) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) ShardLock(org.elasticsearch.env.ShardLock) RepositoriesService(org.elasticsearch.repositories.RepositoriesService) CollectionUtil(org.apache.lucene.util.CollectionUtil) XContentParser(org.elasticsearch.common.xcontent.XContentParser) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) IndicesFieldDataCache(org.elasticsearch.indices.fielddata.cache.IndicesFieldDataCache) CommonStatsFlags(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags) SearchStats(org.elasticsearch.index.search.stats.SearchStats) ScriptService(org.elasticsearch.script.ScriptService) ElasticsearchException(org.elasticsearch.ElasticsearchException) Property(org.elasticsearch.common.settings.Setting.Property) MapperRegistry(org.elasticsearch.indices.mapper.MapperRegistry) Settings(org.elasticsearch.common.settings.Settings) ShardSearchRequest(org.elasticsearch.search.internal.ShardSearchRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) ThreadPool(org.elasticsearch.threadpool.ThreadPool) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) Releasable(org.elasticsearch.common.lease.Releasable) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) PluginsService(org.elasticsearch.plugins.PluginsService) Setting(org.elasticsearch.common.settings.Setting) Collections.emptyList(java.util.Collections.emptyList) DirectoryReader(org.apache.lucene.index.DirectoryReader) ShardLockObtainFailedException(org.elasticsearch.env.ShardLockObtainFailedException) AnalysisRegistry(org.elasticsearch.index.analysis.AnalysisRegistry) Flag(org.elasticsearch.action.admin.indices.stats.CommonStatsFlags.Flag) BytesReference(org.elasticsearch.common.bytes.BytesReference) IllegalIndexShardStateException(org.elasticsearch.index.shard.IllegalIndexShardStateException) Collectors(java.util.stream.Collectors) MapBuilder.newMapBuilder(org.elasticsearch.common.collect.MapBuilder.newMapBuilder) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) Engine(org.elasticsearch.index.engine.Engine) RamUsageEstimator(org.apache.lucene.util.RamUsageEstimator) MapperService(org.elasticsearch.index.mapper.MapperService) RefreshStats(org.elasticsearch.index.refresh.RefreshStats) List(java.util.List) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) IndicesClusterStateService(org.elasticsearch.indices.cluster.IndicesClusterStateService) RecoveryState(org.elasticsearch.indices.recovery.RecoveryState) GetStats(org.elasticsearch.index.get.GetStats) CommonStats(org.elasticsearch.action.admin.indices.stats.CommonStats) FieldStats(org.elasticsearch.action.fieldstats.FieldStats) IndexingOperationListener(org.elasticsearch.index.shard.IndexingOperationListener) ShardRouting(org.elasticsearch.cluster.routing.ShardRouting) XContentType(org.elasticsearch.common.xcontent.XContentType) SearchContext(org.elasticsearch.search.internal.SearchContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) MetaStateService(org.elasticsearch.gateway.MetaStateService) Index(org.elasticsearch.index.Index) ParameterizedMessage(org.apache.logging.log4j.message.ParameterizedMessage) ResourceAlreadyExistsException(org.elasticsearch.ResourceAlreadyExistsException) ShardStats(org.elasticsearch.action.admin.indices.stats.ShardStats) TimeValue(org.elasticsearch.common.unit.TimeValue) Iterables(org.elasticsearch.common.util.iterable.Iterables) IndexSettings(org.elasticsearch.index.IndexSettings) ExecutorService(java.util.concurrent.ExecutorService) Collections.emptyMap(java.util.Collections.emptyMap) EsExecutors(org.elasticsearch.common.util.concurrent.EsExecutors) IndexShardState(org.elasticsearch.index.shard.IndexShardState) IndexFieldDataCache(org.elasticsearch.index.fielddata.IndexFieldDataCache) Iterator(java.util.Iterator) AbstractLifecycleComponent(org.elasticsearch.common.component.AbstractLifecycleComponent) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) ClusterSettings(org.elasticsearch.common.settings.ClusterSettings) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) StreamInput(org.elasticsearch.common.io.stream.StreamInput) Closeable(java.io.Closeable) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) IndexNameExpressionResolver(org.elasticsearch.cluster.metadata.IndexNameExpressionResolver) AliasFilter(org.elasticsearch.search.internal.AliasFilter) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) XContentParser(org.elasticsearch.common.xcontent.XContentParser) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 9 with AliasFilter

use of org.elasticsearch.search.internal.AliasFilter in project elasticsearch by elastic.

the class ExplainRequestTests method testSerialize50Request.

// BWC test for changes from #20916
public void testSerialize50Request() throws IOException {
    ExplainRequest request = new ExplainRequest("index", "type", "id");
    request.fetchSourceContext(new FetchSourceContext(true, new String[] { "field1.*" }, new String[] { "field2.*" }));
    request.filteringAlias(new AliasFilter(QueryBuilders.termQuery("filter_field", "value"), new String[] { "alias0", "alias1" }));
    request.preference("the_preference");
    request.query(QueryBuilders.termQuery("field", "value"));
    request.storedFields(new String[] { "field1", "field2" });
    request.routing("some_routing");
    BytesArray requestBytes = new BytesArray(Base64.getDecoder().decode("AAABBWluZGV4BHR5cGUCaWQBDHNvbWVfcm91dGluZwEOdGhlX3ByZWZlcmVuY2UEdGVybT" + "+AAAAABWZpZWxkFQV2YWx1ZQIGYWxpYXMwBmFsaWFzMQECBmZpZWxkMQZmaWVsZDIBAQEIZmllbGQxLioBCGZpZWxkMi4qAA"));
    try (StreamInput in = new NamedWriteableAwareStreamInput(requestBytes.streamInput(), namedWriteableRegistry)) {
        in.setVersion(Version.V_5_0_0);
        ExplainRequest readRequest = new ExplainRequest();
        readRequest.readFrom(in);
        assertEquals(0, in.available());
        assertArrayEquals(request.filteringAlias().getAliases(), readRequest.filteringAlias().getAliases());
        expectThrows(IllegalStateException.class, () -> readRequest.filteringAlias().getQueryBuilder());
        assertArrayEquals(request.storedFields(), readRequest.storedFields());
        assertEquals(request.preference(), readRequest.preference());
        assertEquals(request.query(), readRequest.query());
        assertEquals(request.routing(), readRequest.routing());
        assertEquals(request.fetchSourceContext(), readRequest.fetchSourceContext());
        BytesStreamOutput output = new BytesStreamOutput();
        output.setVersion(Version.V_5_0_0);
        readRequest.writeTo(output);
        assertEquals(output.bytes().toBytesRef(), requestBytes.toBytesRef());
    }
}
Also used : AliasFilter(org.elasticsearch.search.internal.AliasFilter) BytesArray(org.elasticsearch.common.bytes.BytesArray) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) StreamInput(org.elasticsearch.common.io.stream.StreamInput) NamedWriteableAwareStreamInput(org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput) ExplainRequest(org.elasticsearch.action.explain.ExplainRequest) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput)

Example 10 with AliasFilter

use of org.elasticsearch.search.internal.AliasFilter in project elasticsearch by elastic.

the class RemoteClusterService method processRemoteShards.

Function<String, Transport.Connection> processRemoteShards(Map<String, ClusterSearchShardsResponse> searchShardsResponses, List<ShardIterator> remoteShardIterators, Map<String, AliasFilter> aliasFilterMap) {
    Map<String, Supplier<Transport.Connection>> nodeToCluster = new HashMap<>();
    for (Map.Entry<String, ClusterSearchShardsResponse> entry : searchShardsResponses.entrySet()) {
        String clusterName = entry.getKey();
        ClusterSearchShardsResponse searchShardsResponse = entry.getValue();
        for (DiscoveryNode remoteNode : searchShardsResponse.getNodes()) {
            nodeToCluster.put(remoteNode.getId(), () -> getConnection(remoteNode, clusterName));
        }
        Map<String, AliasFilter> indicesAndFilters = searchShardsResponse.getIndicesAndFilters();
        for (ClusterSearchShardsGroup clusterSearchShardsGroup : searchShardsResponse.getGroups()) {
            //add the cluster name to the remote index names for indices disambiguation
            //this ends up in the hits returned with the search response
            ShardId shardId = clusterSearchShardsGroup.getShardId();
            Index remoteIndex = shardId.getIndex();
            Index index = new Index(clusterName + REMOTE_CLUSTER_INDEX_SEPARATOR + remoteIndex.getName(), remoteIndex.getUUID());
            ShardIterator shardIterator = new PlainShardIterator(new ShardId(index, shardId.getId()), Arrays.asList(clusterSearchShardsGroup.getShards()));
            remoteShardIterators.add(shardIterator);
            AliasFilter aliasFilter;
            if (indicesAndFilters == null) {
                aliasFilter = new AliasFilter(null, Strings.EMPTY_ARRAY);
            } else {
                aliasFilter = indicesAndFilters.get(shardId.getIndexName());
                assert aliasFilter != null;
            }
            // here we have to map the filters to the UUID since from now on we use the uuid for the lookup
            aliasFilterMap.put(remoteIndex.getUUID(), aliasFilter);
        }
    }
    return (nodeId) -> {
        Supplier<Transport.Connection> supplier = nodeToCluster.get(nodeId);
        if (supplier == null) {
            throw new IllegalArgumentException("unknown remote node: " + nodeId);
        }
        return supplier.get();
    };
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) ShardId(org.elasticsearch.index.shard.ShardId) Arrays(java.util.Arrays) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) TimeoutException(java.util.concurrent.TimeoutException) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Strings(org.elasticsearch.common.Strings) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) Settings(org.elasticsearch.common.settings.Settings) TimeValue(org.elasticsearch.common.unit.TimeValue) Map(java.util.Map) CountDown(org.elasticsearch.common.util.concurrent.CountDown) TransportService(org.elasticsearch.transport.TransportService) ClusterSearchShardsGroup(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup) Transport(org.elasticsearch.transport.Transport) AbstractComponent(org.elasticsearch.common.component.AbstractComponent) Setting(org.elasticsearch.common.settings.Setting) Predicate(java.util.function.Predicate) PlainActionFuture(org.elasticsearch.action.support.PlainActionFuture) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOUtils(org.apache.lucene.util.IOUtils) IOException(java.io.IOException) InetSocketAddress(java.net.InetSocketAddress) UnknownHostException(java.net.UnknownHostException) Collectors(java.util.stream.Collectors) ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) TimeUnit(java.util.concurrent.TimeUnit) AliasFilter(org.elasticsearch.search.internal.AliasFilter) List(java.util.List) Version(org.elasticsearch.Version) Stream(java.util.stream.Stream) TransportAddress(org.elasticsearch.common.transport.TransportAddress) Supplier(org.apache.logging.log4j.util.Supplier) Closeable(java.io.Closeable) TransportException(org.elasticsearch.transport.TransportException) Collections(java.util.Collections) ActionListener(org.elasticsearch.action.ActionListener) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) AliasFilter(org.elasticsearch.search.internal.AliasFilter) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) Index(org.elasticsearch.index.Index) ClusterSearchShardsGroup(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup) ShardId(org.elasticsearch.index.shard.ShardId) ShardIterator(org.elasticsearch.cluster.routing.ShardIterator) PlainShardIterator(org.elasticsearch.cluster.routing.PlainShardIterator) Supplier(org.apache.logging.log4j.util.Supplier) Transport(org.elasticsearch.transport.Transport) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

AliasFilter (org.elasticsearch.search.internal.AliasFilter)22 HashMap (java.util.HashMap)11 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)10 ShardId (org.elasticsearch.index.shard.ShardId)7 ArrayList (java.util.ArrayList)6 Map (java.util.Map)6 ShardIterator (org.elasticsearch.cluster.routing.ShardIterator)6 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)6 NamedWriteableAwareStreamInput (org.elasticsearch.common.io.stream.NamedWriteableAwareStreamInput)6 StreamInput (org.elasticsearch.common.io.stream.StreamInput)6 Set (java.util.Set)5 IOException (java.io.IOException)4 List (java.util.List)4 ActionListener (org.elasticsearch.action.ActionListener)4 GroupShardsIterator (org.elasticsearch.cluster.routing.GroupShardsIterator)4 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)4 Index (org.elasticsearch.index.Index)4 Collections (java.util.Collections)3 HashSet (java.util.HashSet)3 CountDownLatch (java.util.concurrent.CountDownLatch)3