Skip to content

Commit

Permalink
fix: Improve performance for Gravsearch queries (#2857)
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone authored Oct 2, 2023
1 parent 738ab1c commit 86cc4f2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

package org.knora.webapi.util.search
package org.knora.webapi.messages.util.search.gravsearch.transformers

import org.knora.webapi.CoreSpec
import org.knora.webapi.messages.IriConversions._
Expand Down Expand Up @@ -80,11 +80,11 @@ class SparqlTransformerSpec extends CoreSpec {
isDeletedStatement,
linkStatement
)
val optimisedPatterns = SparqlTransformer.optimiseIsDeletedWithFilter(patterns)
val optimisedPatterns = SparqlTransformer.optimiseIsDeletedWithMinus(patterns)
val expectedPatterns = Seq(
typeStatement,
linkStatement,
FilterNotExistsPattern(
MinusPattern(
Seq(
StatementPattern(
subj = QueryVariable("foo"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ final case class ConstructTransformer(
optimisedPatterns <-
ZIO.attempt(
SparqlTransformer.moveBindToBeginning(
SparqlTransformer.optimiseIsDeletedWithFilter(
SparqlTransformer.optimiseIsDeletedWithMinus(
SparqlTransformer.moveLuceneToBeginning(patterns)
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SelectTransformer(
limitInferenceToOntologies = limitInferenceToOntologies
)
override def optimiseQueryPatterns(patterns: Seq[QueryPattern]): Task[Seq[QueryPattern]] = ZIO.attempt {
moveBindToBeginning(optimiseIsDeletedWithFilter(moveLuceneToBeginning(patterns)))
moveBindToBeginning(optimiseIsDeletedWithMinus(moveLuceneToBeginning(patterns)))
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,13 @@ object SparqlTransformer {
createUniqueVariableFromStatement(baseStatement, "LinkValue")

/**
* Optimises a query by replacing `knora-base:isDeleted false` with a `FILTER NOT EXISTS` pattern
* Optimises a query by replacing `knora-base:isDeleted false` with a `MINUS` pattern
* placed at the end of the block.
*
* @param patterns the block of patterns to be optimised.
* @return the result of the optimisation.
*/
def optimiseIsDeletedWithFilter(patterns: Seq[QueryPattern]): Seq[QueryPattern] = {
def optimiseIsDeletedWithMinus(patterns: Seq[QueryPattern]): Seq[QueryPattern] = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance

// Separate the knora-base:isDeleted statements from the rest of the block.
Expand All @@ -107,18 +107,17 @@ object SparqlTransformer {
case _ => false
}

// Replace the knora-base:isDeleted statements with FILTER NOT EXISTS patterns.
val filterPatterns: Seq[FilterNotExistsPattern] = isDeletedPatterns.collect {
case statementPattern: StatementPattern =>
FilterNotExistsPattern(
Seq(
StatementPattern(
subj = statementPattern.subj,
pred = IriRef(OntologyConstants.KnoraBase.IsDeleted.toSmartIri),
obj = XsdLiteral(value = "true", datatype = OntologyConstants.Xsd.Boolean.toSmartIri)
)
// Replace the knora-base:isDeleted statements with MINUS patterns.
val filterPatterns: Seq[MinusPattern] = isDeletedPatterns.collect { case statementPattern: StatementPattern =>
MinusPattern(
Seq(
StatementPattern(
subj = statementPattern.subj,
pred = IriRef(OntologyConstants.KnoraBase.IsDeleted.toSmartIri),
obj = XsdLiteral(value = "true", datatype = OntologyConstants.Xsd.Boolean.toSmartIri)
)
)
)
}

otherPatterns ++ filterPatterns
Expand Down

0 comments on commit 86cc4f2

Please sign in to comment.