Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,52 @@ public Page<MetadataValueWrapperRest> findByValue(@Parameter(value = "schema", r
searchValue = searchValue.replace(":", "");
}

List<MetadataValueWrapper> metadataValueWrappers = new ArrayList<>();
// Perform a search, but only retrieve the total count of results, not the actual objects
DiscoverResult searchResult = createAndRunDiscoverResult(context, metadataField, searchValue, 0);
long totalResultsLong = searchResult.getTotalSearchResults();
// Safe conversion from long to int
int totalResults = (totalResultsLong > Integer.MAX_VALUE) ?
Integer.MAX_VALUE : (int) totalResultsLong;
// Perform the search again, this time retrieving the actual results based on the total count
searchResult = createAndRunDiscoverResult(context, metadataField, searchValue, totalResults);
for (IndexableObject object : searchResult.getIndexableObjects()) {
if (object instanceof IndexableItem) {
// Get the item which has the metadata with the search value
List<MetadataValue> metadataValues = itemService.getMetadataByMetadataString(
((IndexableItem) object).getIndexedObject(), metadataField);

// The Item could have more metadata than the metadata with searching value, filter that metadata
String finalSearchValue = searchValue;
List<MetadataValue> filteredMetadataValues = metadataValues.stream()
.filter(metadataValue -> metadataValue.getValue().contains(finalSearchValue))
.collect(Collectors.toList());

// convert metadata values to the wrapper
List<MetadataValueWrapper> metadataValueWrapperList =
this.convertMetadataValuesToWrappers(filteredMetadataValues);
metadataValueWrappers.addAll(metadataValueWrapperList);
}
}

// filter eu sponsor -> do not return eu sponsor suggestions for items where eu sponsor is used.
// openAIRE API
if (StringUtils.equals(schemaName, "local") && StringUtils.equals(elementName, "sponsor")) {
metadataValueWrappers = filterEUSponsors(metadataValueWrappers);
}
metadataValueWrappers = distinctMetadataValues(metadataValueWrappers);

return converter.toRestPage(metadataValueWrappers, pageable, utils.obtainProjection());
}

/**
* Create a discover query and retrieve the results from the Solr Search core.
*/
private DiscoverResult createAndRunDiscoverResult(Context context, String metadataField,
String searchValue, int maxResults) {
// Find matches in Solr Search core
DiscoverQuery discoverQuery =
this.createDiscoverQuery(metadataField, searchValue);
this.createDiscoverQuery(metadataField, searchValue, maxResults);

if (ObjectUtils.isEmpty(discoverQuery)) {
throw new IllegalArgumentException("Cannot create a DiscoverQuery from the arguments.");
Expand All @@ -124,41 +167,12 @@ public Page<MetadataValueWrapperRest> findByValue(@Parameter(value = "schema", r
if (StringUtils.isNotBlank(normalizedQuery)) {
discoverQuery.setQuery(normalizedQuery);
}

List<MetadataValueWrapper> metadataValueWrappers = new ArrayList<>();
try {
DiscoverResult searchResult = searchService.search(context, discoverQuery);
for (IndexableObject object : searchResult.getIndexableObjects()) {
if (object instanceof IndexableItem) {
// Get the item which has the metadata with the search value
List<MetadataValue> metadataValues = itemService.getMetadataByMetadataString(
((IndexableItem) object).getIndexedObject(), metadataField);

// The Item could have more metadata than the metadata with searching value, filter that metadata
String finalSearchValue = searchValue;
List<MetadataValue> filteredMetadataValues = metadataValues.stream()
.filter(metadataValue -> metadataValue.getValue().contains(finalSearchValue))
.collect(Collectors.toList());

// convert metadata values to the wrapper
List<MetadataValueWrapper> metadataValueWrapperList =
this.convertMetadataValuesToWrappers(filteredMetadataValues);
metadataValueWrappers.addAll(metadataValueWrapperList);
}
}
return searchService.search(context, discoverQuery);
} catch (SearchServiceException e) {
log.error("Error while searching with Discovery", e);
throw new IllegalArgumentException("Error while searching with Discovery: " + e.getMessage());
}

// filter eu sponsor -> do not return eu sponsor suggestions for items where eu sponsor is used.
// openAIRE API
if (StringUtils.equals(schemaName, "local") && StringUtils.equals(elementName, "sponsor")) {
metadataValueWrappers = filterEUSponsors(metadataValueWrappers);
}
metadataValueWrappers = distinctMetadataValues(metadataValueWrappers);

return converter.toRestPage(metadataValueWrappers, pageable, utils.obtainProjection());
}

public List<MetadataValueWrapper> filterEUSponsors(List<MetadataValueWrapper> metadataWrappers) {
Expand All @@ -172,11 +186,10 @@ public List<MetadataValueWrapper> distinctMetadataValues(List<MetadataValueWrapp
.collect( Collectors.toList() );
}

private DiscoverQuery createDiscoverQuery(String metadataField, String searchValue) {
private DiscoverQuery createDiscoverQuery(String metadataField, String searchValue, int maxResults) {
DiscoverQuery discoverQuery = new DiscoverQuery();
discoverQuery.setQuery(metadataField + ":" + "*" + searchValue + "*");
discoverQuery.setMaxResults(500);
// return only metadata field values
discoverQuery.setMaxResults(maxResults);
discoverQuery.addSearchField(metadataField);
discoverQuery.addFilterQueries("search.resourcetype:" + IndexableItem.TYPE);

Expand All @@ -195,7 +208,7 @@ private List<MetadataValueWrapper> convertMetadataValuesToWrappers(List<Metadata


@Override
@PreAuthorize("permitAll()")
@PreAuthorize("hasAuthority('AUTHENTICATED')")
public MetadataValueWrapperRest findOne(Context context, Integer id) {
MetadataValueWrapper metadataValueWrapper = new MetadataValueWrapper();
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,12 @@
import org.dspace.app.rest.model.hateoas.VocabularyEntryResource;
import org.dspace.app.rest.utils.Utils;
import org.dspace.core.Context;
import org.dspace.discovery.DiscoverFacetField;
import org.dspace.discovery.DiscoverQuery;
import org.dspace.discovery.DiscoverResult;
import org.dspace.discovery.SearchService;
import org.dspace.discovery.SearchServiceException;
import org.dspace.discovery.indexobject.IndexableItem;
import org.dspace.discovery.configuration.DiscoveryConfigurationParameters;
import org.dspace.services.ConfigurationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
Expand Down Expand Up @@ -230,8 +231,12 @@ private List<VocabularyEntryRest> loadSuggestionsFromSolr(String autocompleteCus
DiscoverQuery discoverQuery = new DiscoverQuery();
// Process the custom query if it contains the specific query parameter `?query=`
autocompleteCustom = updateAutocompleteAndQuery(autocompleteCustom, discoverQuery);
// TODO - search facets and process facet results instead of indexable objects
discoverQuery.setMaxResults(500);
DiscoverFacetField facetField = new DiscoverFacetField(autocompleteCustom,
DiscoveryConfigurationParameters.TYPE_STANDARD,
-1, // no limit (get all facet values)
DiscoveryConfigurationParameters.SORT.VALUE // sorting order
);
discoverQuery.addFacetField(facetField);
// return only metadata field values
discoverQuery.addSearchField(autocompleteCustom);

Expand All @@ -255,30 +260,17 @@ private List<VocabularyEntryRest> loadSuggestionsFromSolr(String autocompleteCus
*/
private void processSolrSearchResults(DiscoverResult searchResult, String autocompleteCustom, String searchValue,
List<VocabularyEntryRest> results) {
searchResult.getIndexableObjects().forEach(object -> {
if (!(object instanceof IndexableItem)) {
return;
}
IndexableItem item = (IndexableItem) object;
// Get all search documents for the item.
searchResult.getSearchDocument(item).forEach((searchDocument) -> {
searchResult.getFacetResult(autocompleteCustom).forEach(facetResult -> {
String displayedValue = facetResult.getDisplayedValue();
if (displayedValue.contains(searchValue)) {
// Create a new VocabularyEntryRest object
VocabularyEntryRest vocabularyEntryRest = new VocabularyEntryRest();
// All values from Item's specific index - it could contain values we are not looking for.
// The must be filtered out.
List<String> docValues = searchDocument.getSearchFieldValues(autocompleteCustom);

// Filter values that contain searchValue
List<String> filteredValues = docValues.stream()
.filter(value -> value.contains(searchValue))
.collect(Collectors.toList());

// Add filtered values to the results. It contains only values that contain searchValue.
filteredValues.forEach(value -> {
vocabularyEntryRest.setDisplay(value);
vocabularyEntryRest.setValue(value);
results.add(vocabularyEntryRest);
});
});
vocabularyEntryRest.setDisplay(displayedValue);
vocabularyEntryRest.setValue(displayedValue);

// Add the filtered value to the results
results.add(vocabularyEntryRest);
}
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public void findAll() throws Exception {
// Get title metadata from the item
MetadataValue titleMetadataValue = this.getTitleMetadataValue();

getClient().perform(get("/api/core/metadatavalues")
String token = getAuthToken(eperson.getEmail(), password);
getClient(token).perform(get("/api/core/metadatavalues")
.param("size", String.valueOf(100)))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
Expand All @@ -105,7 +106,8 @@ public void findOne() throws Exception {
// Get title metadata from the item
MetadataValue titleMetadataValue = this.getTitleMetadataValue();

getClient().perform(get("/api/core/metadatavalues/" + titleMetadataValue.getID()))
String token = getAuthToken(eperson.getEmail(), password);
getClient(token).perform(get("/api/core/metadatavalues/" + titleMetadataValue.getID()))
.andExpect(status().isOk())
.andExpect(content().contentType(contentType))
.andExpect(jsonPath("$", Matchers.is(
Expand Down Expand Up @@ -160,7 +162,8 @@ public void findByValue_searchValue() throws Exception {
String metadataQualifier = titleMetadataValue.getMetadataField().getQualifier();
String searchValue = titleMetadataValue.getValue();

getClient().perform(get(SEARCH_BYVALUE_ENDPOINT)
String token = getAuthToken(eperson.getEmail(), password);
getClient(token).perform(get(SEARCH_BYVALUE_ENDPOINT)
.param("schema", metadataSchema)
.param("element", metadataElement)
.param("qualifier", metadataQualifier)
Expand Down Expand Up @@ -197,7 +200,8 @@ public void findByValue_searchValueWithStringAndNumber() throws Exception {
String metadataQualifier = titleMetadataValue.getMetadataField().getQualifier();
String searchValue = titleMetadataValue.getValue();

getClient().perform(get(SEARCH_BYVALUE_ENDPOINT)
String token = getAuthToken(eperson.getEmail(), password);
getClient(token).perform(get(SEARCH_BYVALUE_ENDPOINT)
.param("schema", metadataSchema)
.param("element", metadataElement)
.param("qualifier", metadataQualifier)
Expand Down Expand Up @@ -234,7 +238,8 @@ public void findByValue_searchValueIsNumber() throws Exception {
String metadataQualifier = titleMetadataValue.getMetadataField().getQualifier();
String searchValue = titleMetadataValue.getValue();

getClient().perform(get(SEARCH_BYVALUE_ENDPOINT)
String token = getAuthToken(eperson.getEmail(), password);
getClient(token).perform(get(SEARCH_BYVALUE_ENDPOINT)
.param("schema", metadataSchema)
.param("element", metadataElement)
.param("qualifier", metadataQualifier)
Expand All @@ -260,7 +265,8 @@ public void shouldReturnDistinctSuggestion() throws Exception {
String metadataQualifier = titleMetadataValue.getMetadataField().getQualifier();
String searchValue = titleMetadataValue.getValue();

getClient().perform(get(SEARCH_BYVALUE_ENDPOINT)
String token = getAuthToken(eperson.getEmail(), password);
getClient(token).perform(get(SEARCH_BYVALUE_ENDPOINT)
.param("schema", metadataSchema)
.param("element",metadataElement)
.param("qualifier",metadataQualifier)
Expand Down Expand Up @@ -312,7 +318,8 @@ public void shouldReturnOneSuggestionWhenInputHasMoreMetadataValues() throws Exc
String metadataQualifier = titleMetadataValue.getMetadataField().getQualifier();
String searchValue = titleMetadataValue.getValue();

getClient().perform(get(SEARCH_BYVALUE_ENDPOINT)
String token = getAuthToken(eperson.getEmail(), password);
getClient(token).perform(get(SEARCH_BYVALUE_ENDPOINT)
.param("schema", metadataSchema)
.param("element",metadataElement)
.param("qualifier",metadataQualifier)
Expand Down
Loading