use of edu.harvard.iq.dataverse.search.FileView in project dataverse by IQSS.
the class Index method filesearch.
@GET
@Path("filesearch")
public Response filesearch(@QueryParam("persistentId") String persistentId, @QueryParam("semanticVersion") String semanticVersion, @QueryParam("q") String userSuppliedQuery) {
Dataset dataset = datasetService.findByGlobalId(persistentId);
if (dataset == null) {
return error(Status.BAD_REQUEST, "Could not find dataset with persistent id " + persistentId);
}
User user = GuestUser.get();
try {
AuthenticatedUser authenticatedUser = findAuthenticatedUserOrDie();
if (authenticatedUser != null) {
user = authenticatedUser;
}
} catch (WrappedResponse ex) {
}
RetrieveDatasetVersionResponse datasetVersionResponse = datasetVersionService.retrieveDatasetVersionByPersistentId(persistentId, semanticVersion);
if (datasetVersionResponse == null) {
return error(Status.BAD_REQUEST, "Problem searching for files. Could not find dataset version based on " + persistentId + " and " + semanticVersion);
}
DatasetVersion datasetVersion = datasetVersionResponse.getDatasetVersion();
FileView fileView = searchFilesService.getFileView(datasetVersion, user, userSuppliedQuery);
if (fileView == null) {
return error(Status.BAD_REQUEST, "Problem searching for files. Null returned from getFileView.");
}
JsonArrayBuilder filesFound = Json.createArrayBuilder();
JsonArrayBuilder cards = Json.createArrayBuilder();
JsonArrayBuilder fileIds = Json.createArrayBuilder();
for (SolrSearchResult result : fileView.getSolrSearchResults()) {
cards.add(result.getNameSort());
fileIds.add(result.getEntityId());
JsonObjectBuilder fileFound = Json.createObjectBuilder();
fileFound.add("name", result.getNameSort());
fileFound.add("entityId", result.getEntityId().toString());
fileFound.add("datasetVersionId", result.getDatasetVersionId());
fileFound.add("datasetId", result.getParent().get(SearchFields.ID));
filesFound.add(fileFound);
}
JsonArrayBuilder facets = Json.createArrayBuilder();
for (FacetCategory facetCategory : fileView.getFacetCategoryList()) {
facets.add(facetCategory.getFriendlyName());
}
JsonArrayBuilder filterQueries = Json.createArrayBuilder();
for (String filterQuery : fileView.getFilterQueries()) {
filterQueries.add(filterQuery);
}
JsonArrayBuilder allDatasetVersionIds = Json.createArrayBuilder();
for (DatasetVersion dsVersion : dataset.getVersions()) {
allDatasetVersionIds.add(dsVersion.getId());
}
JsonObjectBuilder data = Json.createObjectBuilder();
data.add("filesFound", filesFound);
data.add("cards", cards);
data.add("fileIds", fileIds);
data.add("facets", facets);
data.add("user", user.getIdentifier());
data.add("persistentID", persistentId);
data.add("query", fileView.getQuery());
data.add("filterQueries", filterQueries);
data.add("allDataverVersionIds", allDatasetVersionIds);
data.add("semanticVersion", datasetVersion.getSemanticVersion());
return ok(data);
}
Aggregations