Skip to content

Commit

Permalink
Merge pull request #9820 from IQSS/9785-files-api-extension-search
Browse files Browse the repository at this point in the history
Dataset files API extension for search text filtering
  • Loading branch information
kcondon authored Sep 28, 2023
2 parents 0cd37a4 + 9cc1eba commit 0de95c4
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 18 deletions.
3 changes: 3 additions & 0 deletions doc/release-notes/9785-files-api-extension-search-text.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
The getVersionFiles endpoint (/api/datasets/{id}/versions/{versionId}/files) has been extended to support optional filtering by search text through the `searchText` query parameter.

The search will be applied to the labels and descriptions of the dataset files.
8 changes: 8 additions & 0 deletions doc/sphinx-guides/source/api/native-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,14 @@ Usage example:
curl "https://demo.dataverse.org/api/datasets/24/versions/1.0/files?contentType=image/png"
Filtering by search text is also optionally supported. The search will be applied to the labels and descriptions of the dataset files, to return the files that contain the text searched in one of such fields.

Usage example:

.. code-block:: bash
curl "https://demo.dataverse.org/api/datasets/24/versions/1.0/files?searchText=word"
File access filtering is also optionally supported. In particular, by the following possible values:

* ``Public``
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,11 @@ public enum DataFileAccessStatus {
* @param contentType for retrieving only files with this content type
* @param accessStatus for retrieving only files with this DataFileAccessStatus
* @param categoryName for retrieving only files categorized with this category name
* @param searchText for retrieving only files that contain the specified text within their labels or descriptions
* @param orderCriteria a FileMetadatasOrderCriteria to order the results
* @return a FileMetadata list from the specified DatasetVersion
*/
public List<FileMetadata> getFileMetadatas(DatasetVersion datasetVersion, Integer limit, Integer offset, String contentType, DataFileAccessStatus accessStatus, String categoryName, FileMetadatasOrderCriteria orderCriteria) {
public List<FileMetadata> getFileMetadatas(DatasetVersion datasetVersion, Integer limit, Integer offset, String contentType, DataFileAccessStatus accessStatus, String categoryName, String searchText, FileMetadatasOrderCriteria orderCriteria) {
JPAQuery<FileMetadata> baseQuery = createBaseQuery(datasetVersion, orderCriteria);

if (contentType != null) {
Expand All @@ -72,6 +73,10 @@ public List<FileMetadata> getFileMetadatas(DatasetVersion datasetVersion, Intege
if (categoryName != null) {
baseQuery.from(dataFileCategory).where(dataFileCategory.name.eq(categoryName).and(fileMetadata.fileCategories.contains(dataFileCategory)));
}
if (searchText != null && !searchText.isEmpty()) {
searchText = searchText.trim().toLowerCase();
baseQuery.where(fileMetadata.label.lower().contains(searchText).or(fileMetadata.description.lower().contains(searchText)));
}

applyOrderCriteriaToQuery(baseQuery, orderCriteria);

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/edu/harvard/iq/dataverse/api/Datasets.java
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,7 @@ public Response getVersionFiles(@Context ContainerRequestContext crc,
@QueryParam("contentType") String contentType,
@QueryParam("accessStatus") String accessStatus,
@QueryParam("categoryName") String categoryName,
@QueryParam("searchText") String searchText,
@QueryParam("orderCriteria") String orderCriteria,
@Context UriInfo uriInfo,
@Context HttpHeaders headers) {
Expand All @@ -516,7 +517,7 @@ public Response getVersionFiles(@Context ContainerRequestContext crc,
} catch (IllegalArgumentException e) {
return error(Response.Status.BAD_REQUEST, "Invalid access status: " + accessStatus);
}
return ok(jsonFileMetadatas(datasetVersionFilesServiceBean.getFileMetadatas(datasetVersion, limit, offset, contentType, dataFileAccessStatus, categoryName, fileMetadatasOrderCriteria)));
return ok(jsonFileMetadatas(datasetVersionFilesServiceBean.getFileMetadatas(datasetVersion, limit, offset, contentType, dataFileAccessStatus, categoryName, searchText, fileMetadatasOrderCriteria)));
}, getRequestUser(crc));
}

Expand Down
40 changes: 25 additions & 15 deletions src/test/java/edu/harvard/iq/dataverse/api/DatasetsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3303,7 +3303,7 @@ public void getVersionFiles() throws IOException {
int testPageSize = 2;

// Test page 1
Response getVersionFilesResponsePaginated = UtilIT.getVersionFiles(datasetId, testDatasetVersion, testPageSize, null, null, null, null, null, apiToken);
Response getVersionFilesResponsePaginated = UtilIT.getVersionFiles(datasetId, testDatasetVersion, testPageSize, null, null, null, null, null, null, apiToken);

getVersionFilesResponsePaginated.then().assertThat()
.statusCode(OK.getStatusCode())
Expand All @@ -3317,7 +3317,7 @@ public void getVersionFiles() throws IOException {
String testFileId2 = JsonPath.from(getVersionFilesResponsePaginated.body().asString()).getString("data[1].dataFile.id");

// Test page 2
getVersionFilesResponsePaginated = UtilIT.getVersionFiles(datasetId, testDatasetVersion, testPageSize, testPageSize, null, null, null, null, apiToken);
getVersionFilesResponsePaginated = UtilIT.getVersionFiles(datasetId, testDatasetVersion, testPageSize, testPageSize, null, null, null, null, null, apiToken);

getVersionFilesResponsePaginated.then().assertThat()
.statusCode(OK.getStatusCode())
Expand All @@ -3330,7 +3330,7 @@ public void getVersionFiles() throws IOException {
String testFileId3 = JsonPath.from(getVersionFilesResponsePaginated.body().asString()).getString("data[0].dataFile.id");

// Test page 3 (last)
getVersionFilesResponsePaginated = UtilIT.getVersionFiles(datasetId, testDatasetVersion, testPageSize, testPageSize * 2, null, null, null, null, apiToken);
getVersionFilesResponsePaginated = UtilIT.getVersionFiles(datasetId, testDatasetVersion, testPageSize, testPageSize * 2, null, null, null, null, null, apiToken);

getVersionFilesResponsePaginated.then().assertThat()
.statusCode(OK.getStatusCode())
Expand All @@ -3340,7 +3340,7 @@ public void getVersionFiles() throws IOException {
assertEquals(1, fileMetadatasCount);

// Test NameZA order criteria
Response getVersionFilesResponseNameZACriteria = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, null, null, DatasetVersionFilesServiceBean.FileMetadatasOrderCriteria.NameZA.toString(), apiToken);
Response getVersionFilesResponseNameZACriteria = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, null, null, null, DatasetVersionFilesServiceBean.FileMetadatasOrderCriteria.NameZA.toString(), apiToken);

getVersionFilesResponseNameZACriteria.then().assertThat()
.statusCode(OK.getStatusCode())
Expand All @@ -3351,7 +3351,7 @@ public void getVersionFiles() throws IOException {
.body("data[4].label", equalTo(testFileName1));

// Test Newest order criteria
Response getVersionFilesResponseNewestCriteria = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, null, null, DatasetVersionFilesServiceBean.FileMetadatasOrderCriteria.Newest.toString(), apiToken);
Response getVersionFilesResponseNewestCriteria = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, null, null, null, DatasetVersionFilesServiceBean.FileMetadatasOrderCriteria.Newest.toString(), apiToken);

getVersionFilesResponseNewestCriteria.then().assertThat()
.statusCode(OK.getStatusCode())
Expand All @@ -3362,7 +3362,7 @@ public void getVersionFiles() throws IOException {
.body("data[4].label", equalTo(testFileName1));

// Test Oldest order criteria
Response getVersionFilesResponseOldestCriteria = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, null, null, DatasetVersionFilesServiceBean.FileMetadatasOrderCriteria.Oldest.toString(), apiToken);
Response getVersionFilesResponseOldestCriteria = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, null, null, null, DatasetVersionFilesServiceBean.FileMetadatasOrderCriteria.Oldest.toString(), apiToken);

getVersionFilesResponseOldestCriteria.then().assertThat()
.statusCode(OK.getStatusCode())
Expand All @@ -3373,7 +3373,7 @@ public void getVersionFiles() throws IOException {
.body("data[4].label", equalTo(testFileName4));

// Test Size order criteria
Response getVersionFilesResponseSizeCriteria = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, null, null, DatasetVersionFilesServiceBean.FileMetadatasOrderCriteria.Size.toString(), apiToken);
Response getVersionFilesResponseSizeCriteria = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, null, null, null, DatasetVersionFilesServiceBean.FileMetadatasOrderCriteria.Size.toString(), apiToken);

getVersionFilesResponseSizeCriteria.then().assertThat()
.statusCode(OK.getStatusCode())
Expand All @@ -3384,7 +3384,7 @@ public void getVersionFiles() throws IOException {
.body("data[4].label", equalTo(testFileName4));

// Test Type order criteria
Response getVersionFilesResponseTypeCriteria = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, null, null, DatasetVersionFilesServiceBean.FileMetadatasOrderCriteria.Type.toString(), apiToken);
Response getVersionFilesResponseTypeCriteria = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, null, null, null, DatasetVersionFilesServiceBean.FileMetadatasOrderCriteria.Type.toString(), apiToken);

getVersionFilesResponseTypeCriteria.then().assertThat()
.statusCode(OK.getStatusCode())
Expand All @@ -3396,13 +3396,13 @@ public void getVersionFiles() throws IOException {

// Test invalid order criteria
String invalidOrderCriteria = "invalidOrderCriteria";
Response getVersionFilesResponseInvalidOrderCriteria = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, null, null, invalidOrderCriteria, apiToken);
Response getVersionFilesResponseInvalidOrderCriteria = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, null, null, null, invalidOrderCriteria, apiToken);
getVersionFilesResponseInvalidOrderCriteria.then().assertThat()
.statusCode(BAD_REQUEST.getStatusCode())
.body("message", equalTo("Invalid order criteria: " + invalidOrderCriteria));

