diff --git a/src/main/scala/com/gu/contentapi/porter/graphql/PaginationParameters.scala b/src/main/scala/com/gu/contentapi/porter/graphql/PaginationParameters.scala index 0ba8614..10f9b62 100644 --- a/src/main/scala/com/gu/contentapi/porter/graphql/PaginationParameters.scala +++ b/src/main/scala/com/gu/contentapi/porter/graphql/PaginationParameters.scala @@ -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"), diff --git a/src/main/scala/com/gu/contentapi/porter/graphql/RootQuery.scala b/src/main/scala/com/gu/contentapi/porter/graphql/RootQuery.scala index 9e03b8c..93ae4f3 100644 --- a/src/main/scala/com/gu/contentapi/porter/graphql/RootQuery.scala +++ b/src/main/scala/com/gu/contentapi/porter/graphql/RootQuery.scala @@ -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) @@ -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, + ) + } }) ) ) diff --git a/src/main/scala/datastore/ElasticsearchRepo.scala b/src/main/scala/datastore/ElasticsearchRepo.scala index ed5d00e..c7bd8fb 100644 --- a/src/main/scala/datastore/ElasticsearchRepo.scala +++ b/src/main/scala/datastore/ElasticsearchRepo.scala @@ -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)) } }