mirror of
https://github.com/TheAnachronism/docspell.git
synced 2025-06-22 02:18:26 +00:00
Basic poc to search via custom query
This commit is contained in:
@ -145,8 +145,8 @@ trait Conversions {
|
||||
|
||||
def mkQuery(m: ItemSearch, account: AccountId): OItemSearch.Query =
|
||||
OItemSearch.Query(
|
||||
OItemSearch.Query.Fix(account, None),
|
||||
OItemSearch.Query.QueryCond(
|
||||
OItemSearch.Query.Fix(account, None, None),
|
||||
OItemSearch.Query.QueryForm(
|
||||
m.name,
|
||||
if (m.inbox) Seq(ItemState.Created)
|
||||
else ItemState.validStates.toList,
|
||||
|
@ -25,5 +25,9 @@ object QueryParam {
|
||||
|
||||
object QueryOpt extends OptionalQueryParamDecoderMatcher[QueryString]("q")
|
||||
|
||||
object Query extends OptionalQueryParamDecoderMatcher[String]("q")
|
||||
object Limit extends OptionalQueryParamDecoderMatcher[Int]("limit")
|
||||
object Offset extends OptionalQueryParamDecoderMatcher[Int]("offset")
|
||||
|
||||
object WithFallback extends OptionalQueryParamDecoderMatcher[Boolean]("withFallback")
|
||||
}
|
||||
|
@ -4,21 +4,20 @@ import cats.Monoid
|
||||
import cats.data.NonEmptyList
|
||||
import cats.effect._
|
||||
import cats.implicits._
|
||||
|
||||
import docspell.backend.BackendApp
|
||||
import docspell.backend.auth.AuthToken
|
||||
import docspell.backend.ops.OCustomFields.{RemoveValue, SetValue}
|
||||
import docspell.backend.ops.OFulltext
|
||||
import docspell.backend.ops.OItemSearch.Batch
|
||||
import docspell.backend.ops.OItemSearch.{Batch, Query}
|
||||
import docspell.common._
|
||||
import docspell.common.syntax.all._
|
||||
import docspell.query.ItemQueryParser
|
||||
import docspell.restapi.model._
|
||||
import docspell.restserver.Config
|
||||
import docspell.restserver.conv.Conversions
|
||||
import docspell.restserver.http4s.BinaryUtil
|
||||
import docspell.restserver.http4s.Responses
|
||||
import docspell.restserver.http4s.{QueryParam => QP}
|
||||
|
||||
import org.http4s.HttpRoutes
|
||||
import org.http4s.circe.CirceEntityDecoder._
|
||||
import org.http4s.circe.CirceEntityEncoder._
|
||||
@ -46,6 +45,33 @@ object ItemRoutes {
|
||||
resp <- Ok(Conversions.basicResult(res, "Task submitted"))
|
||||
} yield resp
|
||||
|
||||
case GET -> Root / "search" :? QP.Query(q) :? QP.Limit(limit) :? QP.Offset(
|
||||
offset
|
||||
) =>
|
||||
val query =
|
||||
q.map(ItemQueryParser.parse) match {
|
||||
case Some(Right(q)) =>
|
||||
Right(Query(Query.Fix(user.account, None, None), Query.QueryExpr(q)))
|
||||
case Some(Left(err)) =>
|
||||
Left(err)
|
||||
case None =>
|
||||
Right(Query(Query.Fix(user.account, None, None), Query.QueryForm.empty))
|
||||
}
|
||||
val li = limit.getOrElse(cfg.maxItemPageSize)
|
||||
val of = offset.getOrElse(0)
|
||||
query match {
|
||||
case Left(err) =>
|
||||
BadRequest(BasicResult(false, err))
|
||||
case Right(sq) =>
|
||||
for {
|
||||
items <- backend.itemSearch.findItems(cfg.maxNoteLength)(
|
||||
sq,
|
||||
Batch(of, li).restrictLimitTo(cfg.maxItemPageSize)
|
||||
)
|
||||
ok <- Ok(Conversions.mkItemList(items))
|
||||
} yield ok
|
||||
}
|
||||
|
||||
case req @ POST -> Root / "search" =>
|
||||
for {
|
||||
mask <- req.as[ItemSearch]
|
||||
|
Reference in New Issue
Block a user