Skip to content

Commit

Permalink
Merge pull request IQSS#11138 from IQSS/11127-search-api-counts
Browse files Browse the repository at this point in the history
Search API show_type_counts=true now includes all object types
  • Loading branch information
ofahimIQSS authored Feb 3, 2025
2 parents eb98e21 + 9d84ccc commit 8353b85
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
### show-type-counts Behavior changed in Search API

In the Search API if you set show_type_counts=true the response will include all object types (Dataverses, Datasets, and Files) even if the search result for any given type is 0.

See also the [guides](https://preview.guides.gdcc.io/en/develop/api/search.html#parameters), #11127 and #11138.
45 changes: 41 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 @@ -211,13 +211,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
41 changes: 38 additions & 3 deletions src/test/java/edu/harvard/iq/dataverse/api/SearchIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1744,18 +1744,21 @@ public void testSearchFilesAndUrlImages() throws InterruptedException {
}

@Test
public void testShowTypeCounts() {
public void testShowTypeCounts() throws InterruptedException {
//Create 1 user and 1 Dataverse/Collection
Response createUser = UtilIT.createRandomUser();
String username = UtilIT.getUsernameFromResponse(createUser);
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,41 @@ 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);

sleep(4000); //make sure new dataverse gets indexed

// 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 8353b85

Please sign in to comment.