Skip to content

Commit

Permalink
refactor: Remove redundancies in search by label queries (#2933)
Browse files Browse the repository at this point in the history
  • Loading branch information
BalduinLandolt committed Nov 17, 2023
1 parent 8f35d81 commit a333e34
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 300 deletions.
@@ -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
)
}

}
Expand Up @@ -804,23 +804,15 @@ final case class SearchResponderV2Live(
limitToProject: Option[IRI],
limitToResourceClass: Option[SmartIri]
) = {
val searchPhrase: MatchStringWhileTyping = MatchStringWhileTyping(searchValue)
val searchTerm = MatchStringWhileTyping(searchValue).generateLiteralForLuceneIndexWithoutExactSequence
val countSparql = SearchQueries.selectCountByLabel(
searchTerm = searchTerm,
limitToProject = limitToProject,
limitToResourceClass = limitToResourceClass.map(_.toString)
)

for {
countSparql <-
ZIO.attempt(
sparql.v2.txt
.searchResourceByLabel(
searchTerm = searchPhrase,
limitToProject = limitToProject,
limitToResourceClass = limitToResourceClass.map(_.toString),
limit = 1,
offset = 0,
countQuery = true
)
)

countResponse <- triplestore.query(Select(countSparql))
countResponse <- triplestore.query(countSparql)

count <- // query response should contain one result with one row with the name "count"
ZIO
Expand Down Expand Up @@ -854,30 +846,18 @@ final case class SearchResponderV2Live(
targetSchema: ApiV2Schema,
requestingUser: UserADM
): Task[ReadResourcesSequenceV2] = {
val searchLimit = appConfig.v2.resourcesSequence.resultsPerPage
val searchOffset = offset * appConfig.v2.resourcesSequence.resultsPerPage
val searchTerm = MatchStringWhileTyping(searchValue).generateLiteralForLuceneIndexWithoutExactSequence

val searchResourceByLabelSparql =
limitToResourceClass match {
case None =>
Construct(
sparql.v2.txt.searchResourceByLabel(
searchTerm = MatchStringWhileTyping(searchValue),
limitToProject = limitToProject,
limitToResourceClass = limitToResourceClass.map(_.toString),
limit = appConfig.v2.resourcesSequence.resultsPerPage,
offset = offset * appConfig.v2.resourcesSequence.resultsPerPage,
countQuery = false
)
)
case Some(cls) =>
Construct(
sparql.v2.txt.searchResourceByLabelWithDefinedResourceClass(
searchTerm = MatchStringWhileTyping(searchValue),
limitToResourceClass = cls.toString,
limit = appConfig.v2.resourcesSequence.resultsPerPage,
offset = offset * appConfig.v2.resourcesSequence.resultsPerPage
)
)
}
SearchQueries.constructSearchByLabel(
searchTerm,
limitToResourceClass.map(_.toIri),
limitToProject,
searchLimit,
searchOffset
)

for {
searchResourceByLabelResponse <- triplestore.query(searchResourceByLabelSparql).flatMap(_.asExtended)
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit a333e34

Please sign in to comment.