// Test Content Type
Response getVersionFilesResponseContentType = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, "image/png", null, null, null, apiToken);
Response getVersionFilesResponseContentType = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, "image/png", null, null, null, null, apiToken);

getVersionFilesResponseContentType.then().assertThat()
.statusCode(OK.getStatusCode())
Expand All @@ -3412,7 +3412,7 @@ public void getVersionFiles() throws IOException {
assertEquals(1, fileMetadatasCount);

// Test Category Name
Response getVersionFilesResponseCategoryName = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, "image/png", null, "nonExistentCategory", null, apiToken);
Response getVersionFilesResponseCategoryName = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, "image/png", null, "nonExistentCategory", null, null, apiToken);

getVersionFilesResponseCategoryName.then().assertThat()
.statusCode(OK.getStatusCode());
Expand All @@ -3425,7 +3425,7 @@ public void getVersionFiles() throws IOException {
restrictFileResponse.then().assertThat()
.statusCode(OK.getStatusCode());

Response getVersionFilesResponseRestricted = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, DatasetVersionFilesServiceBean.DataFileAccessStatus.Restricted.toString(), null, null, apiToken);
Response getVersionFilesResponseRestricted = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, DatasetVersionFilesServiceBean.DataFileAccessStatus.Restricted.toString(), null, null, null, apiToken);

