|
26 | 26 | import java.util.List;
|
27 | 27 |
|
28 | 28 | import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
| 29 | +import static org.hamcrest.Matchers.aMapWithSize; |
29 | 30 | import static org.hamcrest.Matchers.arrayContaining;
|
| 31 | +import static org.hamcrest.Matchers.arrayContainingInAnyOrder; |
30 | 32 | import static org.hamcrest.Matchers.containsInAnyOrder;
|
31 | 33 | import static org.hamcrest.Matchers.equalTo;
|
| 34 | +import static org.hamcrest.Matchers.hasKey; |
32 | 35 | import static org.hamcrest.Matchers.hasSize;
|
33 | 36 |
|
34 | 37 | public class CCSFieldCapabilitiesIT extends AbstractMultiClustersTestCase {
|
@@ -126,4 +129,130 @@ public void testFailedToConnectToRemoteCluster() throws Exception {
|
126 | 129 | assertThat(failures, hasSize(1));
|
127 | 130 | assertThat(failures.get(0).getIndices(), arrayContaining("remote_cluster:*"));
|
128 | 131 | }
|
| 132 | + |
| 133 | + private void populateIndices(String localIndex, String remoteIndex, String remoteClusterAlias, boolean invertLocalRemoteMappings) { |
| 134 | + final Client localClient = client(LOCAL_CLUSTER); |
| 135 | + final Client remoteClient = client(remoteClusterAlias); |
| 136 | + |
| 137 | + String[] localMappings = new String[] { "timestamp", "type=date", "field1", "type=keyword", "field3", "type=keyword" }; |
| 138 | + String[] remoteMappings = new String[] { "timestamp", "type=date", "field2", "type=long", "field3", "type=long" }; |
| 139 | + |
| 140 | + assertAcked( |
| 141 | + localClient.admin().indices().prepareCreate(localIndex).setMapping(invertLocalRemoteMappings ? remoteMappings : localMappings) |
| 142 | + ); |
| 143 | + |
| 144 | + assertAcked( |
| 145 | + remoteClient.admin().indices().prepareCreate(remoteIndex).setMapping(invertLocalRemoteMappings ? localMappings : remoteMappings) |
| 146 | + ); |
| 147 | + } |
| 148 | + |
| 149 | + public void testIncludeIndices() { |
| 150 | + String localIndex = "index-local"; |
| 151 | + String remoteIndex = "index-remote"; |
| 152 | + String remoteClusterAlias = "remote_cluster"; |
| 153 | + populateIndices(localIndex, remoteIndex, remoteClusterAlias, false); |
| 154 | + remoteIndex = String.join(":", remoteClusterAlias, remoteIndex); |
| 155 | + FieldCapabilitiesResponse response = client().prepareFieldCaps(localIndex, remoteIndex) |
| 156 | + .setFields("*") |
| 157 | + .setIncludeIndices(true) |
| 158 | + .get(); |
| 159 | + |
| 160 | + assertThat(response.getIndices(), arrayContainingInAnyOrder(localIndex, remoteIndex)); |
| 161 | + assertThat(response.getField("timestamp"), aMapWithSize(1)); |
| 162 | + assertThat(response.getField("timestamp"), hasKey("date")); |
| 163 | + assertThat(response.getField("timestamp").get("date").indices(), arrayContainingInAnyOrder(localIndex, remoteIndex)); |
| 164 | + |
| 165 | + assertThat(response.getField("field1"), aMapWithSize(1)); |
| 166 | + assertThat(response.getField("field1"), hasKey("keyword")); |
| 167 | + assertThat(response.getField("field1").get("keyword").indices(), arrayContaining(localIndex)); |
| 168 | + |
| 169 | + assertThat(response.getField("field2"), aMapWithSize(1)); |
| 170 | + assertThat(response.getField("field2"), hasKey("long")); |
| 171 | + assertThat(response.getField("field2").get("long").indices(), arrayContaining(remoteIndex)); |
| 172 | + |
| 173 | + assertThat(response.getField("field3"), aMapWithSize(2)); |
| 174 | + assertThat(response.getField("field3"), hasKey("long")); |
| 175 | + assertThat(response.getField("field3"), hasKey("keyword")); |
| 176 | + // mapping conflict, therefore indices is always present for `field3` |
| 177 | + assertThat(response.getField("field3").get("long").indices(), arrayContaining(remoteIndex)); |
| 178 | + assertThat(response.getField("field3").get("keyword").indices(), arrayContaining(localIndex)); |
| 179 | + |
| 180 | + } |
| 181 | + |
| 182 | + public void testRandomIncludeIndices() { |
| 183 | + String localIndex = "index-local"; |
| 184 | + String remoteIndex = "index-remote"; |
| 185 | + String remoteClusterAlias = "remote_cluster"; |
| 186 | + populateIndices(localIndex, remoteIndex, remoteClusterAlias, false); |
| 187 | + remoteIndex = String.join(":", remoteClusterAlias, remoteIndex); |
| 188 | + boolean shouldAlwaysIncludeIndices = randomBoolean(); |
| 189 | + FieldCapabilitiesResponse response = client().prepareFieldCaps(localIndex, remoteIndex) |
| 190 | + .setFields("*") |
| 191 | + .setIncludeIndices(shouldAlwaysIncludeIndices) |
| 192 | + .get(); |
| 193 | + |
| 194 | + assertThat(response.getIndices(), arrayContainingInAnyOrder(localIndex, remoteIndex)); |
| 195 | + assertThat(response.getField("timestamp"), aMapWithSize(1)); |
| 196 | + assertThat(response.getField("timestamp"), hasKey("date")); |
| 197 | + if (shouldAlwaysIncludeIndices) { |
| 198 | + assertThat(response.getField("timestamp").get("date").indices(), arrayContainingInAnyOrder(localIndex, remoteIndex)); |
| 199 | + } else { |
| 200 | + assertNull(response.getField("timestamp").get("date").indices()); |
| 201 | + } |
| 202 | + |
| 203 | + assertThat(response.getField("field1"), aMapWithSize(1)); |
| 204 | + assertThat(response.getField("field1"), hasKey("keyword")); |
| 205 | + if (shouldAlwaysIncludeIndices) { |
| 206 | + assertThat(response.getField("field1").get("keyword").indices(), arrayContaining(localIndex)); |
| 207 | + } else { |
| 208 | + assertNull(response.getField("field1").get("keyword").indices()); |
| 209 | + } |
| 210 | + |
| 211 | + assertThat(response.getField("field2"), aMapWithSize(1)); |
| 212 | + assertThat(response.getField("field2"), hasKey("long")); |
| 213 | + if (shouldAlwaysIncludeIndices) { |
| 214 | + assertThat(response.getField("field2").get("long").indices(), arrayContaining(remoteIndex)); |
| 215 | + } else { |
| 216 | + assertNull(response.getField("field2").get("long").indices()); |
| 217 | + } |
| 218 | + |
| 219 | + assertThat(response.getField("field3"), aMapWithSize(2)); |
| 220 | + assertThat(response.getField("field3"), hasKey("long")); |
| 221 | + assertThat(response.getField("field3"), hasKey("keyword")); |
| 222 | + // mapping conflict, therefore indices is always present for `field3` |
| 223 | + assertThat(response.getField("field3").get("long").indices(), arrayContaining(remoteIndex)); |
| 224 | + assertThat(response.getField("field3").get("keyword").indices(), arrayContaining(localIndex)); |
| 225 | + } |
| 226 | + |
| 227 | + public void testIncludeIndicesSwapped() { |
| 228 | + // exact same setup as testIncludeIndices but with mappings swapped between local and remote index |
| 229 | + String localIndex = "index-local"; |
| 230 | + String remoteIndex = "index-remote"; |
| 231 | + String remoteClusterAlias = "remote_cluster"; |
| 232 | + populateIndices(localIndex, remoteIndex, remoteClusterAlias, true); |
| 233 | + remoteIndex = String.join(":", remoteClusterAlias, remoteIndex); |
| 234 | + FieldCapabilitiesResponse response = client().prepareFieldCaps(localIndex, remoteIndex) |
| 235 | + .setFields("*") |
| 236 | + .setIncludeIndices(true) |
| 237 | + .get(); |
| 238 | + |
| 239 | + assertThat(response.getIndices(), arrayContainingInAnyOrder(localIndex, remoteIndex)); |
| 240 | + assertThat(response.getField("timestamp"), aMapWithSize(1)); |
| 241 | + assertThat(response.getField("timestamp"), hasKey("date")); |
| 242 | + assertThat(response.getField("timestamp").get("date").indices(), arrayContainingInAnyOrder(localIndex, remoteIndex)); |
| 243 | + |
| 244 | + assertThat(response.getField("field1"), aMapWithSize(1)); |
| 245 | + assertThat(response.getField("field1"), hasKey("keyword")); |
| 246 | + assertThat(response.getField("field1").get("keyword").indices(), arrayContaining(remoteIndex)); |
| 247 | + |
| 248 | + assertThat(response.getField("field2"), aMapWithSize(1)); |
| 249 | + assertThat(response.getField("field2"), hasKey("long")); |
| 250 | + assertThat(response.getField("field2").get("long").indices(), arrayContaining(localIndex)); |
| 251 | + |
| 252 | + assertThat(response.getField("field3"), aMapWithSize(2)); |
| 253 | + assertThat(response.getField("field3"), hasKey("long")); |
| 254 | + assertThat(response.getField("field3"), hasKey("keyword")); |
| 255 | + assertThat(response.getField("field3").get("long").indices(), arrayContaining(localIndex)); |
| 256 | + assertThat(response.getField("field3").get("keyword").indices(), arrayContaining(remoteIndex)); |
| 257 | + } |
129 | 258 | }
|
0 commit comments