Skip to content

Commit d535666

Browse files
authored
Build index qualified name in cross cluster vector tile search (#94574)
Use the method RemoteClusterAware#buildRemoteIndexName to populate the _index tag.
1 parent 790f54a commit d535666

File tree

3 files changed

+57
-12
lines changed

3 files changed

+57
-12
lines changed

docs/changelog/94574.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 94574
2+
summary: Build index qualified name in cross cluster vector tile search
3+
area: Geo
4+
type: bug
5+
issues:
6+
- 94557

x-pack/plugin/vector-tile/qa/multi-cluster/src/javaRestTest/java/org/elasticsearch/vectortile/VectorTileCCSIT.java

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,19 +69,31 @@ public void testBasic() throws IOException {
6969
try (RestClient local = buildLocalClusterClient(); RestClient remote = buildRemoteClusterClient()) {
7070
final int localGeometries = createIndex(local, "test");
7171
final int remoteGeometries = createIndex(remote, "test");
72-
// check call in each cluster
73-
final Request mvtRequest = new Request(HttpPost.METHOD_NAME, "test/_mvt/location/0/0/0");
74-
final VectorTile.Tile localTile = execute(local, mvtRequest);
75-
assertThat(getLayer(localTile, "hits").getFeaturesCount(), Matchers.equalTo(localGeometries));
76-
final VectorTile.Tile remoteTile = execute(remote, mvtRequest);
77-
assertThat(getLayer(remoteTile, "hits").getFeaturesCount(), Matchers.equalTo(remoteGeometries));
78-
// call to both clusters
79-
final Request mvtCCSRequest = new Request(HttpPost.METHOD_NAME, "/test,other:test/_mvt/location/0/0/0");
80-
final VectorTile.Tile ccsTile = execute(local, mvtCCSRequest);
81-
assertThat(getLayer(ccsTile, "hits").getFeaturesCount(), Matchers.equalTo(localGeometries + remoteGeometries));
72+
// check with no params
73+
assertLocalAndRemote(local, remote, localGeometries, remoteGeometries, "");
74+
// check with labels
75+
assertLocalAndRemote(local, remote, 2 * localGeometries, 2 * remoteGeometries, "?with_labels=true");
8276
}
8377
}
8478

79+
private void assertLocalAndRemote(RestClient local, RestClient remote, int localGeometries, int remoteGeometries, String param)
80+
throws IOException {
81+
// check call in each cluster
82+
final Request mvtRequest = new Request(HttpPost.METHOD_NAME, "test/_mvt/location/0/0/0" + param);
83+
final VectorTile.Tile localTile = execute(local, mvtRequest);
84+
assertThat(getLayer(localTile, "hits").getFeaturesCount(), Matchers.equalTo(localGeometries));
85+
assertEquals(localGeometries, countFeaturesWithTag(getLayer(localTile, "hits"), "_index", "test"));
86+
final VectorTile.Tile remoteTile = execute(remote, mvtRequest);
87+
assertThat(getLayer(remoteTile, "hits").getFeaturesCount(), Matchers.equalTo(remoteGeometries));
88+
assertEquals(remoteGeometries, countFeaturesWithTag(getLayer(remoteTile, "hits"), "_index", "test"));
89+
// call to both clusters
90+
final Request mvtCCSRequest = new Request(HttpPost.METHOD_NAME, "/test,other:test/_mvt/location/0/0/0" + param);
91+
final VectorTile.Tile ccsTile = execute(local, mvtCCSRequest);
92+
assertThat(getLayer(ccsTile, "hits").getFeaturesCount(), Matchers.equalTo(localGeometries + remoteGeometries));
93+
assertEquals(localGeometries, countFeaturesWithTag(getLayer(ccsTile, "hits"), "_index", "test"));
94+
assertEquals(remoteGeometries, countFeaturesWithTag(getLayer(ccsTile, "hits"), "_index", "other:test"));
95+
}
96+
8597
private VectorTile.Tile.Layer getLayer(VectorTile.Tile tile, String layerName) {
8698
for (int i = 0; i < tile.getLayersCount(); i++) {
8799
final VectorTile.Tile.Layer layer = tile.getLayers(i);
@@ -117,4 +129,26 @@ private RestClient buildClient(final String url) throws IOException {
117129
);
118130
return buildClient(restAdminSettings(), new HttpHost[] { httpHost });
119131
}
132+
133+
private int countFeaturesWithTag(VectorTile.Tile.Layer layer, String tag, String value) {
134+
int count = 0;
135+
for (int i = 0; i < layer.getFeaturesCount(); i++) {
136+
VectorTile.Tile.Feature feature = layer.getFeatures(i);
137+
if (hasLabel(layer, feature, tag, value)) {
138+
count++;
139+
}
140+
}
141+
return count;
142+
}
143+
144+
private boolean hasLabel(VectorTile.Tile.Layer layer, VectorTile.Tile.Feature feature, String tag, String value) {
145+
for (int i = 0; i < feature.getTagsCount(); i += 2) {
146+
String thisTag = layer.getKeys(feature.getTags(i));
147+
if (tag.equals(thisTag)) {
148+
VectorTile.Tile.Value thisValue = layer.getValues(feature.getTags(i + 1));
149+
return value.equals(thisValue.getStringValue());
150+
}
151+
}
152+
return false;
153+
}
120154
}

x-pack/plugin/vector-tile/src/main/java/org/elasticsearch/xpack/vectortile/rest/RestVectorTileAction.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656

5757
import static org.elasticsearch.rest.RestRequest.Method.GET;
5858
import static org.elasticsearch.rest.RestRequest.Method.POST;
59+
import static org.elasticsearch.transport.RemoteClusterAware.buildRemoteIndexName;
5960

6061
/**
6162
* Main class handling a call to the _mvt API.
@@ -294,7 +295,7 @@ private static VectorTile.Tile.Layer.Builder buildHitsLayer(SearchHit[] hits, Ve
294295
featureBuilder.clear();
295296
featureBuilder.mergeFrom((byte[]) feature);
296297
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, ID_TAG, searchHit.getId());
297-
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, INDEX_TAG, searchHit.getIndex());
298+
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, INDEX_TAG, buildQualifiedIndex(searchHit));
298299
addHitsFields(featureBuilder, layerProps, requestField, fields);
299300
hitsLayerBuilder.addFeatures(featureBuilder);
300301
}
@@ -308,7 +309,7 @@ private static VectorTile.Tile.Layer.Builder buildHitsLayer(SearchHit[] hits, Ve
308309
featureBuilder.clear();
309310
featureBuilder.mergeFrom(labelPosFeature);
310311
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, ID_TAG, searchHit.getId());
311-
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, INDEX_TAG, searchHit.getIndex());
312+
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, INDEX_TAG, buildQualifiedIndex(searchHit));
312313
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, LABEL_POSITION_FIELD_NAME, true);
313314
addHitsFields(featureBuilder, layerProps, requestField, fields);
314315
hitsLayerBuilder.addFeatures(featureBuilder);
@@ -333,6 +334,10 @@ private static void addHitsFields(
333334
}
334335
}
335336

337+
private static String buildQualifiedIndex(SearchHit hit) {
338+
return buildRemoteIndexName(hit.getClusterAlias(), hit.getIndex());
339+
}
340+
336341
private static VectorTile.Tile.Layer.Builder buildAggsLayer(
337342
InternalGeoGrid<?> grid,
338343
VectorTileRequest request,

0 commit comments

Comments
 (0)