From 6a87cda01b28136fbc971c29aa46e836d349e29f Mon Sep 17 00:00:00 2001 From: Stephen Kraffmiller Date: Tue, 7 Jan 2025 15:35:08 -0500 Subject: [PATCH] #11127 get full array of counts --- .../edu/harvard/iq/dataverse/api/Search.java | 46 +++++++++++++++++-- .../harvard/iq/dataverse/api/SearchIT.java | 38 ++++++++++++++- 2 files changed, 78 insertions(+), 6 deletions(-) diff --git a/src/main/java/edu/harvard/iq/dataverse/api/Search.java b/src/main/java/edu/harvard/iq/dataverse/api/Search.java index ba82f8f758b..aa7c314d615 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/Search.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/Search.java @@ -34,6 +34,7 @@ import jakarta.ws.rs.container.ContainerRequestContext; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.Response; +import java.math.BigDecimal; import org.apache.commons.lang3.StringUtils; /** @@ -211,13 +212,50 @@ public Response search( } value.add("count_in_response", solrSearchResults.size()); - if (showTypeCounts && !solrQueryResponse.getTypeFacetCategories().isEmpty()) { + + // we want to show the missing dvobject types with count = 0 + // per https://github.com/IQSS/dataverse/issues/11127 + + if (showTypeCounts) { JsonObjectBuilder objectTypeCounts = Json.createObjectBuilder(); - for (FacetCategory facetCategory : solrQueryResponse.getTypeFacetCategories()) { - for (FacetLabel facetLabel : facetCategory.getFacetLabel()) { - objectTypeCounts.add(facetLabel.getName(), facetLabel.getCount()); + if (!solrQueryResponse.getTypeFacetCategories().isEmpty()) { + boolean filesMissing = true; + boolean datasetsMissing = true; + boolean dataversesMissing = true; + for (FacetCategory facetCategory : solrQueryResponse.getTypeFacetCategories()) { + for (FacetLabel facetLabel : facetCategory.getFacetLabel()) { + objectTypeCounts.add(facetLabel.getName(), facetLabel.getCount()); + if (facetLabel.getName().equals((SearchConstants.UI_DATAVERSES))) { + dataversesMissing = false; + } + if (facetLabel.getName().equals((SearchConstants.UI_DATASETS))) { + datasetsMissing = false; + } + if (facetLabel.getName().equals((SearchConstants.UI_FILES))) { + filesMissing = false; + } + } + } + + if (solrQueryResponse.getTypeFacetCategories().size() < 3) { + if (dataversesMissing) { + objectTypeCounts.add(SearchConstants.UI_DATAVERSES, 0); + } + if (datasetsMissing) { + objectTypeCounts.add(SearchConstants.UI_DATASETS, 0); + } + if (filesMissing) { + objectTypeCounts.add(SearchConstants.UI_FILES, 0); + } } + } + if (showTypeCounts && solrQueryResponse.getTypeFacetCategories().isEmpty()) { + objectTypeCounts.add(SearchConstants.UI_DATAVERSES, 0); + objectTypeCounts.add(SearchConstants.UI_DATASETS, 0); + objectTypeCounts.add(SearchConstants.UI_FILES, 0); + } + value.add("total_count_per_object_type", objectTypeCounts); } /** diff --git a/src/test/java/edu/harvard/iq/dataverse/api/SearchIT.java b/src/test/java/edu/harvard/iq/dataverse/api/SearchIT.java index 00c41073ebe..07e994deff6 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/SearchIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/SearchIT.java @@ -1751,11 +1751,14 @@ public void testShowTypeCounts() { String apiToken = UtilIT.getApiTokenFromResponse(createUser); String affiliation = "testAffiliation"; - // test total_count_per_object_type is not included because the results are empty + // test total_count_per_object_type is included with zero counts for each type Response searchResp = UtilIT.search(username, apiToken, "&show_type_counts=true"); searchResp.then().assertThat() .statusCode(OK.getStatusCode()) - .body("data.total_count_per_object_type", CoreMatchers.equalTo(null)); + .body("data.total_count_per_object_type.Dataverses", CoreMatchers.is(0)) + .body("data.total_count_per_object_type.Datasets", CoreMatchers.is(0)) + .body("data.total_count_per_object_type.Files", CoreMatchers.is(0)); + Response createDataverseResponse = UtilIT.createRandomDataverse(apiToken, affiliation); assertEquals(201, createDataverseResponse.getStatusCode()); @@ -1782,6 +1785,7 @@ public void testShowTypeCounts() { // This call forces a wait for dataset indexing to finish and gives time for file uploads to complete UtilIT.search("id:dataset_" + datasetId, apiToken); + UtilIT.sleepForReindex(datasetId, apiToken, 3); } // Test Search without show_type_counts @@ -1797,10 +1801,40 @@ public void testShowTypeCounts() { // Test Search with show_type_counts = TRUE searchResp = UtilIT.search(dataverseAlias, apiToken, "&show_type_counts=true"); searchResp.prettyPrint(); + searchResp.then().assertThat() .statusCode(OK.getStatusCode()) .body("data.total_count_per_object_type.Dataverses", CoreMatchers.is(1)) .body("data.total_count_per_object_type.Datasets", CoreMatchers.is(3)) .body("data.total_count_per_object_type.Files", CoreMatchers.is(6)); + + + + // go through the same exercise with only a collection to verify that Dataasets and Files + // are there with a count of 0 + + createDataverseResponse = UtilIT.createRandomDataverse(apiToken, affiliation); + assertEquals(201, createDataverseResponse.getStatusCode()); + dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse); + + + // Test Search without show_type_counts + searchResp = UtilIT.search(dataverseAlias, apiToken); + searchResp.then().assertThat() + .statusCode(OK.getStatusCode()) + .body("data.total_count_per_object_type", CoreMatchers.equalTo(null)); + // Test Search with show_type_counts = FALSE + searchResp = UtilIT.search(dataverseAlias, apiToken, "&show_type_counts=false"); + searchResp.then().assertThat() + .statusCode(OK.getStatusCode()) + .body("data.total_count_per_object_type", CoreMatchers.equalTo(null)); + // Test Search with show_type_counts = TRUE + searchResp = UtilIT.search(dataverseAlias, apiToken, "&show_type_counts=true"); + searchResp.prettyPrint(); + searchResp.then().assertThat() + .statusCode(OK.getStatusCode()) + .body("data.total_count_per_object_type.Dataverses", CoreMatchers.is(1)) + .body("data.total_count_per_object_type.Datasets", CoreMatchers.is(0)) + .body("data.total_count_per_object_type.Files", CoreMatchers.is(0)); } }