Skip to content

Commit

Permalink
Add tags to indices for resources, files, schemas (#4320)
Browse files Browse the repository at this point in the history
* Add tags to indices for resources, files and schemas

* Add Sparql test to query default view by tag
  • Loading branch information
dantb committed Oct 12, 2023
1 parent 09f3df6 commit 093e468
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
"type": "long",
"copy_to": "_all_fields"
},
"_tags": {
"type": "text",
"copy_to": "_all_fields"
},
"_deprecated": {
"type": "boolean",
"copy_to": "_all_fields"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
package ch.epfl.bluebrain.nexus.delta.plugins.storage.files.model

import ch.epfl.bluebrain.nexus.delta.plugins.storage.files.Files
import ch.epfl.bluebrain.nexus.delta.plugins.storage.files.model.File.Metadata
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.StoragesConfig.StorageTypeConfig
import ch.epfl.bluebrain.nexus.delta.plugins.storage.storages.model.StorageType
import ch.epfl.bluebrain.nexus.delta.rdf.IriOrBNode.Iri
import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.contexts
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.ContextValue
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.JsonLdContext.keywords
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.encoder.JsonLdEncoder
import ch.epfl.bluebrain.nexus.delta.sdk.ResourceShift
import ch.epfl.bluebrain.nexus.delta.sdk.jsonld.JsonLdContent
import ch.epfl.bluebrain.nexus.delta.sdk.model.{BaseUri, IdSegmentRef, Tags}
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ResourceRef
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag
import io.circe.syntax._
import io.circe.{Encoder, Json}

Expand All @@ -37,32 +41,42 @@ final case class File(
storageType: StorageType,
attributes: FileAttributes,
tags: Tags
)
) {
def metadata: Metadata = Metadata(tags.tags)
}

