Skip to content

Commit

Permalink
#11127 get full array of counts
Browse files Browse the repository at this point in the history
  • Loading branch information
sekmiller committed Jan 7, 2025
1 parent 3352bb7 commit 6a87cda
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 6 deletions.
46 changes: 42 additions & 4 deletions src/main/java/edu/harvard/iq/dataverse/api/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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);
}
/**
Expand Down
38 changes: 36 additions & 2 deletions src/test/java/edu/harvard/iq/dataverse/api/SearchIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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
Expand All @@ -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));
}
}

0 comments on commit 6a87cda

Please sign in to comment.