Skip to content

Commit

Permalink
fix type counts to include all results and not just the pages worth
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenwinship committed Dec 13, 2024
1 parent 9cc3f7f commit af10ae9
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
6 changes: 3 additions & 3 deletions doc/sphinx-guides/source/api/search.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ show_entity_ids boolean Whether or not to show the database IDs of the search
geo_point string Latitude and longitude in the form ``geo_point=42.3,-71.1``. You must supply ``geo_radius`` as well. See also :ref:`geospatial-search`.
geo_radius string Radial distance in kilometers from ``geo_point`` (which must be supplied as well) such as ``geo_radius=1.5``.
metadata_fields string Includes the requested fields for each dataset in the response. Multiple "metadata_fields" parameters can be used to include several fields. The value must be in the form "{metadata_block_name}:{field_name}" to include a specific field from a metadata block (see :ref:`example <dynamic-citation-some>`) or "{metadata_field_set_name}:\*" to include all the fields for a metadata block (see :ref:`example <dynamic-citation-all>`). "{field_name}" cannot be a subfield of a compound field. If "{field_name}" is a compound field, all subfields are included.
show_type_counts boolean Whether or not to include total_count_per_object_type for types: dataverse, dataset, and files.
show_type_counts boolean Whether or not to include total_count_per_object_type for types: Dataverse, Dataset, and Files.
================ ======= ===========

Basic Search Example
Expand Down Expand Up @@ -704,8 +704,8 @@ The above example ``metadata_fields=citation:dsDescription&metadata_fields=citat
],
"count_in_response": 4,
"total_count_per_object_type": {
"datasets": 2,
"dataverses": 2
"Datasets": 2,
"Dataverses": 2
}
}
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/edu/harvard/iq/dataverse/api/Search.java
Original file line number Diff line number Diff line change
Expand Up @@ -174,13 +174,9 @@ public Response search(
return error(Response.Status.INTERNAL_SERVER_ERROR, message);
}

Map<String, Integer> itemCountByType = new HashMap<>();
JsonArrayBuilder itemsArrayBuilder = Json.createArrayBuilder();
List<SolrSearchResult> solrSearchResults = solrQueryResponse.getSolrSearchResults();
for (SolrSearchResult solrSearchResult : solrSearchResults) {
if (showTypeCounts) {
itemCountByType.merge(solrSearchResult.getType(), 1, Integer::sum);
}
itemsArrayBuilder.add(solrSearchResult.json(showRelevance, showEntityIds, showApiUrls, metadataFields));
}

Expand Down Expand Up @@ -216,9 +212,13 @@ public Response search(
}

value.add("count_in_response", solrSearchResults.size());
if (showTypeCounts && !itemCountByType.isEmpty()) {
if (showTypeCounts && !solrQueryResponse.getTypeFacetCategories().isEmpty()) {
JsonObjectBuilder objectTypeCounts = Json.createObjectBuilder();
itemCountByType.forEach((k,v) -> objectTypeCounts.add(k,v));
for (FacetCategory facetCategory : solrQueryResponse.getTypeFacetCategories()) {
for (FacetLabel facetLabel : facetCategory.getFacetLabel()) {
objectTypeCounts.add(facetLabel.getName(), facetLabel.getCount());
}
}
value.add("total_count_per_object_type", objectTypeCounts);
}
/**
Expand Down
6 changes: 3 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 @@ -1404,8 +1404,8 @@ public void testShowTypeCounts() {
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));
.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));
}
}

0 comments on commit af10ae9

Please sign in to comment.