Skip to content

Commit

Permalink
Fix fields wildcard support to vector tile search API (#85595) (#85615)
Browse files Browse the repository at this point in the history
This commit changes the strategy on how fields are fetched by iterating over the returned map. In addition, the geo field is added the last one on the fields list so it does not get overridden.
  • Loading branch information
iverase committed Apr 1, 2022
1 parent 9f5a883 commit fd3774c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/85595.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 85595
summary: Fix fields wildcard support to vector tile search API
area: Geo
type: bug
issues:
- 85592
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,36 @@ public void testWithFields() throws Exception {
assertLayer(tile, META_LAYER, 4096, 1, 13);
}

public void testWithNoExistingFields() throws Exception {
final Request mvtRequest = new Request(getHttpMethod(), INDEX_POLYGON + "/_mvt/location/" + z + "/" + x + "/" + y);
mvtRequest.setJsonEntity("{\"fields\": [\"otherField\"] }");
final VectorTile.Tile tile = execute(mvtRequest);
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
assertLayer(tile, HITS_LAYER, 4096, 1, 2);
assertLayer(tile, AGGS_LAYER, 4096, 256 * 256, 2);
assertLayer(tile, META_LAYER, 4096, 1, 13);
}

public void testWithNullFields() throws Exception {
final Request mvtRequest = new Request(getHttpMethod(), INDEX_POLYGON + "/_mvt/location/" + z + "/" + x + "/" + y);
mvtRequest.setJsonEntity("{\"fields\": [\"nullField\"] }");
final VectorTile.Tile tile = execute(mvtRequest);
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
assertLayer(tile, HITS_LAYER, 4096, 1, 2);
assertLayer(tile, AGGS_LAYER, 4096, 256 * 256, 2);
assertLayer(tile, META_LAYER, 4096, 1, 13);
}

public void testWithFieldsWildCard() throws Exception {
final Request mvtRequest = new Request(getHttpMethod(), INDEX_POLYGON + "/_mvt/location/" + z + "/" + x + "/" + y);
mvtRequest.setJsonEntity("{\"fields\": [\"*\"] }");
final VectorTile.Tile tile = execute(mvtRequest);
assertThat(tile.getLayersCount(), Matchers.equalTo(3));
assertLayer(tile, HITS_LAYER, 4096, 1, 5);
assertLayer(tile, AGGS_LAYER, 4096, 256 * 256, 2);
assertLayer(tile, META_LAYER, 4096, 1, 13);
}

public void testSingleValueAgg() throws Exception {
final Request mvtRequest = new Request(getHttpMethod(), INDEX_POLYGON + "/_mvt/location/" + z + "/" + x + "/" + y);
mvtRequest.setJsonEntity(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -180,15 +181,16 @@ private static SearchRequestBuilder searchRequestBuilder(RestCancellableNodeClie
searchRequestBuilder.setSize(request.getSize());
searchRequestBuilder.setFetchSource(false);
searchRequestBuilder.setTrackTotalHitsUpTo(request.getTrackTotalHitsUpTo());
for (FieldAndFormat field : request.getFieldAndFormats()) {
searchRequestBuilder.addFetchField(field);
}
// added last in case there is a wildcard, the last one is picked
searchRequestBuilder.addFetchField(
new FieldAndFormat(
request.getField(),
"mvt(" + request.getZ() + "/" + request.getX() + "/" + request.getY() + "@" + request.getExtent() + ")"
)
);
for (FieldAndFormat field : request.getFieldAndFormats()) {
searchRequestBuilder.addFetchField(field);
}
searchRequestBuilder.setRuntimeMappings(request.getRuntimeMappings());
QueryBuilder qBuilder = QueryBuilders.geoShapeQuery(request.getField(), request.getBoundingBox());
if (request.getQueryBuilder() != null) {
Expand Down Expand Up @@ -256,7 +258,6 @@ private static SearchRequestBuilder searchRequestBuilder(RestCancellableNodeClie
@SuppressWarnings("unchecked")
private static VectorTile.Tile.Layer.Builder buildHitsLayer(SearchHit[] hits, VectorTileRequest request) throws IOException {
final VectorTile.Tile.Layer.Builder hitsLayerBuilder = VectorTileUtils.createLayerBuilder(HITS_LAYER, request.getExtent());
final List<FieldAndFormat> fields = request.getFieldAndFormats();
final MvtLayerProps layerProps = new MvtLayerProps();
final VectorTile.Tile.Feature.Builder featureBuilder = VectorTile.Tile.Feature.newBuilder();
for (SearchHit searchHit : hits) {
Expand All @@ -269,12 +270,10 @@ private static VectorTile.Tile.Layer.Builder buildHitsLayer(SearchHit[] hits, Ve
featureBuilder.mergeFrom((byte[]) feature);
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, ID_TAG, searchHit.getId());
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, INDEX_TAG, searchHit.getIndex());
if (fields != null) {
for (FieldAndFormat field : fields) {
final DocumentField documentField = searchHit.field(field.field);
if (documentField != null) {
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, field.field, documentField.getValue());
}
final Map<String, DocumentField> fields = searchHit.getDocumentFields();
for (String field : fields.keySet()) {
if (request.getField().equals(field) == false) {
VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, field, fields.get(field).getValue());
}
}
hitsLayerBuilder.addFeatures(featureBuilder);
Expand Down

0 comments on commit fd3774c

Please sign in to comment.