Skip to content

Commit 595c261

Browse files
authored
Include _index property for each hit in _mvt response (#77995)
Adds an _index tag for each feature in the hit layer.
1 parent e41cc5a commit 595c261

File tree

2 files changed

+41
-26
lines changed

2 files changed

+41
-26
lines changed

x-pack/plugin/vector-tile/src/javaRestTest/java/org/elasticsearch/xpack/vectortile/VectorTileRestIT.java

Lines changed: 39 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private void indexPoints() throws IOException {
8888
double y = (r.getMaxY() + r.getMinY()) / 2;
8989
for (int i = 0; i < 30; i += 10) {
9090
for (int j = 0; j <= i; j++) {
91-
final Request putRequest = new Request(HttpPost.METHOD_NAME, INDEX_POINTS + "/_doc");
91+
final Request putRequest = new Request(HttpPost.METHOD_NAME, INDEX_POINTS + "/_doc/");
9292
putRequest.setJsonEntity(
9393
"{\n"
9494
+ " \"location\": \"POINT("
@@ -117,10 +117,10 @@ private void indexPoints() throws IOException {
117117

118118
private void indexShapes() throws IOException {
119119
final Rectangle r = GeoTileUtils.toBoundingBox(x, y, z);
120-
createIndexAndPutGeometry(INDEX_POLYGON, toPolygon(r));
120+
createIndexAndPutGeometry(INDEX_POLYGON, toPolygon(r), "polygon");
121121
}
122122

123-
private void createIndexAndPutGeometry(String indexName, Geometry geometry) throws IOException {
123+
private void createIndexAndPutGeometry(String indexName, Geometry geometry, String id) throws IOException {
124124
final Request createRequest = new Request(HttpPut.METHOD_NAME, indexName);
125125
Response response = client().performRequest(createRequest);
126126
assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_OK));
@@ -140,7 +140,7 @@ private void createIndexAndPutGeometry(String indexName, Geometry geometry) thro
140140
response = client().performRequest(mappingRequest);
141141
assertThat(response.getStatusLine().getStatusCode(), Matchers.equalTo(HttpStatus.SC_OK));
142142

143-
final Request putRequest = new Request(HttpPost.METHOD_NAME, indexName + "/_doc");
143+
final Request putRequest = new Request(HttpPost.METHOD_NAME, indexName + "/_doc/" + id);
144144
putRequest.setJsonEntity(
145145
"{\n"
146146
+ " \"location\": \""
@@ -249,7 +249,7 @@ public void testBasicGet() throws Exception {
249249
mvtRequest.setJsonEntity("{\"size\" : 100}");
250250
final VectorTile.Tile tile = execute(mvtRequest);
251251
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
252-
assertLayer(tile, HITS_LAYER, 4096, 33, 1);
252+
assertLayer(tile, HITS_LAYER, 4096, 33, 2);
253253
assertLayer(tile, AGGS_LAYER, 4096, 1, 1);
254254
assertLayer(tile, META_LAYER, 4096, 1, 13);
255255
}
@@ -260,7 +260,7 @@ public void testIndexAllGet() throws Exception {
260260
final VectorTile.Tile tile = execute(mvtRequest);
261261
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
262262
// 33 points, 1 polygon and two from geometry collection
263-
assertLayer(tile, HITS_LAYER, 4096, 36, 1);
263+
assertLayer(tile, HITS_LAYER, 4096, 36, 2);
264264
assertLayer(tile, AGGS_LAYER, 4096, 256 * 256, 1);
265265
assertLayer(tile, META_LAYER, 4096, 1, 13);
266266
}
@@ -270,7 +270,7 @@ public void testExtent() throws Exception {
270270
mvtRequest.setJsonEntity("{\"size\" : 100, \"extent\" : 256}");
271271
final VectorTile.Tile tile = execute(mvtRequest);
272272
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
273-
assertLayer(tile, HITS_LAYER, 256, 33, 1);
273+
assertLayer(tile, HITS_LAYER, 256, 33, 2);
274274
assertLayer(tile, AGGS_LAYER, 256, 1, 1);
275275
assertLayer(tile, META_LAYER, 256, 1, 13);
276276
}
@@ -283,7 +283,7 @@ public void testExtentURL() throws Exception {
283283
mvtRequest.setJsonEntity("{\"size\" : 100, \"extent\" : 256}");
284284
final VectorTile.Tile tile = execute(mvtRequest);
285285
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
286-
assertLayer(tile, HITS_LAYER, 512, 33, 1);
286+
assertLayer(tile, HITS_LAYER, 512, 33, 2);
287287
assertLayer(tile, AGGS_LAYER, 512, 1, 1);
288288
assertLayer(tile, META_LAYER, 512, 1, 13);
289289
}
@@ -339,7 +339,7 @@ public void testGridPrecision() throws Exception {
339339
mvtRequest.setJsonEntity("{\"size\" : 100, \"grid_precision\": 7 }");
340340
final VectorTile.Tile tile = execute(mvtRequest);
341341
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
342-
assertLayer(tile, HITS_LAYER, 4096, 33, 1);
342+
assertLayer(tile, HITS_LAYER, 4096, 33, 2);
343343
assertLayer(tile, AGGS_LAYER, 4096, 1, 1);
344344
assertLayer(tile, META_LAYER, 4096, 1, 13);
345345
}
@@ -357,7 +357,7 @@ public void testGridType() throws Exception {
357357
mvtRequest.setJsonEntity("{\"size\" : 100, \"grid_type\": \"point\" }");
358358
final VectorTile.Tile tile = execute(mvtRequest);
359359
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
360-
assertLayer(tile, HITS_LAYER, 4096, 33, 1);
360+
assertLayer(tile, HITS_LAYER, 4096, 33, 2);
361361
assertLayer(tile, AGGS_LAYER, 4096, 1, 1);
362362
assertLayer(tile, META_LAYER, 4096, 1, 13);
363363
assertFeatureType(tile, AGGS_LAYER, VectorTile.Tile.GeomType.POINT);
@@ -367,7 +367,7 @@ public void testGridType() throws Exception {
367367
mvtRequest.setJsonEntity("{\"size\" : 100, \"grid_type\": \"grid\" }");
368368
final VectorTile.Tile tile = execute(mvtRequest);
369369
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
370-
assertLayer(tile, HITS_LAYER, 4096, 33, 1);
370+
assertLayer(tile, HITS_LAYER, 4096, 33, 2);
371371
assertLayer(tile, AGGS_LAYER, 4096, 1, 1);
372372
assertLayer(tile, META_LAYER, 4096, 1, 13);
373373
assertFeatureType(tile, AGGS_LAYER, VectorTile.Tile.GeomType.POLYGON);
@@ -388,7 +388,7 @@ public void testGridTypeURL() throws Exception {
388388
mvtRequest.setJsonEntity("{\"size\" : 100, \"grid_type\": \"point\" }");
389389
final VectorTile.Tile tile = execute(mvtRequest);
390390
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
391-
assertLayer(tile, HITS_LAYER, 4096, 33, 1);
391+
assertLayer(tile, HITS_LAYER, 4096, 33, 2);
392392
assertLayer(tile, AGGS_LAYER, 4096, 1, 1);
393393
assertLayer(tile, META_LAYER, 4096, 1, 13);
394394
assertFeatureType(tile, AGGS_LAYER, VectorTile.Tile.GeomType.POLYGON);
@@ -399,7 +399,7 @@ public void testNoAggLayer() throws Exception {
399399
mvtRequest.setJsonEntity("{\"size\" : 100, \"grid_precision\": 0 }");
400400
final VectorTile.Tile tile = execute(mvtRequest);
401401
assertThat(tile.getLayersCount(), Matchers.equalTo(2));
402-
assertLayer(tile, HITS_LAYER, 4096, 33, 1);
402+
assertLayer(tile, HITS_LAYER, 4096, 33, 2);
403403
assertLayer(tile, META_LAYER, 4096, 1, 8);
404404
}
405405

@@ -411,7 +411,7 @@ public void testNoAggLayerURL() throws Exception {
411411
mvtRequest.setJsonEntity("{\"size\" : 100, \"grid_precision\": 4 }");
412412
final VectorTile.Tile tile = execute(mvtRequest);
413413
assertThat(tile.getLayersCount(), Matchers.equalTo(2));
414-
assertLayer(tile, HITS_LAYER, 4096, 33, 1);
414+
assertLayer(tile, HITS_LAYER, 4096, 33, 2);
415415
assertLayer(tile, META_LAYER, 4096, 1, 8);
416416
}
417417

@@ -439,7 +439,7 @@ public void testDefaultSort() throws Exception {
439439
mvtRequest.setJsonEntity("{\"size\": 100 }");
440440
final VectorTile.Tile tile = execute(mvtRequest);
441441
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
442-
assertLayer(tile, HITS_LAYER, 4096, 34, 1);
442+
assertLayer(tile, HITS_LAYER, 4096, 34, 2);
443443
final VectorTile.Tile.Layer layer = getLayer(tile, HITS_LAYER);
444444
assertThat(layer.getFeatures(0).getType(), Matchers.equalTo(VectorTile.Tile.GeomType.POLYGON));
445445
assertLayer(tile, AGGS_LAYER, 4096, 256 * 256, 1);
@@ -450,7 +450,7 @@ public void testDefaultSort() throws Exception {
450450
mvtRequest.setJsonEntity("{\"size\": 100, \"sort\" : []}"); // override default sort
451451
final VectorTile.Tile tile = execute(mvtRequest);
452452
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
453-
assertLayer(tile, HITS_LAYER, 4096, 34, 1);
453+
assertLayer(tile, HITS_LAYER, 4096, 34, 2);
454454
final VectorTile.Tile.Layer layer = getLayer(tile, HITS_LAYER);
455455
assertThat(layer.getFeatures(0).getType(), Matchers.equalTo(VectorTile.Tile.GeomType.POINT));
456456
assertLayer(tile, AGGS_LAYER, 4096, 256 * 256, 1);
@@ -487,7 +487,7 @@ public void testRuntimeFieldWithSort() throws Exception {
487487

488488
final VectorTile.Tile tile = execute(mvtRequest);
489489
assertThat(tile.getLayersCount(), Matchers.equalTo(2));
490-
assertLayer(tile, HITS_LAYER, 4096, 34, 1);
490+
assertLayer(tile, HITS_LAYER, 4096, 34, 2);
491491
final VectorTile.Tile.Layer layer = getLayer(tile, HITS_LAYER);
492492
assertThat(layer.getFeatures(0).getType(), Matchers.equalTo(VectorTile.Tile.GeomType.POLYGON));
493493
assertLayer(tile, META_LAYER, 4096, 1, 8);
@@ -513,7 +513,7 @@ public void testRuntimeFieldWithSort() throws Exception {
513513

514514
final VectorTile.Tile tile = execute(mvtRequest);
515515
assertThat(tile.getLayersCount(), Matchers.equalTo(2));
516-
assertLayer(tile, HITS_LAYER, 4096, 34, 1);
516+
assertLayer(tile, HITS_LAYER, 4096, 34, 2);
517517
final VectorTile.Tile.Layer layer = getLayer(tile, HITS_LAYER);
518518
assertThat(layer.getFeatures(33).getType(), Matchers.equalTo(VectorTile.Tile.GeomType.POLYGON));
519519
assertLayer(tile, META_LAYER, 4096, 1, 8);
@@ -535,26 +535,29 @@ public void testBasicQueryGet() throws Exception {
535535
);
536536
final VectorTile.Tile tile = execute(mvtRequest);
537537
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
538-
assertLayer(tile, HITS_LAYER, 4096, 1, 1);
538+
assertLayer(tile, HITS_LAYER, 4096, 1, 2);
539539
assertLayer(tile, AGGS_LAYER, 4096, 1, 1);
540540
assertLayer(tile, META_LAYER, 4096, 1, 13);
541+
assertStringTag(getLayer(tile, HITS_LAYER), getLayer(tile, HITS_LAYER).getFeatures(0), "_index", INDEX_POINTS);
541542
}
542543

543544
public void testBasicShape() throws Exception {
544545
final Request mvtRequest = new Request(getHttpMethod(), INDEX_POLYGON + "/_mvt/location/" + z + "/" + x + "/" + y);
545546
final VectorTile.Tile tile = execute(mvtRequest);
546547
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
547-
assertLayer(tile, HITS_LAYER, 4096, 1, 1);
548+
assertLayer(tile, HITS_LAYER, 4096, 1, 2);
548549
assertLayer(tile, AGGS_LAYER, 4096, 256 * 256, 1);
549550
assertLayer(tile, META_LAYER, 4096, 1, 13);
551+
assertStringTag(getLayer(tile, HITS_LAYER), getLayer(tile, HITS_LAYER).getFeatures(0), "_index", INDEX_POLYGON);
552+
assertStringTag(getLayer(tile, HITS_LAYER), getLayer(tile, HITS_LAYER).getFeatures(0), "_id", "polygon");
550553
}
551554

552555
public void testWithFields() throws Exception {
553556
final Request mvtRequest = new Request(getHttpMethod(), INDEX_POLYGON + "/_mvt/location/" + z + "/" + x + "/" + y);
554557
mvtRequest.setJsonEntity("{\"fields\": [\"name\", \"value1\"] }");
555558
final VectorTile.Tile tile = execute(mvtRequest);
556559
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
557-
assertLayer(tile, HITS_LAYER, 4096, 1, 3);
560+
assertLayer(tile, HITS_LAYER, 4096, 1, 4);
558561
assertLayer(tile, AGGS_LAYER, 4096, 256 * 256, 1);
559562
assertLayer(tile, META_LAYER, 4096, 1, 13);
560563
}
@@ -574,13 +577,13 @@ public void testMinAgg() throws Exception {
574577
);
575578
final VectorTile.Tile tile = execute(mvtRequest);
576579
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
577-
assertLayer(tile, HITS_LAYER, 4096, 1, 1);
580+
assertLayer(tile, HITS_LAYER, 4096, 1, 2);
578581
assertLayer(tile, AGGS_LAYER, 4096, 256 * 256, 2);
579582
assertLayer(tile, META_LAYER, 4096, 1, 18);
580583
// check pipeline aggregation values
581584
final VectorTile.Tile.Layer metaLayer = getLayer(tile, META_LAYER);
582-
assertTag(metaLayer, metaLayer.getFeatures(0), "aggregations.minVal.min", 1.0);
583-
assertTag(metaLayer, metaLayer.getFeatures(0), "aggregations.minVal.max", 1.0);
585+
assertDoubleTag(metaLayer, metaLayer.getFeatures(0), "aggregations.minVal.min", 1.0);
586+
assertDoubleTag(metaLayer, metaLayer.getFeatures(0), "aggregations.minVal.max", 1.0);
584587
}
585588

586589
public void testOverlappingMultipolygon() throws Exception {
@@ -589,7 +592,7 @@ public void testOverlappingMultipolygon() throws Exception {
589592
final String index = "overlapping_multipolygon";
590593
final Rectangle r1 = new Rectangle(-160, 160, 80, -80);
591594
final Rectangle r2 = new Rectangle(-159, 161, 79, -81);
592-
createIndexAndPutGeometry(index, new MultiPolygon(List.of(toPolygon(r1), toPolygon(r2))));
595+
createIndexAndPutGeometry(index, new MultiPolygon(List.of(toPolygon(r1), toPolygon(r2))), "multi_polygon");
593596
final Request mvtRequest = new Request(getHttpMethod(), index + "/_mvt/location/0/0/0?grid_precision=0");
594597
final VectorTile.Tile tile = execute(mvtRequest);
595598
assertThat(tile.getLayersCount(), Matchers.equalTo(2));
@@ -618,7 +621,7 @@ private void assertLayer(VectorTile.Tile tile, String name, int extent, int numF
618621
assertThat(layer.getKeysCount(), Matchers.equalTo(numTags));
619622
}
620623

621-
private void assertTag(VectorTile.Tile.Layer layer, VectorTile.Tile.Feature feature, String tag, double value) {
624+
private void assertDoubleTag(VectorTile.Tile.Layer layer, VectorTile.Tile.Feature feature, String tag, double value) {
622625
for (int i = 0; i < feature.getTagsCount(); i += 2) {
623626
String thisTag = layer.getKeys(feature.getTags(i));
624627
if (tag.equals(thisTag)) {
@@ -630,6 +633,16 @@ private void assertTag(VectorTile.Tile.Layer layer, VectorTile.Tile.Feature feat
630633
fail("Could not find tag [" + tag + " ]");
631634
}
632635

636+
private void assertStringTag(VectorTile.Tile.Layer layer, VectorTile.Tile.Feature feature, String tag, String value) {
637+
for (int i = 0; i < feature.getTagsCount(); i += 2) {
638+
String thisTag = layer.getKeys(feature.getTags(i));
639+
if (tag.equals(thisTag)) {
640+
VectorTile.Tile.Value thisValue = layer.getValues(feature.getTags(i + 1));
641+
assertEquals(thisValue.getStringValue(), value);
642+
}
643+
}
644+
}
645+
633646
private VectorTile.Tile execute(Request mvtRequest) throws IOException {
634647
final Response response = client().performRequest(mvtRequest);
635648
final InputStream inputStream = response.getEntity().getContent();

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public class RestVectorTileAction extends BaseRestHandler {
7070

7171
private static final String COUNT_TAG = "_count";
7272
private static final String ID_TAG = "_id";
73+
private static final String INDEX_TAG = "_index";
7374

7475
// mime type as defined by the mapbox vector tile specification
7576
private static final String MIME_TYPE = "application/vnd.mapbox-vector-tile";
@@ -237,6 +238,7 @@ private static VectorTile.Tile.Layer.Builder buildHitsLayer(SearchHit[] hits, Ve
237238
featureBuilder.clear();
238239
featureBuilder.mergeFrom((byte[]) feature);
239240
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, ID_TAG, searchHit.getId());
241+
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, INDEX_TAG, searchHit.getIndex());
240242
if (fields != null) {
241243
for (FieldAndFormat field : fields) {
242244
final DocumentField documentField = searchHit.field(field.field);

0 commit comments

Comments
 (0)