object File {

implicit def fileEncoder(implicit config: StorageTypeConfig): Encoder.AsObject[File] =
Encoder.encodeJsonObject.contramapObject { file =>
implicit val storageType: StorageType = file.storageType
val storageJson = Json.obj(
keywords.id -> file.storage.iri.asJson,
keywords.tpe -> storageType.iri.asJson,
"_rev" -> file.storage.rev.asJson
)
file.attributes.asJsonObject.add("_storage", storageJson)
}
final case class Metadata(tags: List[UserTag])

implicit def fileEncoder(implicit config: StorageTypeConfig): Encoder[File] = { file =>
implicit val storageType: StorageType = file.storageType
val storageJson = Json.obj(
keywords.id -> file.storage.iri.asJson,
keywords.tpe -> storageType.iri.asJson,
"_rev" -> file.storage.rev.asJson
)
file.attributes.asJson.mapObject(_.add("_storage", storageJson))
}

implicit def fileJsonLdEncoder(implicit config: StorageTypeConfig): JsonLdEncoder[File] =
JsonLdEncoder.computeFromCirce(_.id, Files.context)

type Shift = ResourceShift[FileState, File, Nothing]
implicit private val fileMetadataEncoder: Encoder[Metadata] = { m =>
Json.obj("_tags" -> m.tags.asJson)
}

implicit val fileMetadataJsonLdEncoder: JsonLdEncoder[Metadata] =
JsonLdEncoder.computeFromCirce(ContextValue(contexts.metadata))

type Shift = ResourceShift[FileState, File, Metadata]

def shift(files: Files)(implicit baseUri: BaseUri, config: StorageTypeConfig): Shift =
ResourceShift.apply[FileState, File](
ResourceShift.withMetadata[FileState, File, Metadata](
Files.entityType,
(ref, project) => files.fetch(IdSegmentRef(ref), project),
state => state.toResource,
value => JsonLdContent(value, value.value.asJson, None)
value => JsonLdContent(value, value.value.asJson, Some(value.value.metadata))
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ object Vocabulary {
val score = Metadata("score")
val self = Metadata("self")
val source = Metadata("source")
val tags = Metadata("tags")
val tokenEndpoint = Metadata("tokenEndpoint")
val total = Metadata("total")
val types = Metadata("types")
Expand Down Expand Up @@ -216,7 +217,6 @@ object Vocabulary {
val acls = contexts + "acls.json"
val aclsMetadata = contexts + "acls-metadata.json"
val error = contexts + "error.json"
val validation = contexts + "validation.json"
val identities = contexts + "identities.json"
val metadata = contexts + "metadata.json"
val offset = contexts + "offset.json"
Expand All @@ -236,10 +236,11 @@ object Vocabulary {
val search = contexts + "search.json"
val schemasMetadata = contexts + "schemas-metadata.json"
val shacl = contexts + "shacl-20170720.json"
val statistics = contexts + "statistics.json"
val supervision = contexts + "supervision.json"
val tags = contexts + "tags.json"
val validation = contexts + "validation.json"
val version = contexts + "version.json"
val statistics = contexts + "statistics.json"

}

Expand Down
4 changes: 4 additions & 0 deletions delta/sdk/src/main/resources/contexts/metadata.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"@context": {
"_rev": "https://bluebrain.github.io/nexus/vocabulary/rev",
"_tags": {
"@id": "https://bluebrain.github.io/nexus/vocabulary/tags",
"@container": "@set"
},
"_deprecated": "https://bluebrain.github.io/nexus/vocabulary/deprecated",
"_createdAt": {
"@id": "https://bluebrain.github.io/nexus/vocabulary/createdAt",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import io.circe.Json
import monix.bio.{IO, Task, UIO}

/**
* Aggregates the different [[ResourceShift]] to perform operations on resources indepently of their types
* Aggregates the different [[ResourceShift]] to perform operations on resources independently of their types
*/
trait ResourceShifts {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final case class Tags(value: Map[UserTag, Int]) extends AnyVal {
def contains(tag: UserTag): Boolean = value.contains(tag)
def +(tag: (UserTag, Int)): Tags = Tags(value + tag)
def -(tag: UserTag): Tags = Tags(value - tag)
def tags: List[UserTag] = value.keys.toList
}

object Tags {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ch.epfl.bluebrain.nexus.delta.sdk.resources.model

import ch.epfl.bluebrain.nexus.delta.rdf.IriOrBNode.Iri
import ch.epfl.bluebrain.nexus.delta.rdf.RdfError
import ch.epfl.bluebrain.nexus.delta.rdf.Vocabulary.contexts
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.api.{JsonLdApi, JsonLdOptions}
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.context.{ContextValue, RemoteContextResolution}
import ch.epfl.bluebrain.nexus.delta.rdf.jsonld.encoder.JsonLdEncoder
Expand All @@ -10,9 +11,12 @@ import ch.epfl.bluebrain.nexus.delta.sdk.ResourceShift
import ch.epfl.bluebrain.nexus.delta.sdk.jsonld.JsonLdContent
import ch.epfl.bluebrain.nexus.delta.sdk.model.{BaseUri, IdSegmentRef, Tags}
import ch.epfl.bluebrain.nexus.delta.sdk.resources.Resources
import ch.epfl.bluebrain.nexus.delta.sdk.resources.model.Resource.Metadata
import ch.epfl.bluebrain.nexus.delta.sdk.syntax._
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag
import ch.epfl.bluebrain.nexus.delta.sourcing.model.{ProjectRef, ResourceRef}
import io.circe.Json
import io.circe.syntax.EncoderOps
import io.circe.{Encoder, Json}
import monix.bio.IO

/**
Expand Down Expand Up @@ -41,10 +45,14 @@ final case class Resource(
source: Json,
compacted: CompactedJsonLd,
expanded: ExpandedJsonLd
)
) {
def metadata: Metadata = Metadata(tags.tags)
}

object Resource {

final case class Metadata(tags: List[UserTag])

implicit val resourceJsonLdEncoder: JsonLdEncoder[Resource] =
new JsonLdEncoder[Resource] {

Expand All @@ -62,13 +70,20 @@ object Resource {
value.source.topContextValueOrEmpty
}

type Shift = ResourceShift[ResourceState, Resource, Nothing]
implicit private val fileMetadataEncoder: Encoder[Metadata] = { m =>
Json.obj("_tags" -> m.tags.asJson)
}

implicit val fileMetadataJsonLdEncoder: JsonLdEncoder[Metadata] =
JsonLdEncoder.computeFromCirce(ContextValue(contexts.metadata))

type Shift = ResourceShift[ResourceState, Resource, Metadata]

def shift(resources: Resources)(implicit baseUri: BaseUri): Shift =
ResourceShift.apply[ResourceState, Resource](
ResourceShift.withMetadata[ResourceState, Resource, Metadata](
Resources.entityType,
(ref, project) => resources.fetch(IdSegmentRef(ref), project, None),
state => state.toResource,
value => JsonLdContent(value, value.value.source, None)
value => JsonLdContent(value, value.value.source, Some(value.value.metadata))
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ import ch.epfl.bluebrain.nexus.delta.sdk.model.{BaseUri, IdSegmentRef, Tags}
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.Schemas
import ch.epfl.bluebrain.nexus.delta.sdk.syntax._
import ch.epfl.bluebrain.nexus.delta.sourcing.model.ProjectRef
import io.circe.Json
import io.circe.{Encoder, Json}
import monix.bio.IO
import ch.epfl.bluebrain.nexus.delta.kernel.effect.migration._
import ch.epfl.bluebrain.nexus.delta.sdk.schemas.model.Schema.Metadata
import ch.epfl.bluebrain.nexus.delta.sourcing.model.Tag.UserTag
import io.circe.syntax.EncoderOps

/**
* A schema representation
Expand Down Expand Up @@ -67,10 +70,13 @@ final case class Schema(
Graph.empty(id).add(triples)
}

def metadata: Metadata = Metadata(tags.tags)
}

object Schema {

final case class Metadata(tags: List[UserTag])

implicit val schemaJsonLdEncoder: JsonLdEncoder[Schema] =
new JsonLdEncoder[Schema] {

Expand All @@ -88,14 +94,21 @@ object Schema {
value.source.topContextValueOrEmpty.merge(ContextValue(contexts.shacl))
}

type Shift = ResourceShift[SchemaState, Schema, Nothing]
implicit private val fileMetadataEncoder: Encoder[Metadata] = { m =>
Json.obj("_tags" -> m.tags.asJson)
}

implicit val fileMetadataJsonLdEncoder: JsonLdEncoder[Metadata] =
JsonLdEncoder.computeFromCirce(ContextValue(contexts.metadata))

type Shift = ResourceShift[SchemaState, Schema, Metadata]

def shift(schemas: Schemas)(implicit baseUri: BaseUri): Shift =
ResourceShift.apply[SchemaState, Schema](
ResourceShift.withMetadata[SchemaState, Schema, Metadata](
Schemas.entityType,
(ref, project) => schemas.fetch(IdSegmentRef(ref), project).toBIO[SchemaRejection],
state => state.toResource,
value => JsonLdContent(value, value.value.source, None)
value => JsonLdContent(value, value.value.source, Some(value.value.metadata))
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"_deprecated": false,
"_project": "{{deltaUri}}/projects/{{org2}}/{{proj3}}",
"_rev": 2,
"_tags": ["v1.0.1"],
"_schemaProject": "{{deltaUri}}/projects/{{org2}}/{{proj3}}",
"_updatedBy": "{{deltaUri}}/realms/{{realm}}/users/{{user}}"
},
Expand All @@ -47,6 +48,7 @@
"_deprecated": false,
"_project": "{{deltaUri}}/projects/{{org1}}/{{proj1}}",
"_rev": 2,
"_tags": ["v1.0.0"],
"_schemaProject": "{{deltaUri}}/projects/{{org1}}/{{proj1}}",
"_updatedBy": "{{deltaUri}}/realms/{{realm}}/users/{{user}}"
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"head": {
"vars": [
"s"
]
},
"results": {
"bindings": [
{
"s": {
"type": "uri",
"value": "https://bbp.epfl.ch/nexus/v0/data/bbp/experiment/patchedcell/v0.1.0/010b8ecb-21ac-4987-8faa-91f1274e656d"
}
},
{
"s": {
"type": "uri",
"value": "https://bbp.epfl.ch/nexus/v0/data/bbp/experiment/patchedcell/v0.1.0/03e9025f-35ab-42ca-ac4a-5da2701b93f3"
}
},
{
"s": {
"type": "uri",
"value": "https://bbp.epfl.ch/nexus/v0/data/bbp/experiment/patchedcell/v0.1.0/048bf2a9-c3e4-4401-bb1e-61f5fcacb85b"
}
},
{
"s": {
"type": "uri",
"value": "https://bbp.epfl.ch/nexus/v0/data/bbp/experiment/patchedcell/v0.1.0/04e5f081-26fb-49ea-9d38-da7ef49d9e9f"
}
},
{
"s": {
"type": "uri",
"value": "https://bbp.epfl.ch/nexus/v0/data/bbp/experiment/patchedcell/v0.1.0/08825325-0a84-4924-839b-baabcd9bdbb8"
}
}
]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,24 @@ class SparqlViewsSpec extends BaseSpec with EitherValuable with CirceEq {
}
}

val byTagQuery =
"""
|prefix nxv: <https://bluebrain.github.io/nexus/vocabulary/>
|
|select ?s where {
| ?s nxv:tags "one"
|}
|order by ?s
""".stripMargin

"search by tag in SPARQL endpoint in project 1 with default view" in eventually {
deltaClient.sparqlQuery[Json](s"/views/$fullId/nxv:defaultSparqlIndex/sparql", byTagQuery, ScoobyDoo) {
(json, response) =>
response.status shouldEqual StatusCodes.OK
json shouldEqual jsonContentOf("/kg/views/sparql-search-response-tagged.json")
}
}

"search instances in SPARQL endpoint in project 1 with custom SparqlView after tags added" in {
eventually {
deltaClient.sparqlQuery[Json](s"/views/$fullId/test-resource:cell-view/sparql", query, ScoobyDoo) {
Expand Down

0 comments on commit 093e468

Please sign in to comment.