diff --git a/server/src/main/java/au/org/aodn/ogcapi/server/core/service/ElasticSearch.java b/server/src/main/java/au/org/aodn/ogcapi/server/core/service/ElasticSearch.java index af5d6647..315d9571 100644 --- a/server/src/main/java/au/org/aodn/ogcapi/server/core/service/ElasticSearch.java +++ b/server/src/main/java/au/org/aodn/ogcapi/server/core/service/ElasticSearch.java @@ -650,7 +650,40 @@ public SearchResult searchFeatureSummary(String collectionId, Li for (var hit : response.hits().hits()) { EsFeatureCollectionModel hitFeatureCollection = hit.source(); if (hitFeatureCollection != null && hitFeatureCollection.getFeatures() != null) { - features.addAll(hitFeatureCollection.toFeatureCollectionGeoJSON().getFeatures()); + // A collectionID may map to several dataset key. So we need to identify features with dataset keys. TO get a dataset key which sits in hit.properties.key. For example: + // "properties": { + // "date": "2011-04", + // "collection": "4d3d4aca-472e-4616-88a5-df0f5ab401ba", + // "key": "mooring_acidification_realtime_qc.parquet" + // } + String datasetKey = null; + if (hitFeatureCollection.getProperties() != null) { + Object keyObj = hitFeatureCollection.getProperties().get("key"); + if (keyObj != null) { + datasetKey = keyObj.toString(); + } + } + + List documentFeatures = + hitFeatureCollection.toFeatureCollectionGeoJSON().getFeatures(); + + for (FeatureGeoJSON feature : documentFeatures) { + // add key in property field for each feature + if (datasetKey != null) { + Object featurePropsObj = feature.getProperties(); + + if (featurePropsObj instanceof Map) { + @SuppressWarnings("unchecked") + Map featurePropsMap = (Map) featurePropsObj; + featurePropsMap.put("key", datasetKey); + } else { + Map newPropsMap = new HashMap<>(); + newPropsMap.put("key", datasetKey); + feature.setProperties(newPropsMap); + } + } + features.add(feature); + } } } diff --git a/server/src/test/java/au/org/aodn/ogcapi/server/service/ElasticSearchTest.java b/server/src/test/java/au/org/aodn/ogcapi/server/service/ElasticSearchTest.java index 7447e40a..919e2b49 100644 --- a/server/src/test/java/au/org/aodn/ogcapi/server/service/ElasticSearchTest.java +++ b/server/src/test/java/au/org/aodn/ogcapi/server/service/ElasticSearchTest.java @@ -25,6 +25,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.*; public class ElasticSearchTest { @@ -61,10 +62,22 @@ public void searchFeatureSummaryTest() throws IOException { featureCollectionProperties.put("key", "satellite_ghrsst_l4_gamssa_1day_multi_sensor_world.zarr"); esFeatureCollection.setProperties(featureCollectionProperties); var coords = new ArrayList>>(); + var esFeature = new EsFeatureModel(); + + // mock a single point [147.338884, -43.190779] + List> ring = new ArrayList<>(); + List point = List.of( + new BigDecimal("147.338884"), + new BigDecimal("-43.190779") + ); + ring.add(point); + coords.add(ring); + var polygon = new EsPolygonModel(); polygon.setCoordinates(coords); - var esFeature = new EsFeatureModel(); + esFeature.setGeometry(polygon); + esFeatureCollection.setFeatures(List.of(esFeature)); when(hit.source()).thenReturn(esFeatureCollection); @@ -88,7 +101,18 @@ public void searchFeatureSummaryTest() throws IOException { assertNotNull(result); assertEquals(1, result.getCollections().size()); assertEquals(1L, result.getTotal()); - assertEquals(esFeature.toFeatureGeoJSON(), result.getCollections().get(0)); + // validate geometry keeps same after adding key property + assertEquals(esFeature.toFeatureGeoJSON().getGeometry(), + result.getCollections().get(0).getGeometry()); + + // validate key is in properties + FeatureGeoJSON returnedFeature = result.getCollections().get(0); + @SuppressWarnings("unchecked") + Map featureProps = (Map) returnedFeature.getProperties(); + + assertTrue(featureProps.containsKey("key")); + assertEquals("satellite_ghrsst_l4_gamssa_1day_multi_sensor_world.zarr", + featureProps.get("key")); }