Skip to content

Commit

Permalink
Simplify resources search to only accept keywords
Browse files Browse the repository at this point in the history
  • Loading branch information
shinyhappydan committed Jan 17, 2024
1 parent 7ecbd2e commit 861585b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ final case class QueryBuilder private[client] (private val query: JsonObject) {
params.updatedBy.map(term(nxv.updatedBy.prefix, _)) ++
range(nxv.updatedAt.prefix, params.updatedAt) ++
params.tag.map(term(nxv.tags.prefix, _)) ++
params.fileUserMetadata.toList.flatMap(_.keywords).map { case (key, value) =>
params.keywords.map { case (key, value) =>
term(s"keywords.$key", value)
},
mustNotTerms = typesTerms(params.typeOperator.negate, excludeTypes),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,15 @@ package ch.epfl.bluebrain.nexus.delta.plugins.elasticsearch.model

import akka.http.scaladsl.unmarshalling.{FromStringUnmarshaller, Unmarshaller}
import ch.epfl.bluebrain.nexus.delta.kernel.search.TimeRange
import ch.epfl.bluebrain.nexus.delta.plugins.elasticsearch.model.ResourcesSearchParams.{FileUserMetadata, Type, TypeOperator}
import ch.epfl.bluebrain.nexus.delta.plugins.elasticsearch.model.ResourcesSearchParams.{Type, TypeOperator}
import ch.epfl.bluebrain.nexus.delta.rdf.IriOrBNode.Iri
import ch.epfl.bluebrain.nexus.delta.sdk.marshalling.QueryParamsUnmarshalling.{iriFromStringUnmarshaller, iriVocabFromStringUnmarshaller => iriUnmarshaller}
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.ProjectContext
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Identity.Subject
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag
import ch.epfl.bluebrain.nexus.delta.sourcing.model.{Label, ResourceRef}
import io.circe.Decoder
import io.circe.generic.semiauto.deriveDecoder
import io.circe.{parser, Decoder}

import scala.util.{Failure, Success}

/**
* Search parameters for any generic resource type.
Expand Down Expand Up @@ -53,7 +51,7 @@ final case class ResourcesSearchParams(
updatedAt: TimeRange = TimeRange.Anytime,
types: List[Type] = List.empty,
typeOperator: TypeOperator = TypeOperator.Or,
fileUserMetadata: Option[FileUserMetadata] = None,
keywords: Map[Label, String] = Map.empty,
schema: Option[ResourceRef] = None,
q: Option[String] = None,
tag: Option[UserTag] = None
Expand All @@ -71,14 +69,7 @@ object ResourcesSearchParams {

object FileUserMetadata {

implicit val decoder: Decoder[FileUserMetadata] = deriveDecoder[FileUserMetadata]
implicit val fromStringUnmarshaller: FromStringUnmarshaller[FileUserMetadata] =
Unmarshaller.strict[String, FileUserMetadata] { str =>
parser.parse(str).flatMap(_.as[FileUserMetadata]).toTry match {
case Failure(exception) => throw exception
case Success(value) => value
}
}
implicit val decoder: Decoder[FileUserMetadata] = deriveDecoder[FileUserMetadata]
}

sealed trait TypeOperator extends Product with Serializable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import akka.http.scaladsl.server.{Directive, Directive0, Directive1, MalformedQu
import akka.http.scaladsl.unmarshalling.{FromStringUnmarshaller, Unmarshaller}
import ch.epfl.bluebrain.nexus.delta.plugins.elasticsearch.model.ResourcesSearchParams
import ch.epfl.bluebrain.nexus.delta.plugins.elasticsearch.model.ResourcesSearchParams.TypeOperator.Or
import ch.epfl.bluebrain.nexus.delta.plugins.elasticsearch.model.ResourcesSearchParams.{FileUserMetadata, Type, TypeOperator}
import ch.epfl.bluebrain.nexus.delta.plugins.elasticsearch.model.ResourcesSearchParams.{Type, TypeOperator}
import ch.epfl.bluebrain.nexus.delta.rdf.IriOrBNode.Iri
import ch.epfl.bluebrain.nexus.delta.sdk.directives.{DeltaSchemeDirectives, UriDirectives}
import ch.epfl.bluebrain.nexus.delta.sdk.marshalling.QueryParamsUnmarshalling.IriBase
import ch.epfl.bluebrain.nexus.delta.sdk.model.BaseUri
import ch.epfl.bluebrain.nexus.delta.sdk.model.search.{Sort, SortList}
import ch.epfl.bluebrain.nexus.delta.sdk.projects.model.ProjectContext
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ResourceRef
import ch.epfl.bluebrain.nexus.delta.sourcing.model.{Label, ResourceRef}
import io.circe.parser

trait ElasticSearchViewsDirectives extends UriDirectives {

Expand All @@ -29,8 +30,17 @@ trait ElasticSearchViewsDirectives extends UriDirectives {
private def types(implicit um: FromStringUnmarshaller[Type]): Directive1[List[Type]] =
parameter("type".as[Type].*).map(_.toList.reverse)

private def userFileMetadata: Directive1[Option[FileUserMetadata]] =
parameter("metadata".as[FileUserMetadata].?)
implicit val keywordsFromStringUnmarshaller: FromStringUnmarshaller[Map[Label, String]] = Unmarshaller.strict {
string =>
parser.parse(string).flatMap(_.as[Map[Label, String]]) match {
case Left(e) => throw e
case Right(value) => value
}
}

private def userFileMetadata: Directive1[Map[Label, String]] = {
parameter("keywords".as[Map[Label, String]].withDefault(Map.empty[Label, String]))
}

private def typeOperator(implicit um: FromStringUnmarshaller[TypeOperator]): Directive1[TypeOperator] = {
parameter("typeOperator".as[TypeOperator].?[TypeOperator](Or))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ class FilesSpec extends BaseIntegrationSpec {
}

private def queryForFilesWithKeywords(keywords: (String, String)*): IO[List[Json]] = {
val metadata = UrlUtils.encode(Json.obj("keywords" := keywords.toMap.asJson).noSpaces)
val encodedKeywords = UrlUtils.encode(keywords.toMap.asJson.noSpaces)
deltaClient
.getJson[Json](s"/files/$projectRef?metadata=$metadata", Writer)
.getJson[Json](s"/files/$projectRef?keywords=$encodedKeywords", Writer)
.map { json =>
json.hcursor.downField("_results").as[List[Json]].rightValue
}
Expand Down

0 comments on commit 861585b

Please sign in to comment.