Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

11127 search api DV Object Type counts extended #11138

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### 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.
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);
g-saracca marked this conversation as resolved.
Show resolved Hide resolved
}

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));
}
}
Loading