DS-3489: Adding facet values to search result part 2

DS-3489: Added trace logging
This commit is contained in:
Tom Desair
2017-09-26 11:32:22 +02:00
committed by Tom Desair
parent 78d85140a9
commit 53cdcddd8e
10 changed files with 128 additions and 57 deletions

View File

@@ -7,7 +7,10 @@
*/
package org.dspace.discovery;
import org.apache.commons.collections4.ListUtils;
import org.dspace.content.DSpaceObject;
import org.dspace.discovery.configuration.DiscoveryConfigurationParameters;
import org.dspace.discovery.configuration.DiscoverySearchFilterFacet;
import java.util.*;
@@ -95,7 +98,16 @@ public class DiscoverResult {
}
public List<FacetResult> getFacetResult(String facet){
return facetResults.get(facet) == null ? new ArrayList<FacetResult>() : facetResults.get(facet);
return ListUtils.emptyIfNull(facetResults.get(facet));
}
public List<FacetResult> getFacetResult(DiscoverySearchFilterFacet field) {
List<DiscoverResult.FacetResult> facetValues = getFacetResult(field.getIndexFieldName());
//Check if we are dealing with a date, sometimes the facet values arrive as dates !
if(facetValues.size() == 0 && field.getType().equals(DiscoveryConfigurationParameters.TYPE_DATE)){
facetValues = getFacetResult(field.getIndexFieldName() + ".year");
}
return ListUtils.emptyIfNull(facetValues);
}
public DSpaceObjectHighlightResult getHighlightedResults(DSpaceObject dso)
@@ -114,13 +126,15 @@ public class DiscoverResult {
private String authorityKey;
private String sortValue;
private long count;
private String fieldType;
public FacetResult(String asFilterQuery, String displayedValue, String authorityKey, String sortValue, long count) {
public FacetResult(String asFilterQuery, String displayedValue, String authorityKey, String sortValue, long count, String fieldType) {
this.asFilterQuery = asFilterQuery;
this.displayedValue = displayedValue;
this.authorityKey = authorityKey;
this.sortValue = sortValue;
this.count = count;
this.fieldType = fieldType;
}
public String getAsFilterQuery() {
@@ -149,6 +163,10 @@ public class DiscoverResult {
{
return authorityKey != null?"authority":"equals";
}
public String getFieldType() {
return fieldType;
}
}
public String getSpellCheckQuery() {

View File

@@ -64,12 +64,7 @@ import org.apache.solr.handler.extraction.ExtractingParams;
import org.dspace.authorize.ResourcePolicy;
import org.dspace.authorize.factory.AuthorizeServiceFactory;
import org.dspace.content.Collection;
import org.dspace.content.Community;
import org.dspace.content.DSpaceObject;
import org.dspace.content.Item;
import org.dspace.content.MetadataField;
import org.dspace.content.MetadataSchema;
import org.dspace.content.MetadataValue;
import org.dspace.content.*;
import org.dspace.content.authority.Choices;
import org.dspace.content.authority.service.ChoiceAuthorityService;
import org.dspace.content.authority.service.MetadataAuthorityService;
@@ -77,23 +72,8 @@ import org.dspace.content.factory.ContentServiceFactory;
import org.dspace.content.service.CollectionService;
import org.dspace.content.service.CommunityService;
import org.dspace.content.service.ItemService;
import org.dspace.core.ConfigurationManager;
import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.core.Email;
import org.dspace.core.I18nUtil;
import org.dspace.core.LogManager;
import org.dspace.discovery.configuration.DiscoveryConfiguration;
import org.dspace.discovery.configuration.DiscoveryConfigurationParameters;
import org.dspace.discovery.configuration.DiscoveryHitHighlightFieldConfiguration;
import org.dspace.discovery.configuration.DiscoveryHitHighlightingConfiguration;
import org.dspace.discovery.configuration.DiscoveryMoreLikeThisConfiguration;
import org.dspace.discovery.configuration.DiscoveryRecentSubmissionsConfiguration;
import org.dspace.discovery.configuration.DiscoverySearchFilter;
import org.dspace.discovery.configuration.DiscoverySearchFilterFacet;
import org.dspace.discovery.configuration.DiscoverySortConfiguration;
import org.dspace.discovery.configuration.DiscoverySortFieldConfiguration;
import org.dspace.discovery.configuration.HierarchicalSidebarFacetConfiguration;
import org.dspace.core.*;
import org.dspace.discovery.configuration.*;
import org.dspace.eperson.Group;
import org.dspace.eperson.factory.EPersonServiceFactory;
import org.dspace.handle.service.HandleService;
@@ -103,6 +83,17 @@ import org.dspace.util.MultiFormatDateParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
/**
* SolrIndexer contains the methods that index Items and their metadata,
* collections, communities, etc. It is meant to either be invoked from the
@@ -1946,7 +1937,7 @@ public class SolrServiceImpl implements SearchService, IndexingService {
field,
new DiscoverResult.FacetResult(filterValue,
displayedValue, authorityValue,
sortValue, facetValue.getCount()));
sortValue, facetValue.getCount(), facetFieldConfig.getType()));
}
}
}
@@ -1979,7 +1970,7 @@ public class SolrServiceImpl implements SearchService, IndexingService {
//No need to show empty years
if (0 < count)
{
result.addFacetResult(facetField, new DiscoverResult.FacetResult(filter, name, null, name, count));
result.addFacetResult(facetField, new DiscoverResult.FacetResult(filter, name, null, name, count, DiscoveryConfigurationParameters.TYPE_DATE));
}
}
}

View File

@@ -17,6 +17,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
/**
* TODO TOM UNIT TEST
@@ -64,8 +65,9 @@ public class DiscoveryRestController implements InitializingBean {
log.trace("Searching with scope: " + StringUtils.trimToEmpty(dsoScope)
+ ", configuration name: " + StringUtils.trimToEmpty(configurationName)
+ ", dsoType: " + StringUtils.trimToEmpty(dsoType)
+ ", query: " + StringUtils.trimToEmpty(dsoType));
//TODO add filters and page info
+ ", query: " + StringUtils.trimToEmpty(dsoType)
+ ", filters: " + Objects.toString(searchFilters)
+ ", page: " + Objects.toString(page));
}
//Get the Search results in JSON format
@@ -96,8 +98,9 @@ public class DiscoveryRestController implements InitializingBean {
if(log.isTraceEnabled()) {
log.trace("Facetting on facet " + facetName + " with scope: " + StringUtils.trimToEmpty(dsoScope)
+ ", dsoType: " + StringUtils.trimToEmpty(dsoType)
+ ", query: " + StringUtils.trimToEmpty(dsoType));
//TODO add filters and page info
+ ", query: " + StringUtils.trimToEmpty(dsoType)
+ ", filters: " + Objects.toString(searchFilters)
+ ", page: " + Objects.toString(page));
}
//TODO

View File

@@ -2,6 +2,7 @@ package org.dspace.app.rest.converter;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.dspace.app.rest.model.*;
import org.dspace.app.rest.parameter.SearchFilter;
import org.dspace.authority.AuthorityValue;
@@ -11,7 +12,6 @@ import org.dspace.core.Constants;
import org.dspace.core.Context;
import org.dspace.discovery.DiscoverQuery;
import org.dspace.discovery.DiscoverResult;
import org.dspace.discovery.SearchUtils;
import org.dspace.discovery.configuration.DiscoveryConfiguration;
import org.dspace.discovery.configuration.DiscoverySearchFilterFacet;
import org.springframework.beans.factory.annotation.Autowired;
@@ -36,7 +36,7 @@ public class DiscoverResultConverter {
public SearchResultsRest convert(final Context context, final DiscoverQuery discoverQuery, final String configurationName, final String scope,
final List<SearchFilter> searchFilters, final Pageable page, final DiscoverResult searchResult) {
final List<SearchFilter> searchFilters, final Pageable page, final DiscoverResult searchResult, final DiscoveryConfiguration configuration) {
SearchResultsRest resultsRest = new SearchResultsRest();
@@ -44,26 +44,44 @@ public class DiscoverResultConverter {
addSearchResults(searchResult, resultsRest);
addFacetValues(searchResult, resultsRest);
addFacetValues(searchResult, resultsRest, configuration);
resultsRest.setTotalNumberOfResults(searchResult.getTotalSearchResults());
return resultsRest;
}
private void addFacetValues(final DiscoverResult searchResult, final SearchResultsRest resultsRest) {
Map<String, List<DiscoverResult.FacetResult>> facetResults = searchResult.getFacetResults();
for (Map.Entry<String, List<DiscoverResult.FacetResult>> facetValues : MapUtils.emptyIfNull(facetResults).entrySet()) {
SearchFacetEntryRest facetEntry = new SearchFacetEntryRest(facetValues.getKey());
private void addFacetValues(final DiscoverResult searchResult, final SearchResultsRest resultsRest, final DiscoveryConfiguration configuration) {
for (DiscoverResult.FacetResult value : CollectionUtils.emptyIfNull(facetValues.getValue())) {
SearchFacetValueRest valueRest = new SearchFacetValueRest();
valueRest.setValue(value.getDisplayedValue());
valueRest.setAuthorityKey(value.getAuthorityKey());
valueRest.setSortValue(value.getSortValue());
valueRest.setCount(value.getCount());
List<DiscoverySearchFilterFacet> facets = configuration.getSidebarFacets();
for (DiscoverySearchFilterFacet field : CollectionUtils.emptyIfNull(facets)) {
List<DiscoverResult.FacetResult> facetValues = searchResult.getFacetResult(field);
facetEntry.addValue(valueRest);
SearchFacetEntryRest facetEntry = new SearchFacetEntryRest(field.getIndexFieldName());
int valueCount = 0;
for (DiscoverResult.FacetResult value : CollectionUtils.emptyIfNull(facetValues)) {
//The discover results contains max facetLimit + 1 values. If we reach the "+1", indicate that there are
//more results available.
if(valueCount < field.getFacetLimit()) {
SearchFacetValueRest valueRest = new SearchFacetValueRest();
valueRest.setLabel(value.getDisplayedValue());
valueRest.setFilterValue(value.getAsFilterQuery());
valueRest.setFilterType(value.getFilterType());
valueRest.setAuthorityKey(value.getAuthorityKey());
valueRest.setSortValue(value.getSortValue());
valueRest.setCount(value.getCount());
facetEntry.addValue(valueRest);
} else {
facetEntry.setHasMore(true);
}
if(StringUtils.isBlank(facetEntry.getFacetType())) {
facetEntry.setFacetType(value.getFieldType());
}
valueCount++;
}
resultsRest.addFacetEntry(facetEntry);

View File

@@ -1,7 +1,6 @@
package org.dspace.app.rest.model;
import com.fasterxml.jackson.annotation.JsonIgnore;
import org.apache.commons.lang.StringUtils;
import org.dspace.app.rest.DiscoveryRestController;
import java.util.LinkedList;
@@ -16,12 +15,14 @@ public class SearchFacetEntryRest implements RestModel {
public static final String CATEGORY = RestModel.DISCOVER;
private String name;
private String facetType;
private boolean hasMore = false;
@JsonIgnore
private List<SearchFacetValueRest> values;
public SearchFacetEntryRest(final String name) {
this.name = StringUtils.substringBefore(name, ".year");
this.name = name;
}
public String getCategory() {
@@ -56,4 +57,20 @@ public class SearchFacetEntryRest implements RestModel {
public List<SearchFacetValueRest> getValues() {
return values;
}
public String getFacetType() {
return facetType;
}
public void setFacetType(final String facetType) {
this.facetType = facetType;
}
public void setHasMore(final boolean hasMore) {
this.hasMore = hasMore;
}
public boolean isHasMore() {
return hasMore;
}
}

View File

@@ -10,10 +10,12 @@ public class SearchFacetValueRest implements RestModel {
public static final String NAME = "discover";
public static final String CATEGORY = RestModel.DISCOVER;
private String value;
private String label;
private String filterValue;
private long count;
private String authorityKey;
private String sortValue;
private String filterType;
public String getCategory() {
return CATEGORY;
@@ -27,12 +29,20 @@ public class SearchFacetValueRest implements RestModel {
return DiscoveryRestController.class;
}
public String getValue() {
return value;
public String getLabel() {
return label;
}
public void setValue(final String value) {
this.value = value;
public void setLabel(final String label) {
this.label = label;
}
public String getFilterValue() {
return filterValue;
}
public void setFilterValue(final String filterValue) {
this.filterValue = filterValue;
}
public long getCount() {
@@ -58,4 +68,13 @@ public class SearchFacetValueRest implements RestModel {
public String getSortValue() {
return sortValue;
}
public String getFilterType() {
return filterType;
}
public void setFilterType(final String filterType) {
this.filterType = filterType;
}
}

View File

@@ -14,7 +14,6 @@ public abstract class HALResource extends ResourceSupport {
protected final Map<String, Object> embedded = new HashMap<String, Object>();
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("_embedded")
public Map<String, Object> getEmbeddedResources() {

View File

@@ -47,9 +47,7 @@ public class SearchFacetValueResource extends HALResource {
}
private void addFilterForFacetValue(final UriComponentsBuilder baseLink) {
//TODO Date filters have a special format!
//TODO check that if we have an authority value, we use that one
baseLink.queryParam("f." + facetData.getName(), data.getValue() + ",equals");
baseLink.queryParam("f." + facetData.getName(), data.getFilterValue() + "," + data.getFilterType());
}
private UriComponentsBuilder buildBaseLink() {

View File

@@ -33,4 +33,12 @@ public class SearchFilter {
public boolean hasAuthorityOperator() {
return StringUtils.equals(operator, "authority");
}
public String toString() {
return "SearchFilter{" +
"name='" + name + '\'' +
", operator='" + operator + '\'' +
", value='" + value + '\'' +
'}';
}
}

View File

@@ -81,7 +81,7 @@ public class DiscoveryRestRepository extends AbstractDSpaceRestRepository {
//TODO TOM handle search exception
}
return discoverResultConverter.convert(context, discoverQuery, configurationName, dsoScope, searchFilters, page, searchResult);
return discoverResultConverter.convert(context, discoverQuery, configurationName, dsoScope, searchFilters, page, searchResult, configuration);
}
public void getFacetsConfiguration(final String dsoScope, final String configurationName) {