mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
DS-3489: Converting Discovery results to REST HAL objects part 2
This commit is contained in:
@@ -179,6 +179,10 @@ public class DiscoverResult {
|
||||
{
|
||||
return highlightResults.get(metadataKey);
|
||||
}
|
||||
|
||||
public Map<String, List<String>> getHighlightResults() {
|
||||
return highlightResults;
|
||||
}
|
||||
}
|
||||
|
||||
public void addSearchDocument(DSpaceObject dso, SearchDocument searchDocument){
|
||||
|
@@ -60,12 +60,12 @@ public class DiscoveryRestController implements InitializingBean {
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/search/objects")
|
||||
public PagedResources<SearchResultsResource> getSearchObjects(@RequestParam(name = "query", required = false) String query,
|
||||
public SearchResultsResource getSearchObjects(@RequestParam(name = "query", required = false) String query,
|
||||
@RequestParam(name = "dsoType", required = false) String dsoType,
|
||||
@RequestParam(name = "scope", required = false) String dsoScope,
|
||||
@RequestParam(name = "configuration", required = false) String configurationName,
|
||||
List<SearchFilter> searchFilters,
|
||||
Pageable page, PagedResourcesAssembler<SearchResultsRest> assembler) {
|
||||
Pageable page) {
|
||||
if(log.isTraceEnabled()) {
|
||||
log.trace("Searching with scope: " + StringUtils.trimToEmpty(dsoScope)
|
||||
+ ", configuration name: " + StringUtils.trimToEmpty(configurationName)
|
||||
@@ -75,15 +75,10 @@ public class DiscoveryRestController implements InitializingBean {
|
||||
}
|
||||
|
||||
//Get the Search results in JSON format
|
||||
Page<SearchResultsRest> searchResultsRest = discoveryRestRepository.getSearchObjects(query, dsoType, dsoScope, configurationName, searchFilters, page);
|
||||
|
||||
//Get the self link to this method
|
||||
Link selfLink = linkTo(methodOn(this.getClass()).getSearchObjects(query, dsoType, dsoScope, configurationName, searchFilters, page, assembler)).withSelfRel();
|
||||
SearchResultsRest searchResultsRest = discoveryRestRepository.getSearchObjects(query, dsoType, dsoScope, configurationName, searchFilters, page);
|
||||
|
||||
//Convert the Search JSON results to paginated HAL resources
|
||||
PagedResources<SearchResultsResource> pagedResources = assembler.toResource(searchResultsRest, new SearchResultsResourceAssembler(), selfLink);
|
||||
|
||||
return pagedResources;
|
||||
return new SearchResultsResource(searchResultsRest, page, utils);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/facets")
|
||||
@@ -114,11 +109,4 @@ public class DiscoveryRestController implements InitializingBean {
|
||||
//TODO
|
||||
}
|
||||
|
||||
private class SearchResultsResourceAssembler implements ResourceAssembler<SearchResultsRest, SearchResultsResource> {
|
||||
|
||||
public SearchResultsResource toResource(final SearchResultsRest entity) {
|
||||
return new SearchResultsResource(entity, utils);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package org.dspace.app.rest.converter;
|
||||
|
||||
import org.apache.commons.collections4.CollectionUtils;
|
||||
import org.apache.commons.collections4.MapUtils;
|
||||
import org.dspace.app.rest.model.DSpaceObjectRest;
|
||||
import org.dspace.app.rest.model.SearchResultEntryRest;
|
||||
import org.dspace.app.rest.model.SearchResultsRest;
|
||||
@@ -11,11 +12,13 @@ import org.dspace.discovery.DiscoverQuery;
|
||||
import org.dspace.discovery.DiscoverResult;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* TODO TOM UNIT TEST
|
||||
@@ -26,7 +29,7 @@ public class DiscoverResultConverter {
|
||||
@Autowired
|
||||
public List<DSpaceObjectConverter> converters;
|
||||
|
||||
public Page<SearchResultsRest> convert(final DiscoverQuery discoverQuery, final String configurationName, final String scope,
|
||||
public SearchResultsRest convert(final DiscoverQuery discoverQuery, final String configurationName, final String scope,
|
||||
final List<SearchFilter> searchFilters, final Pageable page, final DiscoverResult searchResult) {
|
||||
|
||||
SearchResultsRest resultsRest = new SearchResultsRest();
|
||||
@@ -35,15 +38,25 @@ public class DiscoverResultConverter {
|
||||
|
||||
addSearchResults(searchResult, resultsRest);
|
||||
|
||||
return null;
|
||||
resultsRest.setTotalNumberOfResults(searchResult.getTotalSearchResults());
|
||||
|
||||
return resultsRest;
|
||||
}
|
||||
|
||||
private <T> void addSearchResults(final DiscoverResult searchResult, final SearchResultsRest resultsRest) {
|
||||
private void addSearchResults(final DiscoverResult searchResult, final SearchResultsRest resultsRest) {
|
||||
for (DSpaceObject dspaceObject : CollectionUtils.emptyIfNull(searchResult.getDspaceObjects())) {
|
||||
SearchResultEntryRest resultEntry = new SearchResultEntryRest();
|
||||
|
||||
//Convert the DSpace Object to its REST model
|
||||
resultEntry.setDspaceObject(convertDSpaceObject(dspaceObject));
|
||||
//TODO HIT HIGHLIGHTING
|
||||
|
||||
//Add hit highlighting for this DSO if present
|
||||
DiscoverResult.DSpaceObjectHighlightResult highlightedResults = searchResult.getHighlightedResults(dspaceObject);
|
||||
if(highlightedResults != null && MapUtils.isNotEmpty(highlightedResults.getHighlightResults())) {
|
||||
for (Map.Entry<String, List<String>> metadataHighlight : highlightedResults.getHighlightResults().entrySet()) {
|
||||
resultEntry.addHitHighlights(metadataHighlight.getKey(), metadataHighlight.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
resultsRest.addSearchResult(resultEntry);
|
||||
}
|
||||
|
@@ -1,7 +1,10 @@
|
||||
package org.dspace.app.rest.model;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonIgnore;
|
||||
import org.dspace.app.rest.DiscoveryRestController;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
@@ -12,7 +15,9 @@ public class SearchResultEntryRest implements RestModel {
|
||||
public static final String NAME = "discover";
|
||||
public static final String CATEGORY = RestModel.DISCOVER;
|
||||
|
||||
private Map<String, String> hitHighlights;
|
||||
private Map<String, List<String>> hitHighlights;
|
||||
|
||||
@JsonIgnore
|
||||
private DSpaceObjectRest dspaceObject;
|
||||
|
||||
|
||||
@@ -28,15 +33,21 @@ public class SearchResultEntryRest implements RestModel {
|
||||
return DiscoveryRestController.class;
|
||||
}
|
||||
|
||||
public Map<String, String> getHitHighlights() {
|
||||
public Map<String, List<String>> getHitHighlights() {
|
||||
return hitHighlights;
|
||||
}
|
||||
|
||||
public void setHitHighlights(final Map<String, String> hitHighlights) {
|
||||
public void addHitHighlights(String key, List<String> value) {
|
||||
if(hitHighlights == null) {
|
||||
hitHighlights = new HashMap<>();
|
||||
}
|
||||
hitHighlights.put(key, value);
|
||||
}
|
||||
|
||||
public void setHitHighlights(final Map<String, List<String>> hitHighlights) {
|
||||
this.hitHighlights = hitHighlights;
|
||||
}
|
||||
|
||||
@LinkRest(linkClass = DSpaceObjectRest.class)
|
||||
public DSpaceObjectRest getDspaceObject() {
|
||||
return dspaceObject;
|
||||
}
|
||||
|
@@ -23,6 +23,7 @@ public class SearchResultsRest extends BaseObjectRest<String> {
|
||||
|
||||
@JsonIgnore
|
||||
List<SearchResultEntryRest> searchResults;
|
||||
private long totalNumberOfResults;
|
||||
|
||||
//TODO List<SearchFacetEntryRest> facets;
|
||||
|
||||
@@ -114,6 +115,14 @@ public class SearchResultsRest extends BaseObjectRest<String> {
|
||||
this.configurationName = configurationName;
|
||||
}
|
||||
|
||||
public long getTotalNumberOfResults() {
|
||||
return totalNumberOfResults;
|
||||
}
|
||||
|
||||
public void setTotalNumberOfResults(long totalNumberOfResults) {
|
||||
this.totalNumberOfResults = totalNumberOfResults;
|
||||
}
|
||||
|
||||
public class AppliedFilter {
|
||||
|
||||
private String filter;
|
||||
|
@@ -21,7 +21,7 @@ public abstract class HALResource extends ResourceSupport {
|
||||
return embedded;
|
||||
}
|
||||
|
||||
public void embedResource(String relationship, ResourceSupport resource) {
|
||||
public void embedResource(String relationship, Object resource) {
|
||||
embedded.put(relationship, resource);
|
||||
}
|
||||
|
||||
|
@@ -2,10 +2,14 @@ package org.dspace.app.rest.model.hateoas;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonUnwrapped;
|
||||
import org.dspace.app.rest.DiscoveryRestController;
|
||||
import org.dspace.app.rest.model.RestModel;
|
||||
import org.dspace.app.rest.model.SearchResultEntryRest;
|
||||
import org.dspace.app.rest.model.SearchResultsRest;
|
||||
import org.dspace.app.rest.utils.Utils;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.PagedResources;
|
||||
import org.springframework.hateoas.Resources;
|
||||
import org.springframework.web.util.UriComponentsBuilder;
|
||||
|
||||
@@ -24,23 +28,26 @@ public class SearchResultsResource extends HALResource {
|
||||
@JsonUnwrapped
|
||||
private final SearchResultsRest data;
|
||||
|
||||
public SearchResultsResource(final SearchResultsRest data, final Utils utils) {
|
||||
public SearchResultsResource(final SearchResultsRest data, final Pageable page, final Utils utils) {
|
||||
this.data = data;
|
||||
|
||||
|
||||
addLinks(data);
|
||||
|
||||
addEmbeds(data, utils);
|
||||
addEmbeds(data, page, utils);
|
||||
|
||||
}
|
||||
|
||||
private void addEmbeds(final SearchResultsRest data, final Utils utils) {
|
||||
private void addEmbeds(final SearchResultsRest data, final Pageable pageable, final Utils utils) {
|
||||
List<SearchResultEntryResource> entryResources = new LinkedList<>();
|
||||
for (SearchResultEntryRest searchResultEntry : data.getSearchResults()) {
|
||||
entryResources.add(new SearchResultEntryResource(searchResultEntry, utils));
|
||||
}
|
||||
|
||||
embedResource("searchResults", new Resources<>(entryResources));
|
||||
PageImpl<SearchResultEntryResource> page = new PageImpl<SearchResultEntryResource>(entryResources,
|
||||
pageable, data.getTotalNumberOfResults());
|
||||
|
||||
embedResource("searchResults", new EmbeddedPage(buildBaseLink(data), page, entryResources));
|
||||
|
||||
//TODO ADD FACETS
|
||||
}
|
||||
|
@@ -59,7 +59,7 @@ public class DiscoveryRestRepository extends AbstractDSpaceRestRepository {
|
||||
//TODO set "hasMore" property on facets
|
||||
}
|
||||
|
||||
public Page<SearchResultsRest> getSearchObjects(final String query, final String dsoType, final String dsoScope, final String configurationName, final List<SearchFilter> searchFilters, final Pageable page) {
|
||||
public SearchResultsRest getSearchObjects(final String query, final String dsoType, final String dsoScope, final String configurationName, final List<SearchFilter> searchFilters, final Pageable page) {
|
||||
Context context = obtainContext();
|
||||
|
||||
DSpaceObject scopeObject = scopeResolver.resolveScope(context, dsoScope);
|
||||
@@ -69,7 +69,7 @@ public class DiscoveryRestRepository extends AbstractDSpaceRestRepository {
|
||||
DiscoverQuery discoverQuery = null;
|
||||
|
||||
try {
|
||||
DiscoverQuery discoverQuery = queryBuilder.buildQuery(context, scopeObject, configuration, query, searchFilters, dsoType, page);
|
||||
discoverQuery = queryBuilder.buildQuery(context, scopeObject, configuration, query, searchFilters, dsoType, page);
|
||||
searchResult = searchService.search(context, scopeObject, discoverQuery);
|
||||
|
||||
} catch (InvalidRequestException e) {
|
||||
@@ -80,7 +80,6 @@ public class DiscoveryRestRepository extends AbstractDSpaceRestRepository {
|
||||
//TODO TOM handle search exception
|
||||
}
|
||||
|
||||
//TODO convert search result to DSO list
|
||||
return discoverResultConverter.convert(discoverQuery, configurationName, dsoScope, searchFilters, page, searchResult);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user