Skip to content

Commit

Permalink
Created :rank and :direction search ontology properties, SortDirectio…
Browse files Browse the repository at this point in the history
…n class
  • Loading branch information
litvinovg committed Jan 9, 2025
1 parent 2d6403c commit 0048035
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 92 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,10 @@ public class FilterValue {

private String id;
private String name = "";
private int order;

private int rank;
private long count;

private boolean selected = false;

private boolean isDefaultValue;

private boolean displayed = false;

public boolean isDisplayed() {
Expand Down Expand Up @@ -46,13 +42,13 @@ public void setName(RDFNode rdfNode) {
}
}

public Integer getOrder() {
return order;
public Integer getRank() {
return rank;
}

public void setOrder(RDFNode rdfNode) {
public void setRank(RDFNode rdfNode) {
if (rdfNode != null) {
order = rdfNode.asLiteral().getInt();
rank = rdfNode.asLiteral().getInt();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,7 @@ private static void addSortField(VitroRequest vreq, SearchQuery query, SortConfi
log.error(String.format("Sort field is not set for '%s'", conf.getId()));
return;
}
query.addSortField(field, conf.getSortOrder());
query.addSortField(field, conf.getSortDirection());
if (sortOptions.containsKey(conf.getFallback())) {
addSortField(vreq, query, sortOptions.get(conf.getFallback()), sortOptions, appliedSortOptions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.util.Optional;
import java.util.stream.Collectors;

import edu.cornell.mannlib.vitro.webapp.modules.searchEngine.SearchQuery.Order;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand Down Expand Up @@ -46,7 +47,7 @@ private enum SortOption {
private String min = "0";
private String max = "2000";
private int moreLimit = 30;
private int order = 0;
private int rank = 0;
private String field = "";
private String endField = "";
private String inputText = "";
Expand All @@ -57,14 +58,14 @@ private enum SortOption {
private Map<String, FilterValue> values = new LinkedHashMap<>();
private boolean inputRegex = false;
private boolean facetsRequired;
private boolean descendingOrder;
private Order valueSortDirection = Order.ASC;
private String type = FILTER;
private String rangeText = "";
private String rangeInput = "";
private boolean displayed = false;
private Optional<Locale> locale;
private boolean multilingual;
private SortOption sortingObjectType = SortOption.labelText;
private SortOption sortOption = SortOption.labelText;

public String getRangeInput() {
return rangeInput;
Expand Down Expand Up @@ -93,14 +94,14 @@ public void setName(RDFNode rdfNode) {
}
}

public void setOrder(RDFNode rdfNode) {
public void setRank(RDFNode rdfNode) {
if (rdfNode != null) {
order = rdfNode.asLiteral().getInt();
rank = rdfNode.asLiteral().getInt();
}
}

public Integer getOrder() {
return order;
public Integer getRank() {
return rank;
}

public String getField() {
Expand Down Expand Up @@ -312,10 +313,10 @@ public int compare(Entry<String, FilterValue> obj1, Entry<String, FilterValue> o
FilterValue first = obj1.getValue();
FilterValue second = obj2.getValue();
// sort by order first
int result = first.getOrder().compareTo(second.getOrder());
int result = first.getRank().compareTo(second.getRank());
if (result == 0) {
result = compareByObjectType(first, second);
if (descendingOrder) {
if (valueSortDirection.equals(Order.DESC)) {
result = -result;
}
}
Expand All @@ -324,7 +325,7 @@ public int compare(Entry<String, FilterValue> obj1, Entry<String, FilterValue> o

private int compareByObjectType(FilterValue first, FilterValue second) {
int result;
switch (sortingObjectType) {
switch (sortOption) {
case hitsCount:
result = Long.compare(first.getCount(), second.getCount());
break;
Expand Down Expand Up @@ -410,15 +411,19 @@ public void setMulitlingual(boolean multilingual) {
this.multilingual = multilingual;
}

public void setDescendingValuesOrder(boolean descendingOrder) {
this.descendingOrder = descendingOrder;
public void setValueSortDirection(boolean isDescending) {
if (isDescending) {
this.valueSortDirection = Order.DESC;
} else {
this.valueSortDirection = Order.ASC;
}
}

public void setSortingObjectType(String uri) {
public void setSortOption(String uri) {
if (uri.startsWith(VITRO_SEARCH_INDIVIDUAL)) {
String optionName = uri.substring(VITRO_SEARCH_INDIVIDUAL.length());
try {
sortingObjectType = SortOption.valueOf(optionName);
sortOption = SortOption.valueOf(optionName);
} catch (Exception e) {
log.error(e, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@ public class SearchFiltering {

private static final String FILTER_QUERY = ""
+ "PREFIX search: <https://vivoweb.org/ontology/vitro-search#>\n"
+ "PREFIX search-ind:<https://vivoweb.org/ontology/vitro-search-individual/>\n"
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
+ "SELECT ?filter_id ?filter_type ?filter_label ?value_label ?value_id ?field_name ?public ?filter_order "
+ "?value_order (STR(?isUriReq) as ?isUri ) ?multivalued ?input ?regex ?facet ?min ?max ?role "
+ "SELECT ?filter_id ?filter_type ?filter_label ?value_label ?value_id ?field_name ?public ?filter_rank "
+ "?value_rank (STR(?isUriReq) as ?isUri ) ?multivalued ?input ?regex ?facet ?min ?max ?role "
+ "?value_public ?more_limit ?multilingual ?isDescending\n"
+ "?filterDisplayLimitRole ?valueDisplayLimitRole ?sortingObjectType \n"
+ "WHERE {\n"
Expand All @@ -70,8 +71,8 @@ public class SearchFiltering {
+ " ?value rdfs:label ?value_label .\n"
+ " ?value search:id ?value_id .\n"
+ " OPTIONAL {"
+ " ?value search:order ?v_order .\n"
+ " bind(?v_order as ?value_order_found).\n"
+ " ?value search:order|search:rank ?v_rank .\n"
+ " bind(?v_rank as ?value_rank_found).\n"
+ " }\n"
+ " OPTIONAL {\n"
+ " ?value search:isDefaultForRole ?role .\n"
Expand All @@ -90,42 +91,47 @@ public class SearchFiltering {
+ " OPTIONAL {?filter search:userInput ?input }\n"
+ " OPTIONAL {?filter search:userInputRegex ?regex }\n"
+ " OPTIONAL {?filter search:facetResults ?facet }\n"
+ " OPTIONAL {?filter search:reverseFacetOrder ?isDescending }\n"
+ " OPTIONAL {?filter search:reverseFacetOrder ?isDescendingDeprecated }\n"
+ " OPTIONAL {"
+ " ?filter search:direction search-ind:descending ."
+ " BIND(true as ?isDescendingNew)"
+ " }\n"
+ " OPTIONAL {?filter search:sortValuesBy ?sortingObjectType }\n"
+ " OPTIONAL {?filter search:from ?min }\n"
+ " OPTIONAL {?filter search:public ?public }\n"
+ " OPTIONAL {?filter search:to ?max }\n"
+ " OPTIONAL {?filter search:moreLimit ?more_limit }\n"
+ " OPTIONAL {?filter search:limitDisplayTo ?filterDisplayLimitRole . }\n"
+ " OPTIONAL {\n"
+ " ?filter search:order ?f_order \n"
+ " bind(?f_order as ?filter_order_found).\n"
+ " ?filter search:order|search:rank ?f_rank \n"
+ " BIND(?f_rank as ?filter_rank_found).\n"
+ " }\n"
+ " BIND(coalesce(?filter_order_found, 0) as ?filter_order)\n"
+ " BIND(coalesce(?value_order_found, 0) as ?value_order)\n"
+ " BIND(COALESCE(?filter_rank_found, 0) as ?filter_rank)\n"
+ " BIND(COALESCE(?value_rank_found, 0) as ?value_rank)\n"
+ " BIND(COALESCE(?bind_multilingual, false) as ?multilingual)\n"
+ "} ORDER BY ?filter_id ?filter_order ?value_order";
+ " BIND(COALESCE(?isDescendingNew, ?isDescendingDeprecated, false) as ?isDescending)\n"
+ "} ORDER BY ?filter_id ?filter_rank ?value_rank";

private static final String FILTER_GROUPS_QUERY = ""
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
+ "PREFIX search: <https://vivoweb.org/ontology/vitro-search#>\n"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
+ "SELECT ?group_id (STR(?group_l) AS ?group_label) ?filter_id ?order ?filter_order ?public\n"
+ "SELECT ?group_id (STR(?group_l) AS ?group_label) ?filter_id ?rank ?filter_rank ?public\n"
+ "?groupDisplayLimitRole\n"
+ "WHERE {\n"
+ " ?filter_group rdf:type search:FilterGroup .\n"
+ " ?filter_group search:contains ?filter .\n"
+ " ?filter_group rdfs:label ?group_l .\n"
+ " ?filter_group search:id ?group_id .\n"
+ " OPTIONAL { ?filter_group search:order ?order .}\n"
+ " OPTIONAL {?filter_group search:order|search:rank ?rank .}\n"
+ " ?filter search:id ?filter_id .\n"
+ " OPTIONAL {?filter_group search:public ?public }\n"
+ " OPTIONAL {?filter search:order ?f_order .\n"
+ " BIND(?f_order as ?filter_order_found).\n"
+ " OPTIONAL {?filter search:order|search:rank ?f_rank .\n"
+ " BIND(?f_rank as ?filter_rank_found).\n"
+ " }\n"
+ " OPTIONAL {?filter_group search:limitDisplayTo ?groupDisplayLimitRole .}\n"
+ " BIND(coalesce(?filter_order_found, 0) as ?filter_order)\n"
+ "} ORDER BY ?order ?group_label ?filter_order";
+ " BIND(COALESCE(?filter_rank_found, 0) as ?filter_rank)\n"
+ "} ORDER BY ?rank ?group_label ?filter_rank";

private static final String LABEL_QUERY = ""
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
Expand All @@ -138,8 +144,9 @@ public class SearchFiltering {
+ "PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n"
+ "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>\n"
+ "PREFIX search: <https://vivoweb.org/ontology/vitro-search#> \n"
+ "PREFIX search-ind:<https://vivoweb.org/ontology/vitro-search-individual/>\n"
+ "SELECT ( STR(?sort_label) as ?label ) ?id ?searchField "
+ "?multilingual ?isAsc ?sort_order ?fallback ?display ?sortDisplayLimitRole\n"
+ "?multilingual ?isAscending ?sort_rank ?fallback ?display ?sortDisplayLimitRole\n"
+ "WHERE {\n"
+ " ?sort rdf:type search:Sort . \n"
+ " ?sort rdfs:label ?sort_label .\n"
Expand All @@ -155,22 +162,25 @@ public class SearchFiltering {
+ " ?sort search:id ?id .\n"
+ " }\n"
+ " OPTIONAL {\n"
+ " ?sort search:isAscending ?f_ord .\n"
+ " BIND(?f_ord as ?f_order) .\n"
+ " ?sort search:isAscending ?isAscendingDeprecated .\n"
+ " }\n"
+ " OPTIONAL {"
+ " ?sort search:direction search-ind:ascending ."
+ " BIND(true as ?isAscendingNew)"
+ " }\n"
+ " OPTIONAL {\n"
+ " ?sort search:hasFallback/search:id ?fallback .\n"
+ " }\n"
+ " OPTIONAL{ "
+ " ?sort search:order ?s_order .\n"
+ " BIND(?s_order as ?sort_order_found).\n"
+ " ?sort search:order|search:rank ?s_rank .\n"
+ " BIND(?s_rank as ?sort_rank_found).\n"
+ " }\n"
+ " OPTIONAL {?sort search:display ?display }\n"
+ " OPTIONAL {?sort search:limitDisplayTo ?sortDisplayLimitRole .}\n"
+ " BIND(coalesce(?sort_order_found, 0) as ?sort_order)\n"
+ " BIND(COALESCE(?f_order, false) as ?isAsc)\n"
+ " BIND(coalesce(?sort_rank_found, 0) as ?sort_rank)\n"
+ " BIND(COALESCE(?isAscendingNew, ?isAscendingDeprecated, false) as ?isAscending)\n"
+ " BIND(COALESCE(?bind_multilingual, false) as ?multilingual)\n"
+ "} ORDER BY ?sort_order ?label ";
+ "} ORDER BY ?sort_rank ?label ";

protected static void addFiltersToQuery(SearchQuery query, Map<String, SearchFilter> filters) {
for (SearchFilter searchFilter : filters.values()) {
Expand Down Expand Up @@ -288,7 +298,7 @@ public static Map<String, SearchFilter> readFilterConfigurations(Set<String> cur
if (!filter.contains(valueId)) {
value = new FilterValue(valueId);
value.setName(solution.get("value_label"));
value.setOrder(solution.get("value_order"));
value.setRank(solution.get("value_rank"));
filter.addValue(value);
}
value = filter.getValue(valueId);
Expand Down Expand Up @@ -372,9 +382,9 @@ public static class FilterComparator implements Comparator<Map.Entry<String, Sea
public int compare(Entry<String, SearchFilter> obj1, Entry<String, SearchFilter> obj2) {
SearchFilter filter1 = obj1.getValue();
SearchFilter filter2 = obj2.getValue();
int result = filter1.getOrder().compareTo(filter2.getOrder());
int result = filter1.getRank().compareTo(filter2.getRank());
if (result == 0) {
// order are equal, sort by name
// ranks are equal, sort by name
return filter1.getName().toLowerCase().compareTo(filter2.getName().toLowerCase());
} else {
return result;
Expand Down Expand Up @@ -445,17 +455,17 @@ public static Map<String, SortConfiguration> getSortConfigurations(VitroRequest
if (multilingual != null) {
config.setMultilingual(multilingual.asLiteral().getBoolean());
}
RDFNode isAsc = solution.get("isAsc");
if (isAsc != null) {
config.setAscOrder(isAsc.asLiteral().getBoolean());
RDFNode isAscending = solution.get("isAscending");
if (isAscending != null) {
config.setSortDirection(isAscending.asLiteral().getBoolean());
}
RDFNode fallback = solution.get("fallback");
if (fallback != null && fallback.isLiteral()) {
config.setFallback(fallback.asLiteral().toString());
}
RDFNode order = solution.get("sort_order");
if (order != null) {
config.setOrder(order.asLiteral().getInt());
RDFNode rank = solution.get("sort_rank");
if (rank != null) {
config.setRank(rank.asLiteral().getInt());
}
RDFNode display = solution.get("display");
if (display != null) {
Expand All @@ -476,7 +486,7 @@ private static SearchFilter createSearchFilter(Map<String, SearchFilter> filters
filter = new SearchFilter(resultFilterId, locale);
filtersByField.put(resultFieldName, filter);
filter.setName(solution.get("filter_label"));
filter.setOrder(solution.get("filter_order"));
filter.setRank(solution.get("filter_rank"));
filter.setType(solution.get("filter_type"));
if (solution.get("isUri") != null && "true".equals(solution.get("isUri").toString())) {
filter.setLocalizationRequired(true);
Expand Down Expand Up @@ -509,12 +519,12 @@ private static SearchFilter createSearchFilter(Map<String, SearchFilter> filters

RDFNode descendingOrder = solution.get("isDescending");
if (descendingOrder != null && descendingOrder.isLiteral()) {
filter.setDescendingValuesOrder(descendingOrder.asLiteral().getBoolean());
filter.setValueSortDirection(descendingOrder.asLiteral().getBoolean());
}

RDFNode sortingObject = solution.get("sortingObjectType");
if (sortingObject != null && sortingObject.isURIResource()) {
filter.setSortingObjectType(sortingObject.asResource().getURI());
RDFNode sortOption = solution.get("sortingObjectType");
if (sortOption != null && sortOption.isURIResource()) {
filter.setSortOption(sortOption.asResource().getURI());
}

RDFNode moreLimit = solution.get("more_limit");
Expand Down
Loading

0 comments on commit 0048035

Please sign in to comment.