diff --git a/search/src/main/scala/weco/api/search/models/Aggregation.scala b/search/src/main/scala/weco/api/search/models/Aggregation.scala index d2cd20498..02e3d10fe 100644 --- a/search/src/main/scala/weco/api/search/models/Aggregation.scala +++ b/search/src/main/scala/weco/api/search/models/Aggregation.scala @@ -68,7 +68,7 @@ object AggregationMapping { ) }.toList - private def parseNestedAggregationBuckets(aggregationJson: Json) = { + private def parseNestedAggregationBuckets(aggregationJson: Json) = bucketsFromAnywhere(aggregationJson).map { bucket => // Each ID-based aggregation bucket contains a list of label-based sub-aggregation buckets, // storing a list of labels associated with a given ID. @@ -95,23 +95,22 @@ object AggregationMapping { count = bucket.count ) }.toList - } def aggregationParser( jsonString: String ): Try[Aggregation] = { println(jsonString) parse(jsonString) - .map(json => - root.nested - .as[Json] - .getOption(json) - .orElse(root.nestedSelf.as[Json].getOption(json)) match { - case Some(nestedJson) => - Aggregation(parseNestedAggregationBuckets(nestedJson)) - case _ => Aggregation(parseAggregationBuckets(json)) - } - ) + .map( + json => + root.nested + .as[Json] + .getOption(json) + .orElse(root.nestedSelf.as[Json].getOption(json)) match { + case Some(nestedJson) => + Aggregation(parseNestedAggregationBuckets(nestedJson)) + case _ => Aggregation(parseAggregationBuckets(json)) + }) .toTry } diff --git a/search/src/main/scala/weco/api/search/services/AggregationsBuilder.scala b/search/src/main/scala/weco/api/search/services/AggregationsBuilder.scala index 172d1a40f..9fc373cc8 100644 --- a/search/src/main/scala/weco/api/search/services/AggregationsBuilder.scala +++ b/search/src/main/scala/weco/api/search/services/AggregationsBuilder.scala @@ -1,7 +1,13 @@ package weco.api.search.services import com.sksamuel.elastic4s.ElasticApi.{boolQuery, termsAgg} -import com.sksamuel.elastic4s.requests.searches.aggs.{Aggregation, FilterAggregation, NestedAggregation, TermsAggregation, TermsOrder} +import com.sksamuel.elastic4s.requests.searches.aggs.{ + Aggregation, + FilterAggregation, + NestedAggregation, + TermsAggregation, + TermsOrder +} import com.sksamuel.elastic4s.requests.searches.queries.Query import com.sksamuel.elastic4s.requests.searches.term.TermsQuery import weco.api.search.models.Pairable @@ -21,7 +27,8 @@ case class AggregationParams( ) trait AggregationsBuilder[AggregationRequest, Filter] { - def pairedAggregationRequests(filter: Filter with Pairable): List[AggregationRequest] + def pairedAggregationRequests( + filter: Filter with Pairable): List[AggregationRequest] def getAggregationParams(request: AggregationRequest): AggregationParams def buildFilterQuery: PartialFunction[Filter, Query] @@ -34,7 +41,9 @@ trait AggregationsBuilder[AggregationRequest, Filter] { .contains(aggregationRequest) } - def getAggregations(filters: List[Filter with Pairable], aggregationRequests: List[AggregationRequest]): Seq[FilterAggregation] = { + def getAggregations( + filters: List[Filter with Pairable], + aggregationRequests: List[AggregationRequest]): Seq[FilterAggregation] = aggregationRequests.map { aggregationRequest => val aggregationParams = getAggregationParams(aggregationRequest) @@ -56,21 +65,28 @@ trait AggregationsBuilder[AggregationRequest, Filter] { ) } } - } private def toFilterAggregation( params: AggregationParams, query: List[Query], pairedQuery: Option[Query] ): FilterAggregation = { - val toAggregation: (AggregationParams, List[String]) => Aggregation = params.aggregationType match { - case AggregationType.LabeledIdAggregation => toLabeledIdAggregation - case AggregationType.LabelOnlyAggregation => toTermsAggregation - } + val toAggregation: (AggregationParams, List[String]) => Aggregation = + params.aggregationType match { + case AggregationType.LabeledIdAggregation => toLabeledIdAggregation + case AggregationType.LabelOnlyAggregation => toTermsAggregation + } val selfAggregation: Option[Aggregation] = pairedQuery match { case Some(TermsQuery(_, values, _, _, _, _, _)) => val pairedValues = values.map(value => value.toString).toList - Some(toAggregation(AggregationParams("self", params.fieldPath, params.size, params.aggregationType), pairedValues)) + Some( + toAggregation( + AggregationParams( + "self", + params.fieldPath, + params.size, + params.aggregationType), + pairedValues)) case _ => None } @@ -109,7 +125,13 @@ trait AggregationsBuilder[AggregationRequest, Filter] { include: List[String] ): NestedAggregation = { val idAggregation = - toTermsAggregation(AggregationParams(params.name, s"${params.fieldPath}.id", params.size, params.aggregationType), include) + toTermsAggregation( + AggregationParams( + params.name, + s"${params.fieldPath}.id", + params.size, + params.aggregationType), + include) val labelAggregation = termsAgg("labels", s"${params.fieldPath}.label").size(1) val nestedAggregation = idAggregation.subAggregations(labelAggregation) diff --git a/search/src/main/scala/weco/api/search/services/ImagesRequestBuilder.scala b/search/src/main/scala/weco/api/search/services/ImagesRequestBuilder.scala index b6e7dd185..082d37beb 100644 --- a/search/src/main/scala/weco/api/search/services/ImagesRequestBuilder.scala +++ b/search/src/main/scala/weco/api/search/services/ImagesRequestBuilder.scala @@ -9,10 +9,7 @@ import io.circe.{Json, JsonObject} import weco.api.search.models.index.IndexedImage import weco.api.search.elasticsearch.{ColorQuery, ImageSimilarity} import weco.api.search.models._ -import weco.api.search.models.request.{ - ProductionDateSortRequest, - SortingOrder -} +import weco.api.search.models.request.{ProductionDateSortRequest, SortingOrder} import weco.api.search.rest.PaginationQuery import weco.api.search.elasticsearch.templateSearch.TemplateSearchRequest @@ -47,7 +44,8 @@ object ImagesRequestBuilder sortByScore = searchOptions.searchQuery.isDefined || searchOptions.color.isDefined, includes = Seq("display", "vectorValues.features"), - aggs = ImagesAggregationsBuilder.getAggregations(pairables, searchOptions.aggregations), + aggs = ImagesAggregationsBuilder + .getAggregations(pairables, searchOptions.aggregations), preFilter = unpairables.collect(buildImageFilterQuery), postFilter = Some( must( @@ -60,11 +58,12 @@ object ImagesRequestBuilder ) } - private def dateOrder(implicit - searchOptions: ImageSearchOptions - ): Option[SortingOrder] = - searchOptions.sortBy collectFirst { case ProductionDateSortRequest => - searchOptions.sortOrder + private def dateOrder( + implicit + searchOptions: ImageSearchOptions): Option[SortingOrder] = + searchOptions.sortBy collectFirst { + case ProductionDateSortRequest => + searchOptions.sortOrder } val buildImageFilterQuery: PartialFunction[ImageFilter, Query] = { diff --git a/search/src/main/scala/weco/api/search/services/ImagesService.scala b/search/src/main/scala/weco/api/search/services/ImagesService.scala index 71ba2367e..e7eb8be99 100644 --- a/search/src/main/scala/weco/api/search/services/ImagesService.scala +++ b/search/src/main/scala/weco/api/search/services/ImagesService.scala @@ -34,7 +34,8 @@ class ImagesService( ): Option[ImageAggregations] = ImageAggregations(searchResponse) - override protected val requestBuilder: ElasticsearchRequestBuilder[ImageSearchOptions] = + override protected val requestBuilder + : ElasticsearchRequestBuilder[ImageSearchOptions] = ImagesRequestBuilder def retrieveSimilarImages( diff --git a/search/src/main/scala/weco/api/search/services/WorksRequestBuilder.scala b/search/src/main/scala/weco/api/search/services/WorksRequestBuilder.scala index d1111653b..f984fa6a9 100644 --- a/search/src/main/scala/weco/api/search/services/WorksRequestBuilder.scala +++ b/search/src/main/scala/weco/api/search/services/WorksRequestBuilder.scala @@ -43,7 +43,8 @@ object WorksRequestBuilder sortByDate = dateOrder, sortByScore = searchOptions.searchQuery.isDefined, includes = Seq("display", "type"), - aggs = WorksAggregationsBuilder.getAggregations(pairables, searchOptions.aggregations), + aggs = WorksAggregationsBuilder + .getAggregations(pairables, searchOptions.aggregations), preFilter = (VisibleWorkFilter :: unpairables).collect(buildWorkFilterQuery), postFilter = Some( @@ -56,11 +57,12 @@ object WorksRequestBuilder ) } - private def dateOrder(implicit - searchOptions: WorkSearchOptions - ): Option[SortingOrder] = - searchOptions.sortBy collectFirst { case ProductionDateSortRequest => - searchOptions.sortOrder + private def dateOrder( + implicit + searchOptions: WorkSearchOptions): Option[SortingOrder] = + searchOptions.sortBy collectFirst { + case ProductionDateSortRequest => + searchOptions.sortOrder } val buildWorkFilterQuery: PartialFunction[WorkFilter, Query] = {