|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0 and the Server Side Public License, v 1; you may not use this file except |
| 5 | + * in compliance with, at your election, the Elastic License 2.0 or the Server |
| 6 | + * Side Public License, v 1. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.elasticsearch.search.retriever; |
| 10 | + |
| 11 | +import org.elasticsearch.action.search.SearchRequest; |
| 12 | +import org.elasticsearch.action.search.SearchResponse; |
| 13 | +import org.elasticsearch.client.internal.Client; |
| 14 | +import org.elasticsearch.common.Strings; |
| 15 | +import org.elasticsearch.common.settings.Setting; |
| 16 | +import org.elasticsearch.common.settings.Settings; |
| 17 | +import org.elasticsearch.core.TimeValue; |
| 18 | +import org.elasticsearch.index.query.MatchAllQueryBuilder; |
| 19 | +import org.elasticsearch.index.query.QueryBuilder; |
| 20 | +import org.elasticsearch.index.query.QueryRewriteContext; |
| 21 | +import org.elasticsearch.search.builder.SearchSourceBuilder; |
| 22 | +import org.elasticsearch.test.AbstractMultiClustersTestCase; |
| 23 | +import org.elasticsearch.test.InternalTestCluster; |
| 24 | +import org.elasticsearch.transport.RemoteClusterAware; |
| 25 | +import org.elasticsearch.xcontent.XContentBuilder; |
| 26 | + |
| 27 | +import java.io.IOException; |
| 28 | +import java.util.Collection; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.HashMap; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.concurrent.ExecutionException; |
| 34 | + |
| 35 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; |
| 36 | +import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertResponse; |
| 37 | +import static org.hamcrest.Matchers.equalTo; |
| 38 | + |
| 39 | +public class MinimalCompoundRetrieverIT extends AbstractMultiClustersTestCase { |
| 40 | + |
| 41 | + // CrossClusterSearchIT |
| 42 | + private static final String REMOTE_CLUSTER = "cluster_a"; |
| 43 | + |
| 44 | + @Override |
| 45 | + protected Collection<String> remoteClusterAlias() { |
| 46 | + return List.of(REMOTE_CLUSTER); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected Map<String, Boolean> skipUnavailableForRemoteClusters() { |
| 51 | + return Map.of(REMOTE_CLUSTER, randomBoolean()); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected boolean reuseClusters() { |
| 56 | + return false; |
| 57 | + } |
| 58 | + |
| 59 | + public void testSimpleSearch() throws ExecutionException, InterruptedException { |
| 60 | + RetrieverBuilder compoundRetriever = new CompoundRetriever(Collections.emptyList()); |
| 61 | + Map<String, Object> testClusterInfo = setupTwoClusters(); |
| 62 | + String localIndex = (String) testClusterInfo.get("local.index"); |
| 63 | + String remoteIndex = (String) testClusterInfo.get("remote.index"); |
| 64 | + SearchRequest searchRequest = new SearchRequest(localIndex, REMOTE_CLUSTER + ":" + remoteIndex); |
| 65 | + searchRequest.source(new SearchSourceBuilder().retriever(compoundRetriever)); |
| 66 | + assertResponse(client(LOCAL_CLUSTER).search(searchRequest), response -> { |
| 67 | + assertNotNull(response); |
| 68 | + |
| 69 | + SearchResponse.Clusters clusters = response.getClusters(); |
| 70 | + assertFalse("search cluster results should NOT be marked as partial", clusters.hasPartialResults()); |
| 71 | + assertThat(clusters.getTotal(), equalTo(2)); |
| 72 | + assertThat(clusters.getClusterStateCount(SearchResponse.Cluster.Status.SUCCESSFUL), equalTo(2)); |
| 73 | + assertThat(clusters.getClusterStateCount(SearchResponse.Cluster.Status.SKIPPED), equalTo(0)); |
| 74 | + assertThat(clusters.getClusterStateCount(SearchResponse.Cluster.Status.RUNNING), equalTo(0)); |
| 75 | + assertThat(clusters.getClusterStateCount(SearchResponse.Cluster.Status.PARTIAL), equalTo(0)); |
| 76 | + assertThat(clusters.getClusterStateCount(SearchResponse.Cluster.Status.FAILED), equalTo(0)); |
| 77 | + assertThat(response.getHits().getTotalHits().value, equalTo(testClusterInfo.get("total_docs"))); |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + private Map<String, Object> setupTwoClusters() { |
| 82 | + int totalDocs = 0; |
| 83 | + String localIndex = "demo"; |
| 84 | + int numShardsLocal = randomIntBetween(2, 10); |
| 85 | + Settings localSettings = indexSettings(numShardsLocal, randomIntBetween(0, 1)).build(); |
| 86 | + assertAcked( |
| 87 | + client(LOCAL_CLUSTER).admin() |
| 88 | + .indices() |
| 89 | + .prepareCreate(localIndex) |
| 90 | + .setSettings(localSettings) |
| 91 | + .setMapping("some_field", "type=keyword") |
| 92 | + ); |
| 93 | + totalDocs += indexDocs(client(LOCAL_CLUSTER), localIndex); |
| 94 | + |
| 95 | + String remoteIndex = "prod"; |
| 96 | + int numShardsRemote = randomIntBetween(2, 10); |
| 97 | + final InternalTestCluster remoteCluster = cluster(REMOTE_CLUSTER); |
| 98 | + remoteCluster.ensureAtLeastNumDataNodes(randomIntBetween(1, 3)); |
| 99 | + assertAcked( |
| 100 | + client(REMOTE_CLUSTER).admin() |
| 101 | + .indices() |
| 102 | + .prepareCreate(remoteIndex) |
| 103 | + .setSettings(indexSettings(numShardsRemote, randomIntBetween(0, 1))) |
| 104 | + .setMapping("some_field", "type=keyword") |
| 105 | + ); |
| 106 | + assertFalse( |
| 107 | + client(REMOTE_CLUSTER).admin() |
| 108 | + .cluster() |
| 109 | + .prepareHealth(remoteIndex) |
| 110 | + .setWaitForYellowStatus() |
| 111 | + .setTimeout(TimeValue.timeValueSeconds(10)) |
| 112 | + .get() |
| 113 | + .isTimedOut() |
| 114 | + ); |
| 115 | + totalDocs += indexDocs(client(REMOTE_CLUSTER), remoteIndex); |
| 116 | + |
| 117 | + String skipUnavailableKey = Strings.format("cluster.remote.%s.skip_unavailable", REMOTE_CLUSTER); |
| 118 | + Setting<?> skipUnavailableSetting = cluster(REMOTE_CLUSTER).clusterService().getClusterSettings().get(skipUnavailableKey); |
| 119 | + boolean skipUnavailable = (boolean) cluster(RemoteClusterAware.LOCAL_CLUSTER_GROUP_KEY).clusterService() |
| 120 | + .getClusterSettings() |
| 121 | + .get(skipUnavailableSetting); |
| 122 | + |
| 123 | + Map<String, Object> clusterInfo = new HashMap<>(); |
| 124 | + clusterInfo.put("local.num_shards", numShardsLocal); |
| 125 | + clusterInfo.put("local.index", localIndex); |
| 126 | + clusterInfo.put("remote.num_shards", numShardsRemote); |
| 127 | + clusterInfo.put("remote.index", remoteIndex); |
| 128 | + clusterInfo.put("remote.skip_unavailable", skipUnavailable); |
| 129 | + clusterInfo.put("total_docs", (long) totalDocs); |
| 130 | + return clusterInfo; |
| 131 | + } |
| 132 | + |
| 133 | + private int indexDocs(Client client, String index) { |
| 134 | + int numDocs = between(500, 1200); |
| 135 | + for (int i = 0; i < numDocs; i++) { |
| 136 | + client.prepareIndex(index).setSource("some_field", i).get(); |
| 137 | + } |
| 138 | + client.admin().indices().prepareRefresh(index).get(); |
| 139 | + return numDocs; |
| 140 | + } |
| 141 | + |
| 142 | + public static class CompoundRetriever extends RetrieverBuilder { |
| 143 | + |
| 144 | + private final List<RetrieverBuilder> sources; |
| 145 | + |
| 146 | + public CompoundRetriever(List<RetrieverBuilder> sources) { |
| 147 | + this.sources = sources; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public boolean isCompound() { |
| 152 | + return true; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public QueryBuilder topDocsQuery() { |
| 157 | + throw new UnsupportedOperationException("should not be called"); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public RetrieverBuilder rewrite(QueryRewriteContext ctx) throws IOException { |
| 162 | + if (ctx.getPointInTimeBuilder() == null) { |
| 163 | + throw new IllegalStateException("PIT is required"); |
| 164 | + } |
| 165 | + if (sources.isEmpty()) { |
| 166 | + StandardRetrieverBuilder standardRetrieverBuilder = new StandardRetrieverBuilder(); |
| 167 | + standardRetrieverBuilder.queryBuilder = new MatchAllQueryBuilder(); |
| 168 | + return standardRetrieverBuilder; |
| 169 | + } |
| 170 | + return sources.get(0); |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void extractToSearchSourceBuilder(SearchSourceBuilder searchSourceBuilder, boolean compoundUsed) { |
| 175 | + throw new UnsupportedOperationException("should not be called"); |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public String getName() { |
| 180 | + return "compound_retriever"; |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + protected void doToXContent(XContentBuilder builder, Params params) throws IOException { |
| 185 | + // no-op |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + protected boolean doEquals(Object o) { |
| 190 | + return false; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + protected int doHashCode() { |
| 195 | + return 0; |
| 196 | + } |
| 197 | + } |
| 198 | +} |
0 commit comments