diff --git a/core/src/main/java/org/elasticsearch/action/search/SearchTransportService.java b/core/src/main/java/org/elasticsearch/action/search/SearchTransportService.java index 08d7d907661..e26f248d081 100644 --- a/core/src/main/java/org/elasticsearch/action/search/SearchTransportService.java +++ b/core/src/main/java/org/elasticsearch/action/search/SearchTransportService.java @@ -175,6 +175,14 @@ public class SearchTransportService extends AbstractComponent { remoteClustersSeeds = buildRemoteClustersSeeds(settings); } + boolean isCrossClusterSearchEnabled() { + return remoteClustersSeeds.isEmpty() == false; + } + + boolean isRemoteClusterRegistered(String clusterName) { + return remoteClustersSeeds.containsKey(clusterName); + } + private DiscoveryNode connectToRemoteCluster(String clusterName) { List nodes = remoteClustersSeeds.get(clusterName); if (nodes == null) { diff --git a/core/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java b/core/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java index fb767dc4dbf..912c0d30ead 100644 --- a/core/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java +++ b/core/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java @@ -109,29 +109,34 @@ public class TransportSearchAction extends HandledTransportAction localIndicesList = new ArrayList<>(); + final String[] localIndices; final Map> remoteIndicesByCluster = new HashMap<>(); - for (String index : searchRequest.indices()) { - int i = index.indexOf(REMOTE_CLUSTER_INDEX_SEPARATOR); - if (i >= 0) { - String remoteCluster = index.substring(0, i); - String remoteIndex = index.substring(i + 1); - List indices = remoteIndicesByCluster.get(remoteCluster); - if (indices == null) { - indices = new ArrayList<>(); - remoteIndicesByCluster.put(remoteCluster, indices); + if (searchTransportService.isCrossClusterSearchEnabled()) { + List localIndicesList = new ArrayList<>(); + for (String index : searchRequest.indices()) { + int i = index.indexOf(REMOTE_CLUSTER_INDEX_SEPARATOR); + if (i >= 0) { + String remoteCluster = index.substring(0, i); + if (searchTransportService.isRemoteClusterRegistered(remoteCluster)) { + String remoteIndex = index.substring(i + 1); + List indices = remoteIndicesByCluster.get(remoteCluster); + if (indices == null) { + indices = new ArrayList<>(); + remoteIndicesByCluster.put(remoteCluster, indices); + } + indices.add(remoteIndex); + } else { + localIndicesList.add(index); + } + } else { + localIndicesList.add(index); } - indices.add(remoteIndex); - } else { - localIndicesList.add(index); } + localIndices = localIndicesList.toArray(new String[localIndicesList.size()]); + } else { + localIndices = searchRequest.indices(); } - String[] localIndices = localIndicesList.toArray(new String[localIndicesList.size()]); - if (remoteIndicesByCluster.isEmpty()) { executeSearch((SearchTask)task, startTimeInMillis, searchRequest, localIndices, Strings.EMPTY_ARRAY, Collections.emptyList(), Collections.emptySet(), listener);