Skip to content

Commit

Permalink
Merge pull request #132 from SwissDataScienceCenter/namespace-term
Browse files Browse the repository at this point in the history
Support for `namespace:*` search term in user query
  • Loading branch information
eikek authored May 21, 2024
2 parents 4f10d3a + deb2e55 commit 0b795e8
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ enum Field:
case Type
case Role
case Keyword
case Namespace

val name: String = Strings.lowerFirst(productPrefix)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package io.renku.search.query

import cats.data.NonEmptyList

import io.renku.search.model.Namespace
import io.renku.search.model.projects.Visibility
import io.renku.search.model.{EntityType, Keyword, MemberRole}

Expand All @@ -39,6 +40,8 @@ enum FieldTerm(val field: Field, val cmp: Comparison):
extends FieldTerm(Field.Role, Comparison.Is)
case KeywordIs(values: NonEmptyList[Keyword])
extends FieldTerm(Field.Keyword, Comparison.Is)
case NamespaceIs(values: NonEmptyList[Namespace])
extends FieldTerm(Field.Namespace, Comparison.Is)

private[query] def asString =
val value = this match
Expand All @@ -55,6 +58,7 @@ enum FieldTerm(val field: Field, val cmp: Comparison):
case CreatedByIs(values) => FieldTerm.nelToString(values)
case RoleIs(values) => FieldTerm.nelToString(values.map(_.name))
case KeywordIs(values) => FieldTerm.nelToString(values.map(_.value))
case NamespaceIs(values) => FieldTerm.nelToString(values.map(_.value))

s"${field.name}${cmp.asString}${value}"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ package io.renku.search.query.parse
import cats.data.NonEmptyList
import cats.parse.{Parser as P, Parser0 as P0}

import io.renku.search.model.*
import io.renku.search.model.projects.Visibility
import io.renku.search.model.{EntityType, Keyword, MemberRole}
import io.renku.search.query.*

private[query] object QueryParser {
Expand Down Expand Up @@ -106,6 +106,7 @@ private[query] object QueryParser {
((field <* is) ~ values).map { case (f, v) =>
f match
case Field.Name => FieldTerm.NameIs(v)
case Field.Namespace => FieldTerm.NamespaceIs(v.map(Namespace(_)))
case Field.Id => FieldTerm.IdIs(v)
case Field.Slug => FieldTerm.SlugIs(v)
case Field.CreatedBy => FieldTerm.CreatedByIs(v)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ import cats.Order as CatsOrder
import cats.data.NonEmptyList
import cats.syntax.all.*

import io.renku.search.model.*
import io.renku.search.model.projects.Visibility
import io.renku.search.model.{CommonGenerators, MemberRole, ModelGenerators}
import io.renku.search.query.parse.QueryUtil
import org.scalacheck.Gen
import org.scalacheck.cats.implicits.*
Expand Down Expand Up @@ -121,6 +121,9 @@ object QueryGenerators:
val nameTerm: Gen[FieldTerm] =
stringValues.map(FieldTerm.NameIs(_))

val namespaceTerm: Gen[FieldTerm] =
stringValues.map(vs => FieldTerm.NamespaceIs(vs.map(Namespace(_))))

val slugTerm: Gen[FieldTerm] =
stringValues.map(FieldTerm.SlugIs(_))

Expand Down Expand Up @@ -157,6 +160,7 @@ object QueryGenerators:
Gen.oneOf(
projectIdTerm,
nameTerm,
namespaceTerm,
slugTerm,
createdByTerm,
visibilityTerm,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,13 @@ trait LuceneQueryEncoders:
)
}

given namespaceIs[F[_]: Applicative]: SolrTokenEncoder[F, FieldTerm.NamespaceIs] =
SolrTokenEncoder.basic { case FieldTerm.NamespaceIs(values) =>
SolrQuery(
SolrToken.orFieldIs(SolrField.namespace, values.map(SolrToken.fromNamespace))
)
}

given created[F[_]: Monad]: SolrTokenEncoder[F, FieldTerm.Created] =
val createdIs = SolrToken.fieldIs(SolrField.creationDate, _)
SolrTokenEncoder.create[F, FieldTerm.Created] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ object SolrToken:
case Comparison.GreaterThan => ">"
case Comparison.LowerThan => "<"

def fromNamespace(ns: Namespace): SolrToken =
fromString(ns.value)

def contentAll(text: String): SolrToken =
val terms: Seq[SolrToken] = text.split("\\s+").map(_.trim).toSeq
s"${SolrField.contentAll.name}:${terms.fuzzy}"
Expand All @@ -72,7 +75,7 @@ object SolrToken:
values.map(fieldIs(field, _)).toList.foldOr

def namespaceIs(ns: Namespace): SolrToken =
fieldIs(SolrField.namespace, fromString(ns.value))
fieldIs(SolrField.namespace, fromNamespace(ns))

def createdDateIs(date: Instant): SolrToken =
fieldIs(SolrField.creationDate, fromInstant(date))
Expand Down

0 comments on commit 0b795e8

Please sign in to comment.