feat(api): add book sorts for author name / author sort name (#2497)

This commit is contained in:
James Ward
2026-09-03 21:56:45 -04:00
committed by GitHub
parent 4e28fde680
commit f44a7df9e6
4 changed files with 110 additions and 23 deletions
@@ -55,8 +55,21 @@ public class BookQueryService {
return findBooksPaged(visibleBooks(libraryIds, userId), pageable, userId);
}
private Specification<BookEntity> distinct(Specification<BookEntity> filter) {
return (root, query, cb) -> {
// We need to query distinct because otherwise the `JOIN` + `LIMIT` cause the page sizes to be wrong.
//
// I've tried a few ways to just get only what we need - distinct IDs from the repository.
// With Projections, we ran into issues with the order by not getting applied. I'm sure
// there's a better way but right now this works well enough, even though it over-fetches.
query.distinct(true);
return filter.toPredicate(root, query, cb);
};
}
public Page<Book> findBooksPaged(Specification<BookEntity> spec, Pageable pageable, Long userId) {
Page<BookEntity> page = bookRepository.findAll(spec, pageable);
Page<BookEntity> page = bookRepository.findAll(distinct(spec), pageable);
Map<Long, BookEntity> booksById = bookRepository
.findAllWithMetadataByIds(page.getContent().stream().map(BookEntity::getId).collect(Collectors.toSet()))
.stream()
@@ -2,6 +2,7 @@ package org.booklore.service.browse;
import jakarta.persistence.criteria.CriteriaBuilder;
import jakarta.persistence.criteria.Expression;
import jakarta.persistence.criteria.From;
import jakarta.persistence.criteria.Join;
import jakarta.persistence.criteria.JoinType;
import jakarta.persistence.criteria.Order;
@@ -45,6 +46,10 @@ public class BookSortRegistry {
for (String field : List.of("personalRating", "lastReadTime", "readStatus", "dateFinished")) {
registry.register(field, progressField(field));
}
registry.register("authorName", authorField("name"));
registry.register("authorSortName", authorField("sortName"));
registry.register("readingProgress", readingProgress());
registry.register("random", random());
@@ -56,17 +61,21 @@ public class BookSortRegistry {
}
private static SortOrderBuilder<BookEntity> metadataField(String field) {
return ctx -> List.of(order(ctx, metadataJoin(ctx.root()).get(field)));
return ctx -> List.of(order(ctx, metadataJoin(ctx).get(field)));
}
private static SortOrderBuilder<BookEntity> progressField(String field) {
return ctx -> List.of(order(ctx, progressJoin(ctx).get(field)));
}
private static SortOrderBuilder<BookEntity> authorField(String field) {
return ctx -> List.of(order(ctx, authorJoin(ctx).get(field)));
}
private static SortOrderBuilder<BookEntity> readingProgress() {
return ctx -> {
CriteriaBuilder cb = ctx.cb();
Join<BookEntity, ?> progress = progressJoin(ctx);
var progress = progressJoin(ctx);
Expression<Float> greatest = null;
for (String field : PROGRESS_PERCENT_FIELDS) {
Expression<Float> value = cb.coalesce(progress.get(field), cb.literal(0f));
@@ -112,28 +121,49 @@ public class BookSortRegistry {
return ctx.descending() ? ctx.cb().desc(expression) : ctx.cb().asc(expression);
}
@SuppressWarnings("unchecked")
private static <Y> Join<BookEntity, Y> metadataJoin(Root<BookEntity> root) {
for (Join<BookEntity, ?> join : root.getJoins()) {
if (join.getAttribute().getName().equals("metadata") && join.getJoinType() == JoinType.LEFT) {
return (Join<BookEntity, Y>) join;
private static Join<?, ?> getSortJoin(From<?, ?> root, String name) {
String alias = "sort_" + name;
for (var join : root.getJoins()) {
if (!alias.equals(join.getAlias())) {
continue;
}
}
return root.join("metadata", JoinType.LEFT);
}
@SuppressWarnings("unchecked")
private static Join<BookEntity, ?> progressJoin(SortContext<BookEntity> ctx) {
Root<BookEntity> root = ctx.root();
for (Join<BookEntity, ?> join : root.getJoins()) {
if (join.getAttribute().getName().equals("userBookProgress")) {
return join;
if (!name.equals(join.getAttribute().getName())) {
continue;
}
if (!JoinType.LEFT.equals(join.getJoinType())) {
continue;
}
return join;
}
Join<BookEntity, ?> join = root.join("userBookProgress", JoinType.LEFT);
CriteriaBuilder cb = ctx.cb();
Path<Object> joinUserId = ((Join<BookEntity, Object>) join).get("user").get("id");
join.on(ctx.userId() != null ? cb.equal(joinUserId, ctx.userId()) : cb.disjunction());
Join<?, ?> join = root.join(name, JoinType.LEFT);
join.alias(alias);
return join;
}
private static Join<?, ?> metadataJoin(SortContext<BookEntity> ctx) {
Root<BookEntity> root = ctx.root();
return getSortJoin(root, "metadata");
}
private static Join<?, ?> progressJoin(SortContext<BookEntity> ctx) {
Root<BookEntity> root = ctx.root();
var join = getSortJoin(root, "userBookProgress");
CriteriaBuilder cb = ctx.cb();
Path<?> joinUserId = join.get("user").get("id");
join.on(ctx.userId() != null ? cb.equal(joinUserId, ctx.userId()) : cb.disjunction());
return join;
}
private static Join<?, ?> authorJoin(SortContext<BookEntity> ctx) {
var parent = metadataJoin(ctx);
return getSortJoin(parent, "authors");
}
}