From a48504debbf888ec16028043929226c1de22b63a Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Tue, 2 Mar 2021 21:09:31 +0100 Subject: [PATCH] Specificly search for field id vs name --- .../src/main/scala/docspell/query/ItemQuery.scala | 2 ++ .../scala/docspell/query/internal/ExprUtil.scala | 2 ++ .../docspell/query/internal/SimpleExprParser.scala | 10 ++++++++-- .../store/qb/generator/ItemQueryGenerator.scala | 12 +++++++++--- .../scala/docspell/store/queries/QueryWildcard.scala | 7 +++++++ modules/webapp/src/main/elm/Comp/SearchMenu.elm | 2 +- modules/webapp/src/main/elm/Data/ItemQuery.elm | 4 ++++ 7 files changed, 33 insertions(+), 6 deletions(-) diff --git a/modules/query/src/main/scala/docspell/query/ItemQuery.scala b/modules/query/src/main/scala/docspell/query/ItemQuery.scala index ec824ecb..8160da7d 100644 --- a/modules/query/src/main/scala/docspell/query/ItemQuery.scala +++ b/modules/query/src/main/scala/docspell/query/ItemQuery.scala @@ -103,6 +103,8 @@ object ItemQuery { final case class CustomFieldMatch(name: String, op: Operator, value: String) extends Expr + final case class CustomFieldIdMatch(id: String, op: Operator, value: String) + extends Expr final case class Fulltext(query: String) extends Expr diff --git a/modules/query/src/main/scala/docspell/query/internal/ExprUtil.scala b/modules/query/src/main/scala/docspell/query/internal/ExprUtil.scala index 75007d11..62489247 100644 --- a/modules/query/src/main/scala/docspell/query/internal/ExprUtil.scala +++ b/modules/query/src/main/scala/docspell/query/internal/ExprUtil.scala @@ -56,5 +56,7 @@ object ExprUtil { expr case CustomFieldMatch(_, _, _) => expr + case CustomFieldIdMatch(_, _, _) => + expr } } diff --git a/modules/query/src/main/scala/docspell/query/internal/SimpleExprParser.scala b/modules/query/src/main/scala/docspell/query/internal/SimpleExprParser.scala index f76fe947..55b92188 100644 --- a/modules/query/src/main/scala/docspell/query/internal/SimpleExprParser.scala +++ b/modules/query/src/main/scala/docspell/query/internal/SimpleExprParser.scala @@ -2,7 +2,6 @@ package docspell.query.internal import cats.parse.{Parser => P} -import docspell.query.ItemQuery.Expr.CustomFieldMatch import docspell.query.ItemQuery._ object SimpleExprParser { @@ -62,7 +61,13 @@ object SimpleExprParser { val customFieldExpr: P[Expr.CustomFieldMatch] = (P.string("f:") *> BasicParser.identParser ~ op ~ BasicParser.singleString).map { case ((name, op), value) => - CustomFieldMatch(name, op, value) + Expr.CustomFieldMatch(name, op, value) + } + + val customFieldIdExpr: P[Expr.CustomFieldIdMatch] = + (P.string("f.id:") *> BasicParser.identParser ~ op ~ BasicParser.singleString).map { + case ((name, op), value) => + Expr.CustomFieldIdMatch(name, op, value) } val inboxExpr: P[Expr.InboxExpr] = @@ -81,6 +86,7 @@ object SimpleExprParser { tagIdExpr, tagExpr, catExpr, + customFieldIdExpr, customFieldExpr, inboxExpr, dirExpr diff --git a/modules/store/src/main/scala/docspell/store/qb/generator/ItemQueryGenerator.scala b/modules/store/src/main/scala/docspell/store/qb/generator/ItemQueryGenerator.scala index 7904b62e..1ed9e7df 100644 --- a/modules/store/src/main/scala/docspell/store/qb/generator/ItemQueryGenerator.scala +++ b/modules/store/src/main/scala/docspell/store/qb/generator/ItemQueryGenerator.scala @@ -145,7 +145,10 @@ object ItemQueryGenerator { } case Expr.CustomFieldMatch(field, op, value) => - tables.item.id.in(itemsWithCustomField(coll, field, makeOp(op), value)) + tables.item.id.in(itemsWithCustomField(_.name ==== field)(coll, makeOp(op), value)) + + case Expr.CustomFieldIdMatch(field, op, value) => + tables.item.id.in(itemsWithCustomField(_.id ==== field)(coll, makeOp(op), value)) case Expr.Fulltext(_) => // not supported here @@ -229,18 +232,21 @@ object ItemQueryGenerator { QOp.Lte } - def itemsWithCustomField(coll: Ident, field: String, op: QOp, value: String): Select = { + private def itemsWithCustomField( + sel: RCustomField.Table => Condition + )(coll: Ident, op: QOp, value: String): Select = { val cf = RCustomField.as("cf") val cfv = RCustomFieldValue.as("cfv") val v = if (op == QOp.LowerLike) QueryWildcard.lower(value) else value Select( select(cfv.itemId), from(cfv).innerJoin(cf, cf.id === cfv.field), - cf.cid === coll && (cf.name ==== field || cf.id ==== field) && Condition.CompareVal( + cf.cid === coll && sel(cf) && Condition.CompareVal( cfv.value, op, v ) ) } + } diff --git a/modules/store/src/main/scala/docspell/store/queries/QueryWildcard.scala b/modules/store/src/main/scala/docspell/store/queries/QueryWildcard.scala index 5ab70d20..16a2f1bb 100644 --- a/modules/store/src/main/scala/docspell/store/queries/QueryWildcard.scala +++ b/modules/store/src/main/scala/docspell/store/queries/QueryWildcard.scala @@ -19,4 +19,11 @@ object QueryWildcard { else res } + def atEnd(s: String): String = + if (s.endsWith("*")) s"${s.dropRight(1)}%" + else s + + def addAtEnd(s: String): String = + if (s.endsWith("*")) atEnd(s) + else s"${s}%" } diff --git a/modules/webapp/src/main/elm/Comp/SearchMenu.elm b/modules/webapp/src/main/elm/Comp/SearchMenu.elm index 1cc34890..669d71b7 100644 --- a/modules/webapp/src/main/elm/Comp/SearchMenu.elm +++ b/modules/webapp/src/main/elm/Comp/SearchMenu.elm @@ -293,7 +293,7 @@ getItemQuery model = |> List.head |> Maybe.map (Q.ConcEquipId Q.Eq) , whenNotEmpty (Data.CustomFieldChange.toFieldValues model.customValues) - (List.map (Q.CustomField Q.Like) >> Q.And) + (List.map (Q.CustomFieldId Q.Like) >> Q.And) , Maybe.map (Q.DateMs Q.Gte) model.fromDate , Maybe.map (Q.DateMs Q.Lte) model.untilDate , Maybe.map (Q.DueDateMs Q.Gte) model.fromDueDate diff --git a/modules/webapp/src/main/elm/Data/ItemQuery.elm b/modules/webapp/src/main/elm/Data/ItemQuery.elm index 4105bac9..d01464b8 100644 --- a/modules/webapp/src/main/elm/Data/ItemQuery.elm +++ b/modules/webapp/src/main/elm/Data/ItemQuery.elm @@ -44,6 +44,7 @@ type ItemQuery | ConcPersId AttrMatch String | ConcEquipId AttrMatch String | CustomField AttrMatch CustomFieldValue + | CustomFieldId AttrMatch CustomFieldValue | DateMs AttrMatch Int | DueDateMs AttrMatch Int | Source AttrMatch String @@ -153,6 +154,9 @@ render q = CustomField m kv -> "f:" ++ kv.field ++ attrMatch m ++ quoteStr kv.value + CustomFieldId m kv -> + "f.id:" ++ kv.field ++ attrMatch m ++ quoteStr kv.value + DateMs m ms -> "date" ++ attrMatch m ++ "ms" ++ String.fromInt ms