Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Remove redundancies in search by label queries (#2933)
- Loading branch information
1 parent
8f35d81
commit a333e34
Showing
5 changed files
with
118 additions
and
300 deletions.
There are no files selected for viewing
101 changes: 101 additions & 0 deletions
101
webapi/src/main/scala/org/knora/webapi/responders/v2/SearchQueries.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright © 2021 - 2023 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package org.knora.webapi.responders.v2 | ||
|
||
import org.knora.webapi.IRI | ||
import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Construct | ||
import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Select | ||
|
||
object SearchQueries { | ||
|
||
def selectCountByLabel( | ||
searchTerm: String, | ||
limitToProject: Option[IRI], | ||
limitToResourceClass: Option[IRI] | ||
): Select = | ||
Select( | ||
s"""|PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
|PREFIX knora-base: <http://www.knora.org/ontology/knora-base#> | ||
|SELECT (count(distinct ?resource) as ?count) | ||
|WHERE { | ||
| ?resource <http://jena.apache.org/text#query> (rdfs:label "$searchTerm") ; | ||
| a ?resourceClass . | ||
| ?resourceClass rdfs:subClassOf* knora-base:Resource . | ||
| ${limitToResourceClass.fold("")(resourceClass => s"?resourceClass rdfs:subClassOf* <$resourceClass> .")} | ||
| ${limitToProject.fold("")(project => s"?resource knora-base:attachedToProject <$project> .")} | ||
| FILTER NOT EXISTS { ?resource knora-base:isDeleted true . } | ||
|} | ||
|""".stripMargin | ||
) | ||
|
||
def constructSearchByLabel( | ||
searchTerm: String, | ||
limitToResourceClass: Option[IRI] = None, | ||
limitToProject: Option[IRI] = None, | ||
limit: Int, | ||
offset: Int = 0 | ||
): Construct = { | ||
val limitToClassOrProject = | ||
(limitToResourceClass, limitToProject) match { | ||
case (Some(cls), _) => s"?resourceClass rdfs:subClassOf* <$cls> ." | ||
case (_, Some(project)) => s"?resource knora-base:attachedToProject <$project> ." | ||
case _ => "" | ||
} | ||
Construct( | ||
s"""|PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> | ||
|PREFIX knora-base: <http://www.knora.org/ontology/knora-base#> | ||
|CONSTRUCT { | ||
| ?resource rdfs:label ?label ; | ||
| a knora-base:Resource ; | ||
| knora-base:isMainResource true ; | ||
| knora-base:isDeleted false ; | ||
| a ?resourceType ; | ||
| knora-base:attachedToUser ?resourceCreator ; | ||
| knora-base:hasPermissions ?resourcePermissions ; | ||
| knora-base:attachedToProject ?resourceProject ; | ||
| knora-base:creationDate ?creationDate ; | ||
| knora-base:lastModificationDate ?lastModificationDate ; | ||
| knora-base:hasValue ?valueObject ; | ||
| ?resourceValueProperty ?valueObject . | ||
| ?valueObject ?valueObjectProperty ?valueObjectValue . | ||
|} WHERE { | ||
| { | ||
| SELECT DISTINCT ?resource ?label | ||
| WHERE { | ||
| ?resource <http://jena.apache.org/text#query> (rdfs:label "$searchTerm") ; | ||
| a ?resourceClass ; | ||
| rdfs:label ?label . | ||
| $limitToClassOrProject | ||
| FILTER NOT EXISTS { ?resource knora-base:isDeleted true . } | ||
| } | ||
| ORDER BY ?resource | ||
| LIMIT $limit | ||
| OFFSET $offset | ||
| } | ||
| | ||
| ?resource a ?resourceType ; | ||
| knora-base:attachedToUser ?resourceCreator ; | ||
| knora-base:hasPermissions ?resourcePermissions ; | ||
| knora-base:attachedToProject ?resourceProject ; | ||
| knora-base:creationDate ?creationDate ; | ||
| rdfs:label ?label . | ||
| OPTIONAL { ?resource knora-base:lastModificationDate ?lastModificationDate . } | ||
| OPTIONAL { | ||
| ?resource ?resourceValueProperty ?valueObject . | ||
| ?resourceValueProperty rdfs:subPropertyOf* knora-base:hasValue . | ||
| ?valueObject a ?valueObjectType ; | ||
| ?valueObjectProperty ?valueObjectValue . | ||
| ?valueObjectType rdfs:subClassOf* knora-base:Value . | ||
| FILTER(?valueObjectType != knora-base:LinkValue) | ||
| FILTER NOT EXISTS { ?valueObject knora-base:isDeleted true . } | ||
| FILTER NOT EXISTS { ?valueObjectValue a knora-base:StandoffTag . } | ||
| } | ||
|} | ||
|""".stripMargin | ||
) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 0 additions & 116 deletions
116
...n/twirl/org/knora/webapi/messages/twirl/queries/sparql/v2/searchResourceByLabel.scala.txt
This file was deleted.
Oops, something went wrong.
57 changes: 0 additions & 57 deletions
57
...org/knora/webapi/messages/twirl/queries/sparql/v2/searchResourceByLabelSubQuery.scala.txt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.