Skip to content

Commit

Permalink
Make the search cursors work by default. Add explicit sort-by-score o…
Browse files Browse the repository at this point in the history
…ption
  • Loading branch information
fredex42 committed Jul 16, 2024
1 parent 2ca11de commit fed0a9f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ object PaginationParameters {
object OrderDateSchema {
val definition = EnumType(
"OrderDate",
Some("Which date field to use for ordering the content"),
Some("Which date field to use for ordering the content, or whether to search on document score"),
List(
EnumValue("score", Some("Ignore when the content was made or published and sort by relevance to the query parameters"), "score"),
EnumValue("published", Some("When the content was published to web"), "webPublicationDate"),
EnumValue("firstPublished", Some("When the first version of this content was published"), "fields.firstPublicationDate"),
EnumValue("lastModified", Some("The last time the content was modified prior to publication"), "fields.lastModified"),
Expand Down
39 changes: 25 additions & 14 deletions src/main/scala/com/gu/contentapi/porter/graphql/RootQuery.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import scala.concurrent.ExecutionContext.Implicits.global
import io.circe.generic.auto._
import org.slf4j.LoggerFactory

import scala.concurrent.Future

object RootQuery {
private val logger = LoggerFactory.getLogger(getClass)

Expand All @@ -36,20 +38,29 @@ object RootQuery {
Field("matchingAnyTag", ArticleEdge, Some("Content which matches any of the tags returned"),
arguments= ContentQueryParameters.AllContentQueryParameters,
resolve = { ctx=>
ctx.ctx.repo.marshalledDocs(ctx arg ContentQueryParameters.QueryString,
queryFields=ctx arg ContentQueryParameters.QueryFields,
atomId = None,
forChannel = ctx arg ContentQueryParameters.ChannelArg,
userTier = ctx.ctx.userTier,
tagIds = Some(ctx.value.nodes.map(_.id)),
excludeTags = ctx arg ContentQueryParameters.ExcludeTagArg,
sectionIds = ctx arg ContentQueryParameters.SectionArg,
excludeSections = ctx arg ContentQueryParameters.ExcludeSectionArg,
orderDate = ctx arg PaginationParameters.OrderDate,
orderBy = ctx arg PaginationParameters.OrderBy,
limit = ctx arg PaginationParameters.Limit,
cursor = ctx arg PaginationParameters.Cursor,
)
if(ctx.value.nodes.isEmpty) {
Future(Edge[Content](
0L,
None,
false,
Seq()
))
} else {
ctx.ctx.repo.marshalledDocs(ctx arg ContentQueryParameters.QueryString,
queryFields = ctx arg ContentQueryParameters.QueryFields,
atomId = None,
forChannel = ctx arg ContentQueryParameters.ChannelArg,
userTier = ctx.ctx.userTier,
tagIds = Some(ctx.value.nodes.map(_.id)),
excludeTags = ctx arg ContentQueryParameters.ExcludeTagArg,
sectionIds = ctx arg ContentQueryParameters.SectionArg,
excludeSections = ctx arg ContentQueryParameters.ExcludeSectionArg,
orderDate = ctx arg PaginationParameters.OrderDate,
orderBy = ctx arg PaginationParameters.OrderBy,
limit = ctx arg PaginationParameters.Limit,
cursor = ctx arg PaginationParameters.Cursor,
)
}
})
)
)
Expand Down
3 changes: 2 additions & 1 deletion src/main/scala/datastore/ElasticsearchRepo.scala
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ class ElasticsearchRepo(endpoint:ElasticNodeEndpoint, val defaultPageSize:Int=20

private def defaultingSortParam(orderDate:Option[String], orderBy:Option[SortOrder]): Sort = {
orderDate match {
case Some("score")=>ScoreSort(orderBy.getOrElse(SortOrder.DESC))
case Some(field)=>FieldSort(field, order = orderBy.getOrElse(SortOrder.DESC))
case None=>ScoreSort(orderBy.getOrElse(SortOrder.DESC))
case None=>FieldSort("webPublicationDate", order=orderBy.getOrElse(SortOrder.DESC))
}
}

Expand Down

0 comments on commit fed0a9f

Please sign in to comment.