From 7b1ec97c97bcdaecc413266ec71e4b5f13e1a75c Mon Sep 17 00:00:00 2001 From: Eike Kettner Date: Mon, 8 Mar 2021 00:46:36 +0100 Subject: [PATCH] Fix sort when using fulltext only --- .../docspell/backend/ops/OSimpleSearch.scala | 46 ++++++++++--------- .../docspell/query/FulltextExtractTest.scala | 5 ++ .../restserver/src/main/resources/logback.xml | 1 - .../restserver/conv/Conversions.scala | 4 ++ .../restserver/routes/ItemRoutes.scala | 12 ++++- 5 files changed, 44 insertions(+), 24 deletions(-) diff --git a/modules/backend/src/main/scala/docspell/backend/ops/OSimpleSearch.scala b/modules/backend/src/main/scala/docspell/backend/ops/OSimpleSearch.scala index e468eaaf..1c5e54df 100644 --- a/modules/backend/src/main/scala/docspell/backend/ops/OSimpleSearch.scala +++ b/modules/backend/src/main/scala/docspell/backend/ops/OSimpleSearch.scala @@ -52,37 +52,41 @@ object OSimpleSearch { sealed trait Items { def fold[A]( - f1: Vector[OFulltext.FtsItem] => A, - f2: Vector[OFulltext.FtsItemWithTags] => A, + f1: Items.FtsItems => A, + f2: Items.FtsItemsFull => A, f3: Vector[OItemSearch.ListItem] => A, f4: Vector[OItemSearch.ListItemWithTags] => A ): A } object Items { - def ftsItems(items: Vector[OFulltext.FtsItem]): Items = - FtsItems(items) + def ftsItems(indexOnly: Boolean)(items: Vector[OFulltext.FtsItem]): Items = + FtsItems(items, indexOnly) - case class FtsItems(items: Vector[OFulltext.FtsItem]) extends Items { + case class FtsItems(items: Vector[OFulltext.FtsItem], indexOnly: Boolean) + extends Items { def fold[A]( - f1: Vector[OFulltext.FtsItem] => A, - f2: Vector[OFulltext.FtsItemWithTags] => A, + f1: FtsItems => A, + f2: FtsItemsFull => A, f3: Vector[OItemSearch.ListItem] => A, f4: Vector[OItemSearch.ListItemWithTags] => A - ): A = f1(items) + ): A = f1(this) } - def ftsItemsFull(items: Vector[OFulltext.FtsItemWithTags]): Items = - FtsItemsFull(items) + def ftsItemsFull(indexOnly: Boolean)( + items: Vector[OFulltext.FtsItemWithTags] + ): Items = + FtsItemsFull(items, indexOnly) - case class FtsItemsFull(items: Vector[OFulltext.FtsItemWithTags]) extends Items { + case class FtsItemsFull(items: Vector[OFulltext.FtsItemWithTags], indexOnly: Boolean) + extends Items { def fold[A]( - f1: Vector[OFulltext.FtsItem] => A, - f2: Vector[OFulltext.FtsItemWithTags] => A, + f1: FtsItems => A, + f2: FtsItemsFull => A, f3: Vector[OItemSearch.ListItem] => A, f4: Vector[OItemSearch.ListItemWithTags] => A - ): A = f2(items) + ): A = f2(this) } def itemsPlain(items: Vector[OItemSearch.ListItem]): Items = @@ -90,8 +94,8 @@ object OSimpleSearch { case class ItemsPlain(items: Vector[OItemSearch.ListItem]) extends Items { def fold[A]( - f1: Vector[OFulltext.FtsItem] => A, - f2: Vector[OFulltext.FtsItemWithTags] => A, + f1: FtsItems => A, + f2: FtsItemsFull => A, f3: Vector[OItemSearch.ListItem] => A, f4: Vector[OItemSearch.ListItemWithTags] => A ): A = f3(items) @@ -102,8 +106,8 @@ object OSimpleSearch { case class ItemsFull(items: Vector[OItemSearch.ListItemWithTags]) extends Items { def fold[A]( - f1: Vector[OFulltext.FtsItem] => A, - f2: Vector[OFulltext.FtsItemWithTags] => A, + f1: FtsItems => A, + f2: FtsItemsFull => A, f3: Vector[OItemSearch.ListItem] => A, f4: Vector[OItemSearch.ListItemWithTags] => A ): A = f4(items) @@ -190,7 +194,7 @@ object OSimpleSearch { q.fix.account, settings.batch ) - .map(Items.ftsItemsFull) + .map(Items.ftsItemsFull(true)) else if (settings.resolveDetails) fts .findItemsWithTags(settings.maxNoteLen)( @@ -198,11 +202,11 @@ object OSimpleSearch { OFulltext.FtsInput(ftq), settings.batch ) - .map(Items.ftsItemsFull) + .map(Items.ftsItemsFull(false)) else fts .findItems(settings.maxNoteLen)(q, OFulltext.FtsInput(ftq), settings.batch) - .map(Items.ftsItems) + .map(Items.ftsItems(false)) case _ => if (settings.resolveDetails) diff --git a/modules/query/shared/src/test/scala/docspell/query/FulltextExtractTest.scala b/modules/query/shared/src/test/scala/docspell/query/FulltextExtractTest.scala index 8b1fe312..9b6ea799 100644 --- a/modules/query/shared/src/test/scala/docspell/query/FulltextExtractTest.scala +++ b/modules/query/shared/src/test/scala/docspell/query/FulltextExtractTest.scala @@ -29,6 +29,11 @@ class FulltextExtractTest extends FunSuite { findFts("content:\"what OR hello\""), Result.Success(ItemQuery.all.expr, "what OR hello".some) ) + + assertEquals( + findFts("(& content:\"what OR hello\" )"), + Result.Success(ItemQuery.all.expr, "what OR hello".some) + ) } test("find no fulltext") { diff --git a/modules/restserver/src/main/resources/logback.xml b/modules/restserver/src/main/resources/logback.xml index d972abcb..f9b2d921 100644 --- a/modules/restserver/src/main/resources/logback.xml +++ b/modules/restserver/src/main/resources/logback.xml @@ -9,7 +9,6 @@ - diff --git a/modules/restserver/src/main/scala/docspell/restserver/conv/Conversions.scala b/modules/restserver/src/main/scala/docspell/restserver/conv/Conversions.scala index a6516bc0..cede3845 100644 --- a/modules/restserver/src/main/scala/docspell/restserver/conv/Conversions.scala +++ b/modules/restserver/src/main/scala/docspell/restserver/conv/Conversions.scala @@ -224,6 +224,10 @@ trait Conversions { if (v.isEmpty) ItemLightList(Nil) else ItemLightList(List(ItemLightGroup("Results", v.map(mkItemLightWithTags).toList))) + def mkItemListFtsPlain(v: Vector[OFulltext.FtsItem]): ItemLightList = + if (v.isEmpty) ItemLightList(Nil) + else ItemLightList(List(ItemLightGroup("Results", v.map(mkItemLight).toList))) + def mkItemLight(i: OItemSearch.ListItem): ItemLight = ItemLight( i.id, diff --git a/modules/restserver/src/main/scala/docspell/restserver/routes/ItemRoutes.scala b/modules/restserver/src/main/scala/docspell/restserver/routes/ItemRoutes.scala index 9422fdb0..2e7fbbb5 100644 --- a/modules/restserver/src/main/scala/docspell/restserver/routes/ItemRoutes.scala +++ b/modules/restserver/src/main/scala/docspell/restserver/routes/ItemRoutes.scala @@ -497,14 +497,22 @@ object ItemRoutes { )(settings: OSimpleSearch.Settings, fixQuery: Query.Fix, itemQuery: ItemQueryString) = { import dsl._ + def convertFts(res: OSimpleSearch.Items.FtsItems): ItemLightList = + if (res.indexOnly) Conversions.mkItemListFtsPlain(res.items) + else Conversions.mkItemListFts(res.items) + + def convertFtsFull(res: OSimpleSearch.Items.FtsItemsFull): ItemLightList = + if (res.indexOnly) Conversions.mkItemListWithTagsFtsPlain(res.items) + else Conversions.mkItemListWithTagsFts(res.items) + backend.simpleSearch .searchByString(settings)(fixQuery, itemQuery) .flatMap { case StringSearchResult.Success(items) => Ok( items.fold( - Conversions.mkItemListFts, - Conversions.mkItemListWithTagsFts, + convertFts, + convertFtsFull, Conversions.mkItemList, Conversions.mkItemListWithTags )