Adjust sorting clauses for all entities
This commit is contained in:
parent
f195d1c58d
commit
a8a498929b
|
@ -84,7 +84,7 @@ public class ProjectRequest {
|
||||||
@Parameter(description = "Number of results per page", schema = @Schema(defaultValue = "10", type = "integer"))
|
@Parameter(description = "Number of results per page", schema = @Schema(defaultValue = "10", type = "integer"))
|
||||||
private int pageSize = 10;
|
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"))
|
@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|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.")
|
@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;
|
private String sortBy;
|
||||||
}
|
}
|
|
@ -145,7 +145,7 @@ public class ResearchProductsRequest {
|
||||||
@Parameter(description = "Number of results per page", schema = @Schema(defaultValue = "10", type = "integer"))
|
@Parameter(description = "Number of results per page", schema = @Schema(defaultValue = "10", type = "integer"))
|
||||||
private int pageSize = 10;
|
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"))
|
@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 = "^((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'.")
|
@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;
|
private String sortBy;
|
||||||
}
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
package eu.openaire.api.mappers;
|
package eu.openaire.api.mappers;
|
||||||
|
|
||||||
import eu.openaire.api.errors.exceptions.BadRequestException;
|
import eu.openaire.api.errors.exceptions.BadRequestException;
|
||||||
|
import org.apache.solr.client.solrj.SolrQuery;
|
||||||
import org.apache.solr.client.solrj.util.ClientUtils;
|
import org.apache.solr.client.solrj.util.ClientUtils;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
@ -8,9 +9,7 @@ import java.time.LocalDateTime;
|
||||||
import java.time.ZoneOffset;
|
import java.time.ZoneOffset;
|
||||||
import java.time.ZonedDateTime;
|
import java.time.ZonedDateTime;
|
||||||
import java.time.format.DateTimeFormatter;
|
import java.time.format.DateTimeFormatter;
|
||||||
import java.util.ArrayList;
|
import java.util.*;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public class Utils {
|
public class Utils {
|
||||||
|
|
||||||
|
@ -150,4 +149,27 @@ public class Utils {
|
||||||
}
|
}
|
||||||
return null;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,9 +11,13 @@ import java.util.Map;
|
||||||
@Mapper(componentModel = "spring")
|
@Mapper(componentModel = "spring")
|
||||||
public interface DataSourceRequestMapper {
|
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 = "start", expression = "java( calculateStart(src.getPage(), src.getPageSize()) )")
|
||||||
@Mapping(target = "rows", source = "pageSize")
|
@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);
|
SolrQueryParams toSolrQuery(DataSourceRequest src);
|
||||||
|
|
||||||
@Named("calculateStart")
|
@Named("calculateStart")
|
||||||
|
|
|
@ -11,9 +11,13 @@ import java.util.Map;
|
||||||
@Mapper(componentModel = "spring")
|
@Mapper(componentModel = "spring")
|
||||||
public interface OrganizationRequestMapper {
|
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 = "start", expression = "java( calculateStart(src.getPage(), src.getPageSize()) )")
|
||||||
@Mapping(target = "rows", source = "pageSize")
|
@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);
|
SolrQueryParams toSolrQuery(OrganizationRequest src);
|
||||||
|
|
||||||
@Named("calculateStart")
|
@Named("calculateStart")
|
||||||
|
|
|
@ -12,9 +12,15 @@ import java.util.Optional;
|
||||||
@Mapper(componentModel = "spring")
|
@Mapper(componentModel = "spring")
|
||||||
public interface ProjectRequestMapper {
|
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 = "start", expression = "java( calculateStart(src.getPage(), src.getPageSize()) )")
|
||||||
@Mapping(target = "rows", source = "pageSize")
|
@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);
|
SolrQueryParams toSolrQuery(ProjectRequest src);
|
||||||
|
|
||||||
@Named("calculateStart")
|
@Named("calculateStart")
|
||||||
|
@ -144,7 +150,6 @@ public interface ProjectRequestMapper {
|
||||||
|
|
||||||
solrQueryParams.setFq(fqList);
|
solrQueryParams.setFq(fqList);
|
||||||
|
|
||||||
System.out.println(solrQueryParams.toString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,9 +13,21 @@ import java.util.Optional;
|
||||||
@Mapper(componentModel = "spring")
|
@Mapper(componentModel = "spring")
|
||||||
public interface ResearchProductsRequestMapper {
|
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 = "start", expression = "java( calculateStart(src.getPage(), src.getPageSize()) )")
|
||||||
@Mapping(target = "rows", source = "pageSize")
|
@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);
|
SolrQueryParams toSolrQuery(ResearchProductsRequest src);
|
||||||
|
|
||||||
@Named("calculateStart")
|
@Named("calculateStart")
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
package eu.openaire.api.repositories;
|
package eu.openaire.api.repositories;
|
||||||
|
|
||||||
import eu.openaire.api.mappers.Utils;
|
|
||||||
import eu.openaire.api.solr.SolrConnectionManager;
|
import eu.openaire.api.solr.SolrConnectionManager;
|
||||||
import eu.openaire.api.solr.SolrQueryParams;
|
import eu.openaire.api.solr.SolrQueryParams;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
@ -47,15 +46,8 @@ public class SolrRepository {
|
||||||
query.setRows(queryParams.getRows());
|
query.setRows(queryParams.getRows());
|
||||||
|
|
||||||
// set sorting
|
// set sorting
|
||||||
if (!Utils.isNullOrEmpty(queryParams.getSort())) {
|
for (var sortClause : queryParams.getSort()) {
|
||||||
|
query.addSort(sortClause);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
log.info(query);
|
log.info(query);
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package eu.openaire.api.solr;
|
package eu.openaire.api.solr;
|
||||||
|
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import org.apache.solr.client.solrj.SolrQuery;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
@ -17,6 +18,6 @@ public class SolrQueryParams {
|
||||||
|
|
||||||
int rows;
|
int rows;
|
||||||
|
|
||||||
String sort;
|
List<SolrQuery.SortClause> sort;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue