Skip to content

Commit 8353b85

Browse files
authored
Merge pull request IQSS#11138 from IQSS/11127-search-api-counts
Search API show_type_counts=true now includes all object types
2 parents eb98e21 + 9d84ccc commit 8353b85

File tree

3 files changed

+84
-7
lines changed

3 files changed

+84
-7
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
### show-type-counts Behavior changed in Search API
2+
3+
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.
4+
5+
See also the [guides](https://preview.guides.gdcc.io/en/develop/api/search.html#parameters), #11127 and #11138.

src/main/java/edu/harvard/iq/dataverse/api/Search.java

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,13 +211,50 @@ public Response search(
211211
}
212212

213213
value.add("count_in_response", solrSearchResults.size());
214-
if (showTypeCounts && !solrQueryResponse.getTypeFacetCategories().isEmpty()) {
214+
215+
// we want to show the missing dvobject types with count = 0
216+
// per https://github.com/IQSS/dataverse/issues/11127
217+
218+
if (showTypeCounts) {
215219
JsonObjectBuilder objectTypeCounts = Json.createObjectBuilder();
216-
for (FacetCategory facetCategory : solrQueryResponse.getTypeFacetCategories()) {
217-
for (FacetLabel facetLabel : facetCategory.getFacetLabel()) {
218-
objectTypeCounts.add(facetLabel.getName(), facetLabel.getCount());
220+
if (!solrQueryResponse.getTypeFacetCategories().isEmpty()) {
221+
boolean filesMissing = true;
222+
boolean datasetsMissing = true;
223+
boolean dataversesMissing = true;
224+
for (FacetCategory facetCategory : solrQueryResponse.getTypeFacetCategories()) {
225+
for (FacetLabel facetLabel : facetCategory.getFacetLabel()) {
226+
objectTypeCounts.add(facetLabel.getName(), facetLabel.getCount());
227+
if (facetLabel.getName().equals((SearchConstants.UI_DATAVERSES))) {
228+
dataversesMissing = false;
229+
}
230+
if (facetLabel.getName().equals((SearchConstants.UI_DATASETS))) {
231+
datasetsMissing = false;
232+
}
233+
if (facetLabel.getName().equals((SearchConstants.UI_FILES))) {
234+
filesMissing = false;
235+
}
236+
}
237+
}
238+
239+
if (solrQueryResponse.getTypeFacetCategories().size() < 3) {
240+
if (dataversesMissing) {
241+
objectTypeCounts.add(SearchConstants.UI_DATAVERSES, 0);
242+
}
243+
if (datasetsMissing) {
244+
objectTypeCounts.add(SearchConstants.UI_DATASETS, 0);
245+
}
246+
if (filesMissing) {
247+
objectTypeCounts.add(SearchConstants.UI_FILES, 0);
248+
}
219249
}
250+
220251
}
252+
if (showTypeCounts && solrQueryResponse.getTypeFacetCategories().isEmpty()) {
253+
objectTypeCounts.add(SearchConstants.UI_DATAVERSES, 0);
254+
objectTypeCounts.add(SearchConstants.UI_DATASETS, 0);
255+
objectTypeCounts.add(SearchConstants.UI_FILES, 0);
256+
}
257+
221258
value.add("total_count_per_object_type", objectTypeCounts);
222259
}
223260
/**

src/test/java/edu/harvard/iq/dataverse/api/SearchIT.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,18 +1744,21 @@ public void testSearchFilesAndUrlImages() throws InterruptedException {
17441744
}
17451745

17461746
@Test
1747-
public void testShowTypeCounts() {
1747+
public void testShowTypeCounts() throws InterruptedException {
17481748
//Create 1 user and 1 Dataverse/Collection
17491749
Response createUser = UtilIT.createRandomUser();
17501750
String username = UtilIT.getUsernameFromResponse(createUser);
17511751
String apiToken = UtilIT.getApiTokenFromResponse(createUser);
17521752
String affiliation = "testAffiliation";
17531753

1754-
// test total_count_per_object_type is not included because the results are empty
1754+
// test total_count_per_object_type is included with zero counts for each type
17551755
Response searchResp = UtilIT.search(username, apiToken, "&show_type_counts=true");
17561756
searchResp.then().assertThat()
17571757
.statusCode(OK.getStatusCode())
1758-
.body("data.total_count_per_object_type", CoreMatchers.equalTo(null));
1758+
.body("data.total_count_per_object_type.Dataverses", CoreMatchers.is(0))
1759+
.body("data.total_count_per_object_type.Datasets", CoreMatchers.is(0))
1760+
.body("data.total_count_per_object_type.Files", CoreMatchers.is(0));
1761+
17591762

17601763
Response createDataverseResponse = UtilIT.createRandomDataverse(apiToken, affiliation);
17611764
assertEquals(201, createDataverseResponse.getStatusCode());
@@ -1782,6 +1785,7 @@ public void testShowTypeCounts() {
17821785

17831786
// This call forces a wait for dataset indexing to finish and gives time for file uploads to complete
17841787
UtilIT.search("id:dataset_" + datasetId, apiToken);
1788+
UtilIT.sleepForReindex(datasetId, apiToken, 3);
17851789
}
17861790

17871791
// Test Search without show_type_counts
@@ -1797,10 +1801,41 @@ public void testShowTypeCounts() {
17971801
// Test Search with show_type_counts = TRUE
17981802
searchResp = UtilIT.search(dataverseAlias, apiToken, "&show_type_counts=true");
17991803
searchResp.prettyPrint();
1804+
18001805
searchResp.then().assertThat()
18011806
.statusCode(OK.getStatusCode())
18021807
.body("data.total_count_per_object_type.Dataverses", CoreMatchers.is(1))
18031808
.body("data.total_count_per_object_type.Datasets", CoreMatchers.is(3))
18041809
.body("data.total_count_per_object_type.Files", CoreMatchers.is(6));
1810+
1811+
1812+
1813+
// go through the same exercise with only a collection to verify that Dataasets and Files
1814+
// are there with a count of 0
1815+
1816+
createDataverseResponse = UtilIT.createRandomDataverse(apiToken, affiliation);
1817+
assertEquals(201, createDataverseResponse.getStatusCode());
1818+
dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse);
1819+
1820+
sleep(4000); //make sure new dataverse gets indexed
1821+
1822+
// Test Search without show_type_counts
1823+
searchResp = UtilIT.search(dataverseAlias, apiToken);
1824+
searchResp.then().assertThat()
1825+
.statusCode(OK.getStatusCode())
1826+
.body("data.total_count_per_object_type", CoreMatchers.equalTo(null));
1827+
// Test Search with show_type_counts = FALSE
1828+
searchResp = UtilIT.search(dataverseAlias, apiToken, "&show_type_counts=false");
1829+
searchResp.then().assertThat()
1830+
.statusCode(OK.getStatusCode())
1831+
.body("data.total_count_per_object_type", CoreMatchers.equalTo(null));
1832+
// Test Search with show_type_counts = TRUE
1833+
searchResp = UtilIT.search(dataverseAlias, apiToken, "&show_type_counts=true");
1834+
searchResp.prettyPrint();
1835+
searchResp.then().assertThat()
1836+
.statusCode(OK.getStatusCode())
1837+
.body("data.total_count_per_object_type.Dataverses", CoreMatchers.is(1))
1838+
.body("data.total_count_per_object_type.Datasets", CoreMatchers.is(0))
1839+
.body("data.total_count_per_object_type.Files", CoreMatchers.is(0));
18051840
}
18061841
}

0 commit comments

Comments
 (0)