getVersionFilesResponseRestricted.then().assertThat()
.statusCode(OK.getStatusCode())
Expand All @@ -3450,7 +3450,7 @@ public void getVersionFiles() throws IOException {
createActiveFileEmbargoResponse.then().assertThat()
.statusCode(OK.getStatusCode());

Response getVersionFilesResponseEmbargoedThenPublic = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, DatasetVersionFilesServiceBean.DataFileAccessStatus.EmbargoedThenPublic.toString(), null, null, apiToken);
Response getVersionFilesResponseEmbargoedThenPublic = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, DatasetVersionFilesServiceBean.DataFileAccessStatus.EmbargoedThenPublic.toString(), null, null, null, apiToken);

getVersionFilesResponseEmbargoedThenPublic.then().assertThat()
.statusCode(OK.getStatusCode())
Expand All @@ -3459,7 +3459,7 @@ public void getVersionFiles() throws IOException {
fileMetadatasCount = getVersionFilesResponseEmbargoedThenPublic.jsonPath().getList("data").size();
assertEquals(1, fileMetadatasCount);

Response getVersionFilesResponseEmbargoedThenRestricted = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, DatasetVersionFilesServiceBean.DataFileAccessStatus.EmbargoedThenRestricted.toString(), null, null, apiToken);
Response getVersionFilesResponseEmbargoedThenRestricted = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, DatasetVersionFilesServiceBean.DataFileAccessStatus.EmbargoedThenRestricted.toString(), null, null, null, apiToken);

getVersionFilesResponseEmbargoedThenRestricted.then().assertThat()
.statusCode(OK.getStatusCode())
Expand All @@ -3469,7 +3469,7 @@ public void getVersionFiles() throws IOException {
assertEquals(1, fileMetadatasCount);

// Test Access Status Public
Response getVersionFilesResponsePublic = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, DatasetVersionFilesServiceBean.DataFileAccessStatus.Public.toString(), null, null, apiToken);
Response getVersionFilesResponsePublic = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, DatasetVersionFilesServiceBean.DataFileAccessStatus.Public.toString(), null, null, null, apiToken);

getVersionFilesResponsePublic.then().assertThat()
.statusCode(OK.getStatusCode())
Expand All @@ -3479,5 +3479,15 @@ public void getVersionFiles() throws IOException {

fileMetadatasCount = getVersionFilesResponsePublic.jsonPath().getList("data").size();
assertEquals(3, fileMetadatasCount);

// Test Search Text
Response getVersionFilesResponseSearchText = UtilIT.getVersionFiles(datasetId, testDatasetVersion, null, null, null, null, null, "test_1", null, apiToken);

getVersionFilesResponseSearchText.then().assertThat()
.statusCode(OK.getStatusCode())
.body("data[0].label", equalTo(testFileName1));

fileMetadatasCount = getVersionFilesResponseSearchText.jsonPath().getList("data").size();
assertEquals(1, fileMetadatasCount);
}
}
5 changes: 4 additions & 1 deletion src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -3276,7 +3276,7 @@ static Response getDatasetVersionCitation(Integer datasetId, String version, Str
return response;
}

static Response getVersionFiles(Integer datasetId, String version, Integer limit, Integer offset, String contentType, String accessStatus, String categoryName, String orderCriteria, String apiToken) {
static Response getVersionFiles(Integer datasetId, String version, Integer limit, Integer offset, String contentType, String accessStatus, String categoryName, String searchText, String orderCriteria, String apiToken) {
RequestSpecification requestSpecification = given()
.header(API_TOKEN_HTTP_HEADER, apiToken)
.contentType("application/json");
Expand All @@ -3295,6 +3295,9 @@ static Response getVersionFiles(Integer datasetId, String version, Integer limit
if (categoryName != null) {
requestSpecification = requestSpecification.queryParam("categoryName", categoryName);
}
if (searchText != null) {
requestSpecification = requestSpecification.queryParam("searchText", searchText);
}
if (orderCriteria != null) {
requestSpecification = requestSpecification.queryParam("orderCriteria", orderCriteria);
}
Expand Down

0 comments on commit 0de95c4

Please sign in to comment.