Skip to content

Commit

Permalink
Apply auto-formatting rules
Browse files Browse the repository at this point in the history
  • Loading branch information
weco-bot committed Nov 27, 2024
1 parent f32eead commit 9eafe21
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 39 deletions.
23 changes: 11 additions & 12 deletions search/src/main/scala/weco/api/search/models/Aggregation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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]

Expand All @@ -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)

Expand All @@ -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
}

Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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(
Expand All @@ -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] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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] = {
Expand Down

0 comments on commit 9eafe21

Please sign in to comment.