|
42 | 42 | import org.elasticsearch.client.RestHighLevelClient; |
43 | 43 | import org.elasticsearch.client.indices.CreateIndexRequest; |
44 | 44 | import org.elasticsearch.cluster.metadata.IndexMetadata; |
| 45 | +import org.elasticsearch.common.document.DocumentField; |
45 | 46 | import org.elasticsearch.common.settings.Settings; |
| 47 | +import org.elasticsearch.common.settings.Settings.Builder; |
46 | 48 | import org.elasticsearch.common.xcontent.DeprecationHandler; |
47 | 49 | import org.elasticsearch.common.xcontent.NamedXContentRegistry; |
48 | 50 | import org.elasticsearch.common.xcontent.XContentParser; |
49 | 51 | import org.elasticsearch.common.xcontent.json.JsonXContent; |
50 | 52 | import org.elasticsearch.rest.action.document.RestIndexAction; |
| 53 | +import org.elasticsearch.search.SearchHit; |
51 | 54 | import org.elasticsearch.test.hamcrest.ElasticsearchAssertions; |
52 | 55 | import org.elasticsearch.test.rest.ESRestTestCase; |
53 | 56 | import org.elasticsearch.test.rest.yaml.ObjectPath; |
54 | 57 |
|
55 | 58 | import java.io.IOException; |
56 | 59 | import java.io.UncheckedIOException; |
57 | 60 | import java.util.ArrayList; |
| 61 | +import java.util.Collections; |
58 | 62 | import java.util.List; |
59 | 63 | import java.util.Map; |
| 64 | +import java.util.Optional; |
60 | 65 | import java.util.concurrent.TimeUnit; |
61 | 66 | import java.util.stream.Collectors; |
62 | 67 |
|
@@ -260,4 +265,80 @@ public void testBWCSearchStates() throws Exception { |
260 | 265 | remoteClient.indices().delete(new DeleteIndexRequest(remoteIndex), RequestOptions.DEFAULT); |
261 | 266 | } |
262 | 267 | } |
| 268 | + |
| 269 | + // TODO move test to its own module where local and remote conditions can be controlled better |
| 270 | + public void testFieldsOptionEmulation() throws Exception { |
| 271 | + String localIndex = "test_bwc_fields_index"; |
| 272 | + String remoteIndex = "test_bwc_fields_remote_index"; |
| 273 | + try (RestHighLevelClient localClient = newLocalClient(); |
| 274 | + RestHighLevelClient remoteClient = newRemoteClient()) { |
| 275 | + localClient.indices().create(new CreateIndexRequest(localIndex) |
| 276 | + .settings(Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 5))), |
| 277 | + RequestOptions.DEFAULT); |
| 278 | + int localNumDocs = indexDocs(localClient, localIndex, between(10, 20)); |
| 279 | + // if not fully upgraded, we should find one node with older version |
| 280 | + Optional<Node> oldVersionNode = getNodes(remoteClient.getLowLevelClient()).stream().filter(n -> n.version.before(Version.CURRENT)).findFirst(); |
| 281 | + if (oldVersionNode.isPresent()) { |
| 282 | + System.out.println(oldVersionNode.get()); |
| 283 | + } |
| 284 | + Builder remoteIndexSettings = Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, between(1, 5)); |
| 285 | + if (oldVersionNode.isPresent()) { |
| 286 | + remoteIndexSettings.put(IndexMetadata.INDEX_ROUTING_REQUIRE_GROUP_PREFIX + "._name", oldVersionNode.get().name); |
| 287 | + } |
| 288 | + remoteClient.indices().create(new CreateIndexRequest(remoteIndex) |
| 289 | + .settings(remoteIndexSettings), |
| 290 | + RequestOptions.DEFAULT); |
| 291 | + int remoteNumDocs = indexDocs(remoteClient, remoteIndex, between(10, 20)); |
| 292 | + int expectedHitCount = localNumDocs + remoteNumDocs; |
| 293 | + |
| 294 | + configureRemoteClusters(getNodes(remoteClient.getLowLevelClient())); |
| 295 | + // TODO other test uses 20 iterations, don't understand why, should this do as well? |
| 296 | + RestClient lowLevelClient = localClient.getLowLevelClient(); |
| 297 | + List<Node> localNodes = getNodes(lowLevelClient); |
| 298 | + Node currentVersionNode = localNodes.stream().filter(n -> n.version.equals(Version.CURRENT)).findFirst().get(); |
| 299 | + lowLevelClient |
| 300 | + .setNodes( |
| 301 | + Collections.singletonList( |
| 302 | + new org.elasticsearch.client.Node( |
| 303 | + HttpHost.create(currentVersionNode.httpAddress), |
| 304 | + null, |
| 305 | + null, |
| 306 | + currentVersionNode.version.toString(), |
| 307 | + null, |
| 308 | + null |
| 309 | + ) |
| 310 | + ) |
| 311 | + ); |
| 312 | + |
| 313 | + for (String minimizeRoundTrips : new String[] { "true", "false" }) { |
| 314 | + Request request = new Request("POST", "/_search"); |
| 315 | + request.addParameter("index", localIndex + "," + CLUSTER_ALIAS + ":" + remoteIndex); |
| 316 | + if (UPGRADE_FROM_VERSION.onOrAfter(Version.V_7_0_0)) { |
| 317 | + request.addParameter("ccs_minimize_roundtrips", minimizeRoundTrips); |
| 318 | + } |
| 319 | + request.setJsonEntity("{\"_source\": false, \"fields\": [\"*\"] , \"size\": " + expectedHitCount + "}"); |
| 320 | + Response response = lowLevelClient.performRequest(request); |
| 321 | + try ( |
| 322 | + XContentParser parser = JsonXContent.jsonXContent.createParser( |
| 323 | + NamedXContentRegistry.EMPTY, |
| 324 | + DeprecationHandler.THROW_UNSUPPORTED_OPERATION, |
| 325 | + response.getEntity().getContent() |
| 326 | + ) |
| 327 | + ) { |
| 328 | + SearchResponse searchResponse = SearchResponse.fromXContent(parser); |
| 329 | + ElasticsearchAssertions.assertNoFailures(searchResponse); |
| 330 | + ElasticsearchAssertions.assertHitCount(searchResponse, expectedHitCount); |
| 331 | + SearchHit[] hits = searchResponse.getHits().getHits(); |
| 332 | + for (SearchHit hit : hits) { |
| 333 | + assertFalse("No source in hit expected but was: " + hit.toString(), hit.hasSource()); |
| 334 | + Map<String, DocumentField> fields = hit.getFields(); |
| 335 | + assertNotNull(fields); |
| 336 | + assertNotNull("Field `f` not found, hit was: " + hit.toString(), fields.get("f")); |
| 337 | + } |
| 338 | + } |
| 339 | + } |
| 340 | + localClient.indices().delete(new DeleteIndexRequest(localIndex), RequestOptions.DEFAULT); |
| 341 | + remoteClient.indices().delete(new DeleteIndexRequest(remoteIndex), RequestOptions.DEFAULT); |
| 342 | + } |
| 343 | + } |
263 | 344 | } |
0 commit comments