Skip to content

Commit

Permalink
fix: search by label produces timeouts if too many resources match (D…
Browse files Browse the repository at this point in the history
  • Loading branch information
BalduinLandolt committed Apr 18, 2023
1 parent c472ff7 commit 78479d3
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ db-dump: ## Dump data from an env. Use as `make db_dump PW=database-password ENV
init-db-from-dump-file: ## init local database from a specified dump file. Use as `make init-db-from-dump-file DUMP=some-dump-file.trig`
@echo $@
@echo dump file: ${DUMP}
@docker compose -f docker-compose.yml up -d db
$(CURRENT_DIR)/webapi/scripts/wait-for-db.sh
@curl -X POST -H "Content-Type: application/sparql-update" -d "DROP ALL" -u "admin:test" "http://localhost:3030/knora-test"
@curl -X POST -H "Content-Type: application/trig" -T "${CURRENT_DIR}/${DUMP}" -u "admin:test" "http://localhost:3030/knora-test"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -914,19 +914,35 @@ final case class SearchResponderV2Live(
val searchPhrase: MatchStringWhileTyping = MatchStringWhileTyping(searchValue)

for {
// TODO-BL: this should be refactored and the old search-by-label code streamlined.
// I leave it for now, in case we have to revert, but I will create a follow-up task to refactor this.
searchResourceByLabelSparql <-
ZIO.attempt(
org.knora.webapi.messages.twirl.queries.sparql.v2.txt
.searchResourceByLabel(
searchTerm = searchPhrase,
limitToProject = limitToProject,
limitToResourceClass = limitToResourceClass.map(_.toString),
limit = appConfig.v2.resourcesSequence.resultsPerPage,
offset = offset * appConfig.v2.resourcesSequence.resultsPerPage,
countQuery = false
limitToResourceClass match {
case None =>
ZIO.attempt(
org.knora.webapi.messages.twirl.queries.sparql.v2.txt
.searchResourceByLabel(
searchTerm = searchPhrase,
limitToProject = limitToProject,
limitToResourceClass = limitToResourceClass.map(_.toString),
limit = appConfig.v2.resourcesSequence.resultsPerPage,
offset = offset * appConfig.v2.resourcesSequence.resultsPerPage,
countQuery = false
)
.toString()
)
.toString()
)
case Some(cls) =>
ZIO.attempt(
org.knora.webapi.messages.twirl.queries.sparql.v2.txt
.searchResourceByLabelWithDefinedResourceClass(
searchTerm = searchPhrase,
limitToResourceClass = cls.toString,
limit = appConfig.v2.resourcesSequence.resultsPerPage,
offset = offset * appConfig.v2.resourcesSequence.resultsPerPage
)
.toString()
)
}

searchResourceByLabelResponse <- triplestoreService.sparqlHttpExtendedConstruct(searchResourceByLabelSparql)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
@*
* Copyright © 2021 - 2023 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*@

@import org.knora.webapi.IRI
@import org.knora.webapi.util.ApacheLuceneSupport.MatchStringWhileTyping

@*
* Performs a search for resources by their label using SPARQL.
*
* @param searchTerm search terms.
* @param limitToResourceClass limit search to given resource class.
* @param limit maximum amount of resources to be returned.
* @param offset offset to be used for paging.
*@
@(searchTerm: MatchStringWhileTyping,
limitToResourceClass: IRI,
limit: Int,
offset: Int)

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 .

?resource 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.generateLiteralForLuceneIndexWithoutExactSequence") .

?resource a ?resourceClass ;
rdfs:label ?label .
?resourceClass rdfs:subClassOf* <@limitToResourceClass> .

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 .
}
}

}

0 comments on commit 78479d3

Please sign in to comment.