Search in sources :

Example 1 with ClusterSearchShardsResponse

use of org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse in project elasticsearch by elastic.

the class GetTermVectorsIT method testArtificialDocWithPreference.

public void testArtificialDocWithPreference() throws ExecutionException, InterruptedException, IOException {
    // setup indices
    Settings.Builder settings = Settings.builder().put(indexSettings()).put("index.analysis.analyzer", "standard");
    assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", "field1", "type=text,term_vector=with_positions_offsets"));
    ensureGreen();
    // index document
    indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "random permutation"));
    // Get search shards
    ClusterSearchShardsResponse searchShardsResponse = client().admin().cluster().prepareSearchShards("test").get();
    List<Integer> shardIds = Arrays.stream(searchShardsResponse.getGroups()).map(s -> s.getShardId().id()).collect(Collectors.toList());
    // request termvectors of artificial document from each shard
    int sumTotalTermFreq = 0;
    int sumDocFreq = 0;
    for (Integer shardId : shardIds) {
        TermVectorsResponse tvResponse = client().prepareTermVectors().setIndex("test").setType("type1").setPreference("_shards:" + shardId).setDoc(jsonBuilder().startObject().field("field1", "random permutation").endObject()).setFieldStatistics(true).setTermStatistics(true).get();
        Fields fields = tvResponse.getFields();
        Terms terms = fields.terms("field1");
        assertNotNull(terms);
        TermsEnum termsEnum = terms.iterator();
        while (termsEnum.next() != null) {
            sumTotalTermFreq += termsEnum.totalTermFreq();
            sumDocFreq += termsEnum.docFreq();
        }
    }
    assertEquals("expected to find term statistics in exactly one shard!", 2, sumTotalTermFreq);
    assertEquals("expected to find term statistics in exactly one shard!", 2, sumDocFreq);
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) ElasticsearchException(org.elasticsearch.ElasticsearchException) Versions(org.elasticsearch.common.lucene.uid.Versions) Arrays(java.util.Arrays) FieldType(org.apache.lucene.document.FieldType) Alias(org.elasticsearch.action.admin.indices.alias.Alias) Fields(org.apache.lucene.index.Fields) HashMap(java.util.HashMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) PayloadHelper(org.apache.lucene.analysis.payloads.PayloadHelper) ActionFuture(org.elasticsearch.action.ActionFuture) Strings(org.elasticsearch.common.Strings) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Settings(org.elasticsearch.common.settings.Settings) ObjectIntHashMap(com.carrotsearch.hppc.ObjectIntHashMap) TermsEnum(org.apache.lucene.index.TermsEnum) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) XContentFactory.jsonBuilder(org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder) VersionConflictEngineException(org.elasticsearch.index.engine.VersionConflictEngineException) ElasticsearchAssertions.assertThrows(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertThrows) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) PostingsEnum(org.apache.lucene.index.PostingsEnum) Terms(org.apache.lucene.index.Terms) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) BytesRef(org.apache.lucene.util.BytesRef) DirectoryReader(org.apache.lucene.index.DirectoryReader) ToXContent(org.elasticsearch.common.xcontent.ToXContent) Set(java.util.Set) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Collections(java.util.Collections) ElasticsearchAssertions.assertAcked(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked) Fields(org.apache.lucene.index.Fields) Terms(org.apache.lucene.index.Terms) Settings(org.elasticsearch.common.settings.Settings) TermsEnum(org.apache.lucene.index.TermsEnum)

Example 2 with ClusterSearchShardsResponse

use of org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse in project elasticsearch by elastic.

the class RemoteClusterConnection method fetchShardsInternal.

private void fetchShardsInternal(SearchRequest searchRequest, List<String> indices, final ActionListener<ClusterSearchShardsResponse> listener) {
    final DiscoveryNode node = nodeSupplier.get();
    ClusterSearchShardsRequest searchShardsRequest = new ClusterSearchShardsRequest(indices.toArray(new String[indices.size()])).indicesOptions(searchRequest.indicesOptions()).local(true).preference(searchRequest.preference()).routing(searchRequest.routing());
    transportService.sendRequest(node, ClusterSearchShardsAction.NAME, searchShardsRequest, new TransportResponseHandler<ClusterSearchShardsResponse>() {

        @Override
        public ClusterSearchShardsResponse newInstance() {
            return new ClusterSearchShardsResponse();
        }

        @Override
        public void handleResponse(ClusterSearchShardsResponse clusterSearchShardsResponse) {
            listener.onResponse(clusterSearchShardsResponse);
        }

        @Override
        public void handleException(TransportException e) {
            listener.onFailure(e);
        }

        @Override
        public String executor() {
            return ThreadPool.Names.SEARCH;
        }
    });
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) ClusterSearchShardsRequest(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsRequest) ConnectTransportException(org.elasticsearch.transport.ConnectTransportException) TransportException(org.elasticsearch.transport.TransportException)

Example 3 with ClusterSearchShardsResponse

use of org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse in project elasticsearch by elastic.

the class ClusterSearchShardsIT method testSingleShardAllocation.

public void testSingleShardAllocation() throws Exception {
    client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", "1").put("index.number_of_replicas", 0).put("index.routing.allocation.include.tag", "A")).execute().actionGet();
    ensureGreen();
    ClusterSearchShardsResponse response = client().admin().cluster().prepareSearchShards("test").execute().actionGet();
    assertThat(response.getGroups().length, equalTo(1));
    assertThat(response.getGroups()[0].getShardId().getIndexName(), equalTo("test"));
    assertThat(response.getGroups()[0].getShardId().getId(), equalTo(0));
    assertThat(response.getGroups()[0].getShards().length, equalTo(1));
    assertThat(response.getNodes().length, equalTo(1));
    assertThat(response.getGroups()[0].getShards()[0].currentNodeId(), equalTo(response.getNodes()[0].getId()));
    response = client().admin().cluster().prepareSearchShards("test").setRouting("A").execute().actionGet();
    assertThat(response.getGroups().length, equalTo(1));
    assertThat(response.getGroups()[0].getShardId().getIndexName(), equalTo("test"));
    assertThat(response.getGroups()[0].getShardId().getId(), equalTo(0));
    assertThat(response.getGroups()[0].getShards().length, equalTo(1));
    assertThat(response.getNodes().length, equalTo(1));
    assertThat(response.getGroups()[0].getShards()[0].currentNodeId(), equalTo(response.getNodes()[0].getId()));
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse)

Example 4 with ClusterSearchShardsResponse

use of org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse in project elasticsearch by elastic.

the class RemoteClusterService method collectSearchShards.

void collectSearchShards(SearchRequest searchRequest, Map<String, List<String>> remoteIndicesByCluster, ActionListener<Map<String, ClusterSearchShardsResponse>> listener) {
    final CountDown responsesCountDown = new CountDown(remoteIndicesByCluster.size());
    final Map<String, ClusterSearchShardsResponse> searchShardsResponses = new ConcurrentHashMap<>();
    final AtomicReference<TransportException> transportException = new AtomicReference<>();
    for (Map.Entry<String, List<String>> entry : remoteIndicesByCluster.entrySet()) {
        final String clusterName = entry.getKey();
        RemoteClusterConnection remoteClusterConnection = remoteClusters.get(clusterName);
        if (remoteClusterConnection == null) {
            throw new IllegalArgumentException("no such remote cluster: " + clusterName);
        }
        final List<String> indices = entry.getValue();
        remoteClusterConnection.fetchSearchShards(searchRequest, indices, new ActionListener<ClusterSearchShardsResponse>() {

            @Override
            public void onResponse(ClusterSearchShardsResponse clusterSearchShardsResponse) {
                searchShardsResponses.put(clusterName, clusterSearchShardsResponse);
                if (responsesCountDown.countDown()) {
                    TransportException exception = transportException.get();
                    if (exception == null) {
                        listener.onResponse(searchShardsResponses);
                    } else {
                        listener.onFailure(transportException.get());
                    }
                }
            }

            @Override
            public void onFailure(Exception e) {
                TransportException exception = new TransportException("unable to communicate with remote cluster [" + clusterName + "]", e);
                if (transportException.compareAndSet(null, exception) == false) {
                    exception = transportException.accumulateAndGet(exception, (previous, current) -> {
                        current.addSuppressed(previous);
                        return current;
                    });
                }
                if (responsesCountDown.countDown()) {
                    listener.onFailure(exception);
                }
            }
        });
    }
}
Also used : ClusterSearchShardsResponse(org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDown(org.elasticsearch.common.util.concurrent.CountDown) TransportException(org.elasticsearch.transport.TransportException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) TransportException(org.elasticsearch.transport.TransportException) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 5 with ClusterSearchShardsResponse

use of org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse 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

ClusterSearchShardsResponse (org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsResponse)11 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 ClusterSearchShardsGroup (org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsGroup)4 UnknownHostException (java.net.UnknownHostException)3 List (java.util.List)3 Map (java.util.Map)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 TransportException (org.elasticsearch.transport.TransportException)3 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)2 TimeoutException (java.util.concurrent.TimeoutException)2 Collectors (java.util.stream.Collectors)2 ClusterSearchShardsRequest (org.elasticsearch.action.admin.cluster.shards.ClusterSearchShardsRequest)2 ShardIterator (org.elasticsearch.cluster.routing.ShardIterator)2 CountDown (org.elasticsearch.common.util.concurrent.CountDown)2