mirror of
https://github.com/DSpace/DSpace.git
synced 2025-10-07 01:54:22 +00:00
Working on the min-max facet implementation
This commit is contained in:
@@ -19,11 +19,13 @@ import java.text.SimpleDateFormat;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@@ -972,7 +974,15 @@ public class SolrServiceImpl implements SearchService, IndexingService {
|
||||
Set<String> moreLikeThisFields = new HashSet<String>();
|
||||
for (DiscoveryConfiguration discoveryConfiguration : discoveryConfigurations) {
|
||||
for (int i = 0; i < discoveryConfiguration.getSearchFilters().size(); i++) {
|
||||
|
||||
List<MetadataValue> metadataValueList = new LinkedList<>();
|
||||
boolean shouldExposeMinMax = false;
|
||||
DiscoverySearchFilter discoverySearchFilter = discoveryConfiguration.getSearchFilters().get(i);
|
||||
if (StringUtils.equalsIgnoreCase(discoverySearchFilter.getFilterType(), "facet")) {
|
||||
if (((DiscoverySearchFilterFacet) discoverySearchFilter).isExposeMinMax()) {
|
||||
shouldExposeMinMax = true;
|
||||
}
|
||||
}
|
||||
for (int j = 0; j < discoverySearchFilter.getMetadataFields().size(); j++) {
|
||||
String metadataField = discoverySearchFilter.getMetadataFields().get(j);
|
||||
List<DiscoverySearchFilter> resultingList;
|
||||
@@ -982,10 +992,39 @@ public class SolrServiceImpl implements SearchService, IndexingService {
|
||||
//New metadata field, create a new list for it
|
||||
resultingList = new ArrayList<DiscoverySearchFilter>();
|
||||
}
|
||||
|
||||
|
||||
if (shouldExposeMinMax) {
|
||||
String[] splittedMetadataField = metadataField.split("\\.");
|
||||
String schema = splittedMetadataField[0];
|
||||
String element = splittedMetadataField.length > 1 ? splittedMetadataField[1] : null;
|
||||
String qualifier = splittedMetadataField.length > 2 ? splittedMetadataField[2] : null;
|
||||
|
||||
metadataValueList.addAll(itemService.getMetadata(item, schema,
|
||||
element, qualifier, Item.ANY));
|
||||
|
||||
}
|
||||
|
||||
resultingList.add(discoverySearchFilter);
|
||||
|
||||
searchFilters.put(metadataField, resultingList);
|
||||
}
|
||||
|
||||
if (!metadataValueList.isEmpty() && shouldExposeMinMax) {
|
||||
metadataValueList.sort(new Comparator<MetadataValue>() {
|
||||
public int compare(MetadataValue mdv1,MetadataValue mdv2) {
|
||||
return mdv1.getValue().compareTo(mdv2.getValue()) ;
|
||||
}
|
||||
});
|
||||
MetadataValue firstMetadataValue = metadataValueList.get(0);
|
||||
MetadataValue lastMetadataValue = metadataValueList.get(metadataValueList.size() - 1);
|
||||
|
||||
doc.addField(discoverySearchFilter.getIndexFieldName() + "_min", firstMetadataValue.getValue());
|
||||
doc.addField(discoverySearchFilter.getIndexFieldName() + "_min_sort", firstMetadataValue.getValue());
|
||||
doc.addField(discoverySearchFilter.getIndexFieldName() + "_max", lastMetadataValue.getValue());
|
||||
doc.addField(discoverySearchFilter.getIndexFieldName() + "_max_sort", lastMetadataValue.getValue());
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
DiscoverySortConfiguration sortConfiguration = discoveryConfiguration.getSearchSortConfiguration();
|
||||
@@ -1244,6 +1283,7 @@ public class SolrServiceImpl implements SearchService, IndexingService {
|
||||
doc.addField(indexField + "_sort", yearUTC);
|
||||
}
|
||||
}
|
||||
|
||||
} else if (searchFilter.getType()
|
||||
.equals(DiscoveryConfigurationParameters.TYPE_HIERARCHICAL)) {
|
||||
HierarchicalSidebarFacetConfiguration hierarchicalSidebarFacetConfiguration =
|
||||
|
@@ -21,7 +21,7 @@ public class DiscoverySearchFilterFacet extends DiscoverySearchFilter {
|
||||
private DiscoveryConfigurationParameters.SORT sortOrderSidebar = DiscoveryConfigurationParameters.SORT.COUNT;
|
||||
private DiscoveryConfigurationParameters.SORT sortOrderFilterPage = DiscoveryConfigurationParameters.SORT.COUNT;
|
||||
public static final String FILTER_TYPE_FACET = "facet";
|
||||
|
||||
private boolean exposeMinMax = false;
|
||||
|
||||
public int getFacetLimit() {
|
||||
if (facetLimit == -1) {
|
||||
@@ -55,4 +55,12 @@ public class DiscoverySearchFilterFacet extends DiscoverySearchFilter {
|
||||
public String getFilterType() {
|
||||
return FILTER_TYPE_FACET;
|
||||
}
|
||||
|
||||
public boolean isExposeMinMax() {
|
||||
return exposeMinMax;
|
||||
}
|
||||
|
||||
public void setExposeMinMax(boolean exposeMinMax) {
|
||||
this.exposeMinMax = exposeMinMax;
|
||||
}
|
||||
}
|
||||
|
@@ -16,9 +16,13 @@ import org.dspace.app.rest.model.SearchFacetValueRest;
|
||||
import org.dspace.app.rest.model.SearchResultsRest;
|
||||
import org.dspace.app.rest.parameter.SearchFilter;
|
||||
import org.dspace.core.Context;
|
||||
import org.dspace.discovery.DiscoverQuery;
|
||||
import org.dspace.discovery.DiscoverResult;
|
||||
import org.dspace.discovery.SearchService;
|
||||
import org.dspace.discovery.SearchServiceException;
|
||||
import org.dspace.discovery.configuration.DiscoveryConfiguration;
|
||||
import org.dspace.discovery.configuration.DiscoverySearchFilterFacet;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@@ -27,6 +31,8 @@ public class DiscoverFacetsConverter {
|
||||
|
||||
private DiscoverFacetValueConverter facetValueConverter = new DiscoverFacetValueConverter();
|
||||
|
||||
@Autowired
|
||||
private SearchService searchService;
|
||||
|
||||
public SearchResultsRest convert(Context context, String query, String dsoType, String configurationName,
|
||||
String dsoScope, List<SearchFilter> searchFilters, final Pageable page,
|
||||
@@ -36,13 +42,14 @@ public class DiscoverFacetsConverter {
|
||||
|
||||
setRequestInformation(context, query, dsoType, configurationName, dsoScope, searchFilters, page,
|
||||
searchResultsRest);
|
||||
addFacetValues(searchResult, searchResultsRest, configuration);
|
||||
addFacetValues(context, searchResult, searchResultsRest, configuration);
|
||||
|
||||
return searchResultsRest;
|
||||
}
|
||||
|
||||
|
||||
private void addFacetValues(final DiscoverResult searchResult, final SearchResultsRest searchResultsRest,
|
||||
private void addFacetValues(Context context, final DiscoverResult searchResult,
|
||||
final SearchResultsRest searchResultsRest,
|
||||
final DiscoveryConfiguration configuration) {
|
||||
|
||||
List<DiscoverySearchFilterFacet> facets = configuration.getSidebarFacets();
|
||||
@@ -53,7 +60,14 @@ public class DiscoverFacetsConverter {
|
||||
SearchFacetEntryRest facetEntry = new SearchFacetEntryRest(field.getIndexFieldName());
|
||||
int valueCount = 0;
|
||||
facetEntry.setFacetLimit(field.getFacetLimit());
|
||||
|
||||
facetEntry.setExposeMinMax(field.isExposeMinMax());
|
||||
if (field.isExposeMinMax()) {
|
||||
try {
|
||||
calculateMinMaxValues(context, facetEntry, field);
|
||||
} catch (SearchServiceException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
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.
|
||||
@@ -75,6 +89,57 @@ public class DiscoverFacetsConverter {
|
||||
}
|
||||
}
|
||||
|
||||
private void calculateMinMaxValues(Context context,SearchFacetEntryRest facetEntry,DiscoverySearchFilterFacet field)
|
||||
throws SearchServiceException {
|
||||
|
||||
//TODO Move to searchService, only call to retrieve values (split logic)
|
||||
int oldestYear = 0;
|
||||
int newestYear = 0;
|
||||
|
||||
DiscoverQuery minQuery = new DiscoverQuery();
|
||||
minQuery.setMaxResults(1);
|
||||
//Set our query to anything that has this value
|
||||
String indexFieldName = field.getIndexFieldName();
|
||||
minQuery.addFieldPresentQueries(indexFieldName + "_min");
|
||||
//Set sorting so our last value will appear on top
|
||||
minQuery.setSortField(indexFieldName + "_min_sort",DiscoverQuery.SORT_ORDER.asc);
|
||||
minQuery.addSearchField(indexFieldName + "_min");
|
||||
DiscoverResult minResult = searchService.search(context, minQuery);
|
||||
|
||||
if (0 < minResult.getDspaceObjects().size()) {
|
||||
List<DiscoverResult.SearchDocument> searchDocuments = minResult
|
||||
.getSearchDocument(minResult.getDspaceObjects().get(0));
|
||||
if (0 < searchDocuments.size() && 0 < searchDocuments.get(0).getSearchFieldValues
|
||||
(indexFieldName).size()) {
|
||||
// oldestYear = Integer.parseInt(searchDocuments.get(0)
|
||||
// .getSearchFieldValues(indexFieldName).get(0));
|
||||
}
|
||||
}
|
||||
//Now get the first year
|
||||
|
||||
DiscoverQuery maxQuery = new DiscoverQuery();
|
||||
maxQuery.setMaxResults(1);
|
||||
//Set our query to anything that has this value
|
||||
indexFieldName = field.getIndexFieldName();
|
||||
maxQuery.addFieldPresentQueries(indexFieldName + "_max");
|
||||
//Set sorting so our last value will appear on top
|
||||
maxQuery.setSortField(indexFieldName + "_max_sort",DiscoverQuery.SORT_ORDER.desc);
|
||||
maxQuery.addSearchField(indexFieldName + "_max");
|
||||
DiscoverResult maxResult = searchService.search(context, maxQuery);
|
||||
if (0 < maxResult.getDspaceObjects().size()) {
|
||||
List<DiscoverResult.SearchDocument> searchDocuments = maxResult
|
||||
.getSearchDocument(maxResult.getDspaceObjects().get(0));
|
||||
if (0 < searchDocuments.size() && 0 < searchDocuments.get(0).getSearchFieldValues
|
||||
(indexFieldName).size()) {
|
||||
// newestYear = Integer.parseInt(searchDocuments.get(0).getSearchFieldValues
|
||||
// (indexFieldName).get(0));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
private void setRequestInformation(final Context context, final String query, final String dsoType,
|
||||
final String configurationName, final String scope,
|
||||
final List<SearchFilter> searchFilters, final Pageable page,
|
||||
|
@@ -70,6 +70,7 @@ public class DiscoverResultConverter {
|
||||
facetEntry.setHasMore(false);
|
||||
facetEntry.setFacetLimit(field.getFacetLimit());
|
||||
|
||||
facetEntry.setExposeMinMax(field.isExposeMinMax());
|
||||
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.
|
||||
|
@@ -28,6 +28,9 @@ public class SearchFacetEntryRest implements RestAddressableModel {
|
||||
private Boolean hasMore = null;
|
||||
private int facetLimit;
|
||||
|
||||
@JsonIgnore
|
||||
private boolean exposeMinMax = false;
|
||||
|
||||
@JsonIgnore
|
||||
private List<SearchFacetValueRest> values = new LinkedList<>();
|
||||
|
||||
@@ -87,4 +90,12 @@ public class SearchFacetEntryRest implements RestAddressableModel {
|
||||
public void setFacetLimit(final int facetLimit) {
|
||||
this.facetLimit = facetLimit;
|
||||
}
|
||||
|
||||
public boolean isExposeMinMax() {
|
||||
return exposeMinMax;
|
||||
}
|
||||
|
||||
public void setExposeMinMax(boolean exposeMinMax) {
|
||||
this.exposeMinMax = exposeMinMax;
|
||||
}
|
||||
}
|
||||
|
@@ -378,6 +378,7 @@
|
||||
<property name="sortOrderSidebar" value="COUNT"/>
|
||||
<property name="sortOrderFilterPage" value="COUNT"/>
|
||||
<property name="isOpenByDefault" value="true"/>
|
||||
<property name="exposeMinMax" value="true"/>
|
||||
|
||||
</bean>
|
||||
|
||||
@@ -407,6 +408,7 @@
|
||||
<property name="sortOrderSidebar" value="COUNT"/>
|
||||
<property name="sortOrderFilterPage" value="COUNT"/>
|
||||
<property name="isOpenByDefault" value="false"/>
|
||||
<property name="exposeMinMax" value="true"/>
|
||||
|
||||
</bean>
|
||||
|
||||
|
@@ -578,6 +578,8 @@
|
||||
<dynamicField name="*_hl" type="text" indexed="true" stored="true" multiValued="true" omitNorms="true"/>
|
||||
|
||||
<dynamicField name="*_sort" type="lowerCaseSort" indexed="true" stored="true" multiValued="false" omitNorms="true"/>
|
||||
<dynamicField name="*_min" type="text" indexed="true" stored="true" multiValued="false" omitNorms="true"/>
|
||||
<dynamicField name="*_max" type="text" indexed="true" stored="true" multiValued="false" omitNorms="true"/>
|
||||
|
||||
<!--Dynamic field used for related item searches-->
|
||||
<dynamicField name="*_mlt" type="text" indexed="true" stored="true" multiValued="true" omitNorms="true" termVectors="true" termPositions="true" termOffsets="true"/>
|
||||
|
Reference in New Issue
Block a user