refactor(api): list facets without their values (#2815)

This commit is contained in:
Alex
2026-10-18 19:49:09 +01:00
committed by GitHub
parent 02b18d78ea
commit 41da123135
4 changed files with 86 additions and 57 deletions
@@ -105,7 +105,7 @@ public class BookController {
return ResponseEntity.ok(bookBrowseService.wrapLegacy(bookService.getBookDTOsPaged(pageable), pageable));
}
@Operation(summary = "Get book facets", description = "Available facet values and counts for the current user, scoped by the same facet, facet_logic, and query parameters as the page endpoint. Each facet omits its own selections from its counts.")
@Operation(summary = "Get book facets", description = "Every facet for the current user, without its values, scoped by the same facet, facet_logic, and query parameters as the page endpoint, plus the sort options. Each facet's hasItems says whether any matching book has a value for it. Load a facet's values from /facets/{facetName}.")
@ApiResponse(responseCode = "200", description = "Facet groups returned successfully")
@GetMapping("/facets")
public ResponseEntity<FacetGroupsResponse> getBookFacets(
@@ -15,7 +15,10 @@ public record FacetGroupsResponse(List<Link> links, List<FacetGroup> facets) {
}
@JsonInclude(JsonInclude.Include.NON_NULL)
public record Metadata(String rel, String key, String title) {
public record Metadata(String rel, String key, String title, Boolean hasItems) {
public Metadata(String rel, String key, String title) {
this(rel, key, title, null);
}
}
@JsonInclude(JsonInclude.Include.NON_NULL)
@@ -22,6 +22,7 @@ import org.booklore.exception.ApiError;
import org.booklore.model.dto.BookLoreUser;
import org.booklore.model.dto.browse.FacetGroupsResponse;
import org.booklore.model.dto.browse.FacetGroupsResponse.FacetGroup;
import org.booklore.model.dto.browse.FacetGroupsResponse.Metadata;
import org.booklore.model.entity.BookEntity;
import org.booklore.model.entity.BookFileEntity;
import org.booklore.model.entity.UserBookProgressEntity;
@@ -157,7 +158,7 @@ public class BookFacetService {
for (FacetDef def : FACETS) {
Specification<BookEntity> base = filterSpecifications.base(query, facets, facetLogic, scope, def.key());
groups.add(facetGroup(def, base, scope, builder));
groups.add(new FacetGroup(new Metadata("facet", def.key(), def.title(), hasItems(def, base, scope)), List.of()));
}
return new FacetGroupsResponse(builder.selfLinks(), groups);
});
@@ -231,6 +232,28 @@ public class BookFacetService {
return builder.group(def.key(), def.title(), counts);
}
private boolean hasItems(FacetDef def, Specification<BookEntity> base, BrowseScope scope) {
return anyValue(def, base, scope) || ("file_type".equals(def.key()) && anyValue(PHYSICAL_FILE_TYPE, base, scope));
}
private boolean anyValue(FacetDef def, Specification<BookEntity> base, BrowseScope scope) {
CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Integer> cq = cb.createQuery(Integer.class);
Root<BookEntity> root = cq.from(BookEntity.class);
Expression<?> value = def.value().apply(cb, root, scope);
List<Predicate> predicates = new ArrayList<>();
Predicate basePredicate = base.toPredicate(root, cq, cb);
if (basePredicate != null) {
predicates.add(basePredicate);
}
predicates.add(cb.isNotNull(value));
cq.select(cb.literal(1));
cq.where(predicates.toArray(Predicate[]::new));
return !entityManager.createQuery(cq).setMaxResults(1).getResultList().isEmpty();
}
private List<FacetResponseBuilder.FacetCount> count(FacetDef def, Specification<BookEntity> base, BrowseScope scope) {
return count(def, base, scope, null, 0, MAX_VALUES);
}
@@ -242,11 +242,11 @@ class BookFacetServiceTest {
book("B", "Romance", "Bob");
em.flush();
FacetGroupsResponse response = facetService.getFacets(null, null, null);
FacetGroup genre = facet("genre", null);
assertThat(count(group(response, "genre"), "Horror")).isEqualTo(1);
assertThat(count(group(response, "genre"), "Romance")).isEqualTo(1);
assertThat(group(response, "author").links()).extracting(FacetLink::value).contains("Alice", "Bob");
assertThat(count(genre, "Horror")).isEqualTo(1);
assertThat(count(genre, "Romance")).isEqualTo(1);
assertThat(facet("author", null).links()).extracting(FacetLink::value).contains("Alice", "Bob");
}
@Test
@@ -256,14 +256,15 @@ class BookFacetServiceTest {
book("C", "Romance", "Bob");
em.flush();
FacetGroupsResponse response = facetService.getFacets(List.of("genre:Horror"), null, null);
List<String> selection = List.of("genre:Horror");
FacetGroup genre = facet("genre", selection);
// genre omits itself: both Horror (2) and Romance (1) still appear with full counts.
assertThat(count(group(response, "genre"), "Horror")).isEqualTo(2);
assertThat(count(group(response, "genre"), "Romance")).isEqualTo(1);
assertThat(count(genre, "Horror")).isEqualTo(2);
assertThat(count(genre, "Romance")).isEqualTo(1);
// a different facet honors the genre:Horror filter: only Horror authors remain.
assertThat(group(response, "author").links()).extracting(FacetLink::value).containsExactly("Alice");
assertThat(facet("author", selection).links()).extracting(FacetLink::value).containsExactly("Alice");
}
@Test
@@ -273,8 +274,7 @@ class BookFacetServiceTest {
book("C", "Romance", "Bob");
em.flush();
List<String> genres = group(facetService.getFacets(null, null, null), "genre").links()
.stream().map(FacetLink::value).toList();
List<String> genres = facet("genre", null).links().stream().map(FacetLink::value).toList();
assertThat(genres).containsExactly("Horror", "Romance");
}
@@ -283,7 +283,7 @@ class BookFacetServiceTest {
book("A", "Horror", "Alice");
em.flush();
FacetLink horror = link(group(facetService.getFacets(null, null, null), "genre"), "Horror");
FacetLink horror = link(facet("genre", null), "Horror");
assertThat(horror.href()).isEqualTo("/api/v1/books/page?facet=genre%3AHorror");
assertThat(horror.properties().numberOfItems()).isEqualTo(1);
assertThat(horror.rel()).containsExactly("facet");
@@ -314,12 +314,12 @@ class BookFacetServiceTest {
book("B", "Romance", "Bob");
em.flush();
FacetGroupsResponse withNull = facetService.getFacets(null, null, null);
FacetGroupsResponse withEmpty = facetService.getFacets(List.of(), null, null);
FacetGroup withNull = facet("genre", null);
FacetGroup withEmpty = facet("genre", List.of());
assertThat(count(group(withEmpty, "genre"), "Horror")).isEqualTo(count(group(withNull, "genre"), "Horror"));
assertThat(count(group(withEmpty, "genre"), "Romance")).isEqualTo(count(group(withNull, "genre"), "Romance"));
assertThat(count(group(withEmpty, "genre"), "Horror")).isEqualTo(1);
assertThat(count(withEmpty, "Horror")).isEqualTo(count(withNull, "Horror"));
assertThat(count(withEmpty, "Romance")).isEqualTo(count(withNull, "Romance"));
assertThat(count(withEmpty, "Horror")).isEqualTo(1);
}
@Test
@@ -329,13 +329,15 @@ class BookFacetServiceTest {
book("C", "Romance", "Alice");
em.flush();
FacetGroupsResponse response = facetService.getFacets(List.of("genre:Horror", "author:Alice"), null, null);
List<String> selection = List.of("genre:Horror", "author:Alice");
FacetGroup genre = facet("genre", selection);
FacetGroup author = facet("author", selection);
assertThat(count(group(response, "genre"), "Horror")).isEqualTo(1);
assertThat(count(group(response, "genre"), "Romance")).isEqualTo(1);
assertThat(count(genre, "Horror")).isEqualTo(1);
assertThat(count(genre, "Romance")).isEqualTo(1);
assertThat(count(group(response, "author"), "Alice")).isEqualTo(1);
assertThat(count(group(response, "author"), "Bob")).isEqualTo(1);
assertThat(count(author, "Alice")).isEqualTo(1);
assertThat(count(author, "Bob")).isEqualTo(1);
}
@Test
@@ -347,12 +349,12 @@ class BookFacetServiceTest {
List<String> genres = List.of("genre:Horror", "genre:Romance");
assertThat(group(facetService.getFacets(genres, "or", null), "author").links())
assertThat(facet("author", genres, "or").links())
.extracting(FacetLink::value).containsExactlyInAnyOrder("Alice", "Bob");
assertThat(group(facetService.getFacets(genres, "and", null), "author").links()).isEmpty();
assertThat(facet("author", genres, "and").links()).isEmpty();
assertThat(group(facetService.getFacets(genres, "not", null), "author").links())
assertThat(facet("author", genres, "not").links())
.extracting(FacetLink::value).containsExactly("Cara");
}
@@ -363,10 +365,10 @@ class BookFacetServiceTest {
}
em.flush();
FacetGroupsResponse response = facetService.getFacets(null, null, null);
assertThat(group(response, "genre").links()).hasSize(100);
assertThat(group(response, "author").links()).hasSize(100);
for (String key : List.of("genre", "author")) {
FacetGroupsResponse response = facetService.getFacet(key, null, null, null, null, PageRequest.of(0, 200));
assertThat(group(response, key).links()).hasSize(100);
}
}
@Test
@@ -389,18 +391,23 @@ class BookFacetServiceTest {
}
@Test
void individualNumberFacetsMatchTheOverallEndpoint() {
BookMetadataEntity metadata = book("A", "Genre", "Author");
metadata.setPageCount(120);
metadata.setGoodreadsRating(4.2);
void listsEveryFacetWithWhetherItHasItems() {
book("A", "Horror", "Alice").setPageCount(120);
book("B", "Horror", "Alice").getBook().setIsPhysical(true);
em.flush();
FacetGroupsResponse overall = facetService.getFacets(null, null, null);
for (String key : List.of("page_count", "goodreads_rating")) {
FacetGroupsResponse single = facetService.getFacet(key, null, null, null, null, PageRequest.of(0, 1));
assertThat(single.facets()).containsExactly(group(overall, key));
assertThat(single.links()).extracting(Link::rel).containsExactly(List.of("self"));
FacetGroupsResponse response = facetService.getFacets(null, null, null);
for (String key : List.of("genre", "page_count", "file_type")) {
assertThat(group(response, key).metadata().hasItems()).isTrue();
}
for (String key : List.of("mood", "goodreads_rating")) {
assertThat(group(response, key).metadata().hasItems()).isFalse();
}
assertThat(group(response, "sort").metadata().hasItems()).isNull();
assertThat(group(response, "genre").links()).isEmpty();
assertThat(springMapper.writeValueAsString(group(response, "mood").metadata()))
.isEqualTo("{\"rel\":\"facet\",\"key\":\"mood\",\"title\":\"Moods\",\"hasItems\":false}");
}
@Test
@@ -410,8 +417,7 @@ class BookFacetServiceTest {
}
em.flush();
FacetGroupsResponse response = facetService.getFacets(null, null, null);
FacetGroup pageCount = group(response, "page_count");
FacetGroup pageCount = facet("page_count", null);
assertThat(pageCount.links()).hasSize(1);
FacetLink range = pageCount.links().getFirst();
@@ -419,7 +425,7 @@ class BookFacetServiceTest {
assertThat(range.href()).isEqualTo("/api/v1/books/page?facet=page_count%3A{min}..{max}");
assertThat(range.properties().min().intValue()).isEqualTo(1);
assertThat(range.properties().max().intValue()).isEqualTo(101);
assertThat(group(response, "genre").links()).noneMatch(l -> Boolean.TRUE.equals(l.templated()));
assertThat(facet("genre", null).links()).noneMatch(l -> Boolean.TRUE.equals(l.templated()));
}
@Test
@@ -432,7 +438,7 @@ class BookFacetServiceTest {
book("High", "Genre", "Author").setGoodreadsRating(4.8);
em.flush();
FacetGroup goodreads = group(facetService.getFacets(List.of("goodreads_rating:4..<4.5"), null, null), "goodreads_rating");
FacetGroup goodreads = facet("goodreads_rating", List.of("goodreads_rating:4..<4.5"));
FacetLink range = goodreads.links().getFirst();
assertThat(range.templated()).isTrue();
@@ -474,7 +480,7 @@ class BookFacetServiceTest {
book("B", "Romance", "Bob");
em.flush();
FacetGroup genre = group(facetService.getFacets(List.of("genre:Horror"), null, null), "genre");
FacetGroup genre = facet("genre", List.of("genre:Horror"));
FacetLink horror = link(genre, "Horror");
FacetLink romance = link(genre, "Romance");
@@ -490,7 +496,7 @@ class BookFacetServiceTest {
book("A", "Horror", "Alice");
em.flush();
FacetGroup genre = group(facetService.getFacets(List.of("genre:Horror", "author:Alice"), null, null), "genre");
FacetGroup genre = facet("genre", List.of("genre:Horror", "author:Alice"));
FacetLink horror = link(genre, "Horror");
assertThat(horror.rel()).containsExactly("self", "facet");
@@ -502,7 +508,7 @@ class BookFacetServiceTest {
book("A", "Horror", "Alice");
em.flush();
FacetGroup genre = group(facetService.getFacets(List.of("genre:horror"), null, null), "genre");
FacetGroup genre = facet("genre", List.of("genre:horror"));
FacetLink horror = link(genre, "Horror");
assertThat(horror.rel()).contains("self");
@@ -530,7 +536,8 @@ class BookFacetServiceTest {
book("A", "Horror", "Alice");
em.flush();
String json = springMapper.writeValueAsString(facetService.getFacets(List.of("genre:Horror"), null, null));
String json = springMapper.writeValueAsString(
facetService.getFacet("genre", List.of("genre:Horror"), null, null, null, PageRequest.of(0, 100)));
assertThat(json).contains("\"rel\":\"self\"");
assertThat(json).contains("\"rel\":[\"self\",\"facet\"]");
@@ -550,10 +557,8 @@ class BookFacetServiceTest {
.permissions(permissions)
.build());
FacetGroupsResponse response = facetService.getFacets(null, null, null);
assertThat(group(response, "genre").links()).isEmpty();
assertThat(group(response, "author").links()).isEmpty();
assertThat(facet("genre", null).links()).isEmpty();
assertThat(facet("author", null).links()).isEmpty();
}
@Test
@@ -568,10 +573,8 @@ class BookFacetServiceTest {
book("R", "Romance", "Bob");
em.flush();
FacetGroupsResponse response = facetService.getFacets(null, null, null);
assertThat(count(group(response, "genre"), "Romance")).isEqualTo(1);
assertThat(count(group(response, "genre"), "Horror")).isNull();
assertThat(count(group(response, "author"), "Alice")).isNull();
assertThat(count(facet("genre", null), "Romance")).isEqualTo(1);
assertThat(count(facet("genre", null), "Horror")).isNull();
assertThat(count(facet("author", null), "Alice")).isNull();
}
}