From 9fe448cde1404ae6870f891f0a615bdc81ad3b4d Mon Sep 17 00:00:00 2001 From: TobiasNx Date: Thu, 9 Jan 2025 15:35:21 +0100 Subject: [PATCH] Draft for add facette for collections #2109 --- .../controllers/resources/Application.java | 38 +++++++++++-------- web/app/controllers/resources/Search.java | 3 +- web/app/views/tags/facets.scala.html | 1 + 3 files changed, 26 insertions(+), 16 deletions(-) diff --git a/web/app/controllers/resources/Application.java b/web/app/controllers/resources/Application.java index 0faa4705c..c23e75d52 100644 --- a/web/app/controllers/resources/Application.java +++ b/web/app/controllers/resources/Application.java @@ -95,6 +95,8 @@ public class Application extends Controller { public static final String OWNER_AGGREGATION = "hasItem.heldBy.id"; /** The internal ES field for the topics. */ public static final String TOPIC_AGGREGATION = "subject.label.raw"; + /** The internal ES field for the topics. */ + public static final String COLLECTION_AGGREGATION = "inCollection.label.raw"; /** The internal ES field for subjects. */ public static final String SUBJECT_FIELD = "subject.componentList.id"; /** The internal ES field for contributing agents. */ @@ -174,6 +176,7 @@ public static String currentUri() { * @param publisher Query for the resource publisher * @param issued Query for the resource issued year * @param medium Query for the resource medium + * @param collection Query for the resource collection * * @param from The page start (offset of page of resource to return) * @param size The page size (size of page of resource to return) * @param owner Owner filter for resource queries @@ -189,7 +192,7 @@ public static String currentUri() { */ public static Promise query(final String q, final String agent, final String name, final String subject, final String id, - final String publisher, final String issued, final String medium, + final String publisher, final String issued, final String medium, String collection, final int from, final int size, final String owner, String t, String sort, String word, String f, String aggs, String location, String nested, String filter) { @@ -217,7 +220,7 @@ public static Promise query(final String q, final String agent, Logger.debug("Not cached: {}, will cache for one hour", cacheId); QueryBuilder queryBuilder = new Queries.Builder().q(q).agent(agent) .name(name).subject(subject).id(id).publisher(publisher).issued(issued) - .medium(medium).t(t).owner(owner).nested(nested).location(location) + .medium(medium).t(t).owner(owner).nested(nested).location(location).collection(collection) .filter(filter).word(word).build(); String sortBy = responseFormat.equals(Accept.Format.RSS.queryParamString) ? "newest" @@ -226,7 +229,7 @@ public static Promise query(final String q, final String agent, .size(size).sort(sortBy).aggs(aggregations).build(); Promise result = - createResult(q, agent, name, subject, id, publisher, issued, medium, + createResult(q, agent, name, subject, id, publisher, issued, medium, collection, from, size, owner, t, sort, word, nested, responseFormat, index); cacheOnRedeem(cacheId, result, ONE_HOUR); @@ -273,7 +276,7 @@ private static Promise resultOrError(final String q, private static Promise createResult(final String q, final String agent, final String name, final String subject, final String id, final String publisher, final String issued, - final String medium, final int from, final int size, final String owner, + final String medium, final String collection, final int from, final int size, final String owner, String t, String sort, String word, String nested, String responseFormat, Search index) { Promise result = @@ -286,7 +289,7 @@ private static Promise createResult(final String q, switch (Format.of(responseFormat)) { case HTML: return ok(query.render(s, q, agent, name, subject, id, - publisher, issued, medium, from, size, + publisher, issued, medium, collection, from, size, queryResources.getTotal(), owner, t, sort, word)); case RSS: String[] segments = request().uri().split("/"); @@ -379,7 +382,7 @@ private static Status withCallback(final JsonNode json) { static String toSuggestions(JsonNode json, String labelFields) { Stream defaultFields = - Stream.of("title", "contribution", "medium", "startDate-endDate"); + Stream.of("title", "contribution", "medium", "collection", "startDate-endDate"); String fields = labelFields.equals("suggest") ? defaultFields.collect(Collectors.joining(",")) : labelFields; Stream documents = Lists.newArrayList(json.elements()).stream(); @@ -670,6 +673,7 @@ private static void uncache(List ids) { * @param publisher Query for the resource publisher * @param issued Query for the resource issued year * @param medium Query for the resource medium + * @param collection Query for the resource collection * * @param from The page start (offset of page of resource to return) * @param size The page size (size of page of resource to return) * @param owner Owner filter for resource queries @@ -683,12 +687,12 @@ private static void uncache(List ids) { * @return The search results */ public static Promise facets(String q, String agent, String name, - String subject, String id, String publisher, String issued, String medium, + String subject, String id, String publisher, String issued, String medium, String collection, int from, int size, String owner, String t, String field, String sort, String word, String location, String nested, String filter) { String key = String.format("facets.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s", - field, q, agent, name, id, publisher, word, subject, issued, medium, + field, q, agent, name, id, publisher, word, subject, issued, medium, collection, owner, t, nested); Result cachedResult = (Result) Cache.get(key); if (cachedResult != null) { @@ -719,9 +723,9 @@ public static Promise facets(String q, String agent, String name, String t1 = p1.getLeft().get("key").asText(); String t2 = p2.getLeft().get("key").asText(); boolean t1Current = - current(subject, agent, medium, owner, t, field, t1, location); + current(subject, agent, medium, collection, owner, t, field, t1, location); boolean t2Current = - current(subject, agent, medium, owner, t, field, t2, location); + current(subject, agent, medium, collection, owner, t, field, t2, location); if (t1Current == t2Current) { if (!field.equals(ISSUED_FIELD)) { Integer c1 = p1.getLeft().get("doc_count").asInt(); @@ -742,6 +746,9 @@ public static Promise facets(String q, String agent, String name, String mediumQuery = !field.equals(MEDIUM_FIELD) // ? medium : queryParam(medium, term); + String collectionQuery = !field.equals(COLLECTION_AGGREGATION) // + ? collection + : queryParam(collection, term); String typeQuery = !field.equals(TYPE_FIELD) // ? t : queryParam(t, term); @@ -762,10 +769,10 @@ public static Promise facets(String q, String agent, String name, : queryParam(location, term); boolean current = - current(subject, agent, medium, owner, t, field, term, location); + current(subject, agent, medium, collection, owner, t, field, term, location); String routeUrl = routes.Application.query(q, agentQuery, name, - subjectQuery, id, publisher, issuedQuery, mediumQuery, from, size, + subjectQuery, id, publisher, issuedQuery, mediumQuery, collectionQuery, from, size, ownerQuery, typeQuery, sort(sort, subjectQuery), word, null, field, locationQuery, nested, filter).url(); @@ -780,7 +787,7 @@ ownerQuery, typeQuery, sort(sort, subjectQuery), word, null, field, }; Promise promise = - query(q, agent, name, subject, id, publisher, issued, medium, from, + query(q, agent, name, subject, id, publisher, issued, medium, collection, from, size, owner, t, sort, word, "json", field, location, nested, filter) .map(result -> { JsonNode json = Json.parse(Helpers.contentAsString(result)) @@ -791,7 +798,7 @@ ownerQuery, typeQuery, sort(sort, subjectQuery), word, null, field, String labelKey = String.format( "facets-labels.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s.%s", field, q, agent, name, id, publisher, word, subject, - issued, medium, + issued, medium, collection, field.equals(OWNER_AGGREGATION) ? "" : owner, t); @SuppressWarnings("unchecked") List> labelledFacets = @@ -812,9 +819,10 @@ private static String sort(String sort, String subjectQuery) { return subjectQuery.contains(",") ? "" /* relevance */ : sort; } - private static boolean current(String subject, String agent, String medium, + private static boolean current(String subject, String agent, String medium, String collection, String owner, String t, String field, String term, String location) { return field.equals(MEDIUM_FIELD) && contains(medium, term) + || field.equals(COLLECTION_AGGREGATION) && contains(collection, term) || field.equals(TYPE_FIELD) && contains(t, term) || field.equals(OWNER_AGGREGATION) && contains(owner, term) || field.equals(SUBJECT_FIELD) && contains(subject, term) diff --git a/web/app/controllers/resources/Search.java b/web/app/controllers/resources/Search.java index 2646ec16e..e7e310387 100644 --- a/web/app/controllers/resources/Search.java +++ b/web/app/controllers/resources/Search.java @@ -5,6 +5,7 @@ import static controllers.resources.Application.AGENT_FIELD; import static controllers.resources.Application.ISSUED_FIELD; import static controllers.resources.Application.MEDIUM_FIELD; +import static controllers.resources.Application.COLLECTION_AGGREGATION; import static controllers.resources.Application.OWNER_AGGREGATION; import static controllers.resources.Application.SUBJECT_FIELD; import static controllers.resources.Application.TOPIC_AGGREGATION; @@ -119,7 +120,7 @@ public Builder() {} * The values supported for the `aggregations` query parameter. */ public static final List SUPPORTED_AGGREGATIONS = Arrays.asList( - ISSUED_FIELD, SUBJECT_FIELD, TYPE_FIELD, MEDIUM_FIELD, OWNER_AGGREGATION, + ISSUED_FIELD, SUBJECT_FIELD, TYPE_FIELD, MEDIUM_FIELD, COLLECTION_AGGREGATION, OWNER_AGGREGATION, AGENT_FIELD, SPATIAL_LABEL_FIELD, SPATIAL_GEO_FIELD, SPATIAL_ID_FIELD, SUBJECT_ID_FIELD, TOPIC_AGGREGATION); diff --git a/web/app/views/tags/facets.scala.html b/web/app/views/tags/facets.scala.html index 708707439..db5ced959 100644 --- a/web/app/views/tags/facets.scala.html +++ b/web/app/views/tags/facets.scala.html @@ -134,4 +134,5 @@
@facetToggle("issued-facet-all"){Erscheinungsjahr} @facetLabel(issued, issue
@facets("Medientypen", Application.MEDIUM_FIELD, medium, "dropup")
@facets("Publikationstypen", Application.TYPE_FIELD, t, "dropup")
@facets("Bestand in Bibliotheken", Application.OWNER_AGGREGATION, owner, "dropup")
+
@facets("Collections", Application.COLLECTION_AGGREGATION, collection, "dropup")