Adjust sorting clauses for all entities

This commit is contained in:
Serafeim Chatzopoulos 2024-06-14 09:49:00 +03:00
parent f195d1c58d
commit a8a498929b
9 changed files with 63 additions and 23 deletions

View File

@ -84,7 +84,7 @@ public class ProjectRequest {
@Parameter(description = "Number of results per page", schema = @Schema(defaultValue = "10", type = "integer"))
private int pageSize = 10;
@Parameter(description = "The field to sort the results by and the sort direction. The format should be in the format `fieldname ASC|DESC`, where fieldname is one of 'relevance', 'projectstartyear', 'projectendyear'. Multiple sorting parameters should be comma-separated." , schema = @Schema(defaultValue = "relevance DESC"))
@Pattern(regexp = "^((relevance|projectstartyear|projectendyear)\\s+(ASC|DESC),?\\s*)+$", message = "The field should be in the format 'fieldname ASC|DESC', where fieldname is one of 'score', 'projectstartyear', 'projectendyear'. Multiple sorting parameters should be comma-separated.")
@Parameter(description = "The field to sort the results by and the sort direction. The format should be in the format `fieldname ASC|DESC`, where fieldname is one of 'relevance', 'startDate', 'endDate'. Multiple sorting parameters should be comma-separated." , schema = @Schema(defaultValue = "relevance DESC"))
@Pattern(regexp = "^((relevance|startDate|endDate)\\s+(ASC|DESC),?\\s*)+$", message = "The field should be in the format 'fieldname ASC|DESC', where fieldname is one of 'relevance', 'startDate', 'endDate'. Multiple sorting parameters should be comma-separated.")
private String sortBy;
}

View File

@ -145,7 +145,7 @@ public class ResearchProductsRequest {
@Parameter(description = "Number of results per page", schema = @Schema(defaultValue = "10", type = "integer"))
private int pageSize = 10;
@Parameter(description = "The field to sort the results by and the sort direction. The format should be in the format `fieldname ASC|DESC`, where fieldname is one of 'score', 'dateofcollection', 'influence', 'popularity', 'citation_count', 'impulse'. Multiple sorting parameters should be comma-separated." , schema = @Schema(defaultValue = "score DESC"))
@Pattern(regexp = "^((score|dateofcollection|influence|popularity|citation_count|impulse)\\s+(ASC|DESC),?\\s*)+$", message = "The field should be in the format 'fieldname ASC|DESC', research products can be only sorted by the 'score', 'dateofcollection', 'influence', 'citation_count', 'impulse'.")
@Parameter(description = "The field to sort the results by and the sort direction. The format should be in the format `fieldname ASC|DESC`, where fieldname is one of 'relevance', 'publicationDate', 'dateOfCollection', 'influence', 'popularity', 'citationCount', 'impulse'. Multiple sorting parameters should be comma-separated." , schema = @Schema(defaultValue = "relevance DESC"))
@Pattern(regexp = "^((relevance|publicationDate|dateOfCollection|influence|popularity|citationCount|impulse)\\s+(ASC|DESC),?\\s*)+$", message = "The field should be in the format 'fieldname ASC|DESC', research products can be only sorted by the 'relevance', 'publicationDate', 'dateOfCollection', 'influence', 'citationCount', 'impulse'.")
private String sortBy;
}

View File

@ -1,6 +1,7 @@
package eu.openaire.api.mappers;
import eu.openaire.api.errors.exceptions.BadRequestException;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.util.ClientUtils;
import java.time.LocalDate;
@ -8,9 +9,7 @@ import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.*;
public class Utils {
@ -150,4 +149,27 @@ public class Utils {
}
return null;
}
static public List<SolrQuery.SortClause> formatSortByParam(String sortBy, Map<String, String> fieldMapping) {
if (Utils.isNullOrEmpty(sortBy)) {
return null;
}
List<SolrQuery.SortClause> sortClauses = new ArrayList<>();
String[] sortByPairs = sortBy.split(",");
for (String pair : sortByPairs) {
String[] parts = pair.trim().split("\\s+");
String field = parts[0];
SolrQuery.ORDER order = parts[1].equalsIgnoreCase("ASC") ? SolrQuery.ORDER.asc : SolrQuery.ORDER.desc;
String fieldName = Optional.ofNullable(fieldMapping.get(field))
.orElseThrow(() -> new BadRequestException("Invalid field name in sortBy parameter: " + field));
sortClauses.add(new SolrQuery.SortClause(fieldName, order));
}
return sortClauses;
}
}

View File

@ -11,9 +11,13 @@ import java.util.Map;
@Mapper(componentModel = "spring")
public interface DataSourceRequestMapper {
Map<String, String> sortFieldMapping = Map.ofEntries(
Map.entry("relevance", "score")
);
@Mapping(target = "start", expression = "java( calculateStart(src.getPage(), src.getPageSize()) )")
@Mapping(target = "rows", source = "pageSize")
@Mapping(target = "sort", source = "sortBy")
@Mapping(target = "sort", expression = "java( eu.openaire.api.mappers.Utils.formatSortByParam(src.getSortBy(), sortFieldMapping) )")
SolrQueryParams toSolrQuery(DataSourceRequest src);
@Named("calculateStart")

View File

@ -11,9 +11,13 @@ import java.util.Map;
@Mapper(componentModel = "spring")
public interface OrganizationRequestMapper {
Map<String, String> sortFieldMapping = Map.ofEntries(
Map.entry("relevance", "score")
);
@Mapping(target = "start", expression = "java( calculateStart(src.getPage(), src.getPageSize()) )")
@Mapping(target = "rows", source = "pageSize")
@Mapping(target = "sort", source = "sortBy")
@Mapping(target = "sort", expression = "java( eu.openaire.api.mappers.Utils.formatSortByParam(src.getSortBy(), sortFieldMapping) )")
SolrQueryParams toSolrQuery(OrganizationRequest src);
@Named("calculateStart")

View File

@ -12,9 +12,15 @@ import java.util.Optional;
@Mapper(componentModel = "spring")
public interface ProjectRequestMapper {
Map<String, String> sortFieldMapping = Map.ofEntries(
Map.entry("relevance", "score"),
Map.entry("startDate", "projectstartyear"),
Map.entry("endDate", "projectendyear")
);
@Mapping(target = "start", expression = "java( calculateStart(src.getPage(), src.getPageSize()) )")
@Mapping(target = "rows", source = "pageSize")
@Mapping(target = "sort", source = "sortBy")
@Mapping(target = "sort", expression = "java( eu.openaire.api.mappers.Utils.formatSortByParam(src.getSortBy(), sortFieldMapping) )")
SolrQueryParams toSolrQuery(ProjectRequest src);
@Named("calculateStart")
@ -144,7 +150,6 @@ public interface ProjectRequestMapper {
solrQueryParams.setFq(fqList);
System.out.println(solrQueryParams.toString());
}
}

View File

@ -13,9 +13,21 @@ import java.util.Optional;
@Mapper(componentModel = "spring")
public interface ResearchProductsRequestMapper {
Map<String, String> sortFieldMapping = Map.ofEntries(
Map.entry("relevance", "score"),
Map.entry("publicationDate", "resultdateofacceptance"),
Map.entry("dateOfCollection", "dateofcollection"),
Map.entry("influence", "influence"),
Map.entry("popularity", "popularity"),
Map.entry("citationCount", "citation_count"),
Map.entry("impulse", "impulse")
);
@Mapping(target = "start", expression = "java( calculateStart(src.getPage(), src.getPageSize()) )")
@Mapping(target = "rows", source = "pageSize")
@Mapping(target = "sort", source = "sortBy")
@Mapping(target = "sort", expression = "java( eu.openaire.api.mappers.Utils.formatSortByParam(src.getSortBy(), sortFieldMapping) )")
SolrQueryParams toSolrQuery(ResearchProductsRequest src);
@Named("calculateStart")

View File

@ -1,6 +1,5 @@
package eu.openaire.api.repositories;
import eu.openaire.api.mappers.Utils;
import eu.openaire.api.solr.SolrConnectionManager;
import eu.openaire.api.solr.SolrQueryParams;
import lombok.RequiredArgsConstructor;
@ -47,15 +46,8 @@ public class SolrRepository {
query.setRows(queryParams.getRows());
// set sorting
if (!Utils.isNullOrEmpty(queryParams.getSort())) {
String[] sortByPairs = queryParams.getSort().split(",");
for (String pair : sortByPairs) {
String[] parts = pair.trim().split("\\s+");
String field = parts[0];
SolrQuery.ORDER order = parts[1].equalsIgnoreCase("ASC") ? SolrQuery.ORDER.asc : SolrQuery.ORDER.desc;
query.addSort(field, order);
}
for (var sortClause : queryParams.getSort()) {
query.addSort(sortClause);
}
log.info(query);

View File

@ -1,6 +1,7 @@
package eu.openaire.api.solr;
import lombok.Data;
import org.apache.solr.client.solrj.SolrQuery;
import java.util.List;
@ -17,6 +18,6 @@ public class SolrQueryParams {
int rows;
String sort;
List<SolrQuery.SortClause> sort;
}