Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3325 api exposing category values #176

Merged
merged 2 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions app/models/ElasticRetriever.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import scala.concurrent.Future
import scala.util.Try
import com.sksamuel.elastic4s.requests.searches.sort.FieldSort
import com.sksamuel.elastic4s.requests.searches.term.TermQuery
import com.sksamuel.elastic4s.handlers.index.Search

class ElasticRetriever @Inject() (
client: ElasticClient,
Expand Down Expand Up @@ -557,6 +558,27 @@ class ElasticRetriever @Inject() (
val filterQueries = boolQuery().must() :: Nil
val fnQueries = boolQuery().should(Seq(fuzzyQueryFn) ++ exactQueryFn) :: Nil
val mainQuery = boolQuery().must(fnQueries ::: filterQueries)
val aggQuery = termsAgg("categories", "category.keyword").size(1000)

val searchFacetCategories = client
.execute {
val aggregations = search(esIndices)
.aggs(aggQuery)
.trackTotalHits(true)
logger.trace(client.show(aggregations))
aggregations
}
.map { case (aggregations) =>
val aggs = (Json.parse(aggregations.body.get) \ "aggregations" \ "categories" \ "buckets")
.validate[Seq[SearchFacetsCategory]] match {
case JsSuccess(value, _) => value
case JsError(errors) =>
logger.error(errors.mkString("", " | ", ""))
Seq.empty
}
aggs
}
.await

if (qString.nonEmpty) {
client
Expand All @@ -581,10 +603,10 @@ class ElasticRetriever @Inject() (
Seq.empty
}

SearchFacetsResults(sresults, hits.result.totalHits)
SearchFacetsResults(sresults, hits.result.totalHits, searchFacetCategories)
}
} else {
Future.successful(SearchFacetsResults.empty)
Future.successful(SearchFacetsResults(Seq.empty, 0, searchFacetCategories))
}
}

Expand Down
5 changes: 3 additions & 2 deletions app/models/GQLSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,11 @@ object GQLSchema {
"facets",
searchFacetsResultsGQLImp,
description = Some("Search facets"),
arguments = queryString :: entityNames :: pageArg :: Nil,
arguments = optQueryString :: entityNames :: pageArg :: Nil,
resolve = ctx => {
val queryString = ctx.arg(optQueryString).getOrElse("")
val entities = ctx.arg(entityNames).getOrElse(Seq.empty)
ctx.ctx.searchFacets(ctx.arg(queryString), ctx.arg(pageArg), entities)
ctx.ctx.searchFacets(queryString, ctx.arg(pageArg), entities)
}
),
Field(
Expand Down
29 changes: 14 additions & 15 deletions app/models/entities/SearchFacetsResults.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,7 @@ import play.api.libs.json._
import play.api.libs.json.Reads._
import play.api.libs.functional.syntax._

case class SearchFacetsResultAggCategory(name: String, total: Long)

case class SearchFacetsResultAggEntity(
name: String,
total: Long,
categories: Seq[SearchFacetsResultAggCategory]
)
case class SearchFacetsCategory(name: String, total: Long)

case class Facet(
label: String,
Expand All @@ -19,8 +13,6 @@ case class Facet(
datasourceId: Option[String]
)

case class SearchFacetsResultAggs(total: Long, entities: Seq[SearchFacetsResultAggEntity])

case class SearchFacetsResult(
id: String,
label: String,
Expand All @@ -33,18 +25,25 @@ case class SearchFacetsResult(

case class SearchFacetsResults(
hits: Seq[SearchFacetsResult],
total: Long
total: Long,
categories: Seq[SearchFacetsCategory]
)

object SearchFacetsResults {
val empty: SearchFacetsResults = SearchFacetsResults(Seq.empty, 0)
implicit val searchFacetsCategoryImpW: OWrites[SearchFacetsCategory] =
Json.writes[models.entities.SearchFacetsCategory]

implicit val FacetF: OFormat[Facet] = Json.format[Facet]
implicit val facetF: OFormat[Facet] = Json.format[Facet]

implicit val SearchFacetsResultImpW: OWrites[SearchFacetsResult] =
implicit val searchFacetsResultImpW: OWrites[SearchFacetsResult] =
Json.writes[models.entities.SearchFacetsResult]

implicit val SearchFacetsResultImpR: Reads[models.entities.SearchFacetsResult] =
implicit val searchFacetsCategoryImpR: Reads[SearchFacetsCategory] = (
(__ \ "key").read[String] and
(__ \ "doc_count").read[Long]
)(SearchFacetsCategory.apply _)

implicit val searchFacetsResultImpR: Reads[models.entities.SearchFacetsResult] =
((__ \ "_id").read[String] and
(__ \ "_source" \ "label").read[String] and
(__ \ "_source" \ "category").read[String] and
Expand All @@ -59,6 +58,6 @@ object SearchFacetsResults {
case None => Seq.empty[String]
})(SearchFacetsResult.apply _)

implicit val mSearchFacetsResultsImpW: OFormat[SearchFacetsResults] =
implicit val searchFacetsResultsImpW: OFormat[SearchFacetsResults] =
Json.format[models.entities.SearchFacetsResults]
}
9 changes: 9 additions & 0 deletions app/models/gql/Objects.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1235,6 +1235,9 @@ object Objects extends Logging {
)
)

implicit val searchFacetsCategoryImp: ObjectType[Backend, SearchFacetsCategory] =
deriveObjectType[Backend, SearchFacetsCategory]()

val searchResultsGQLImp: ObjectType[Backend, SearchResults] = ObjectType(
"SearchResults",
"Search results",
Expand Down Expand Up @@ -1275,6 +1278,12 @@ object Objects extends Logging {
LongType,
description = Some("Total number or results given a entity filter"),
resolve = _.value.total
),
Field(
"categories",
ListType(searchFacetsCategoryImp),
description = Some("Categories"),
resolve = _.value.categories
)
)
)
Expand Down