Return all tags in search stats result

Before only tags with a count > 0 were included. Now those that have
not attached to any item are returned as well.
This commit is contained in:
Eike Kettner 2021-01-11 12:06:20 +01:00
parent 3d164206d3
commit 3fccc3df39
2 changed files with 29 additions and 8 deletions

View File

@ -250,6 +250,7 @@ object QItem {
.innerJoin(tag, tag.tid === ti.tagId) .innerJoin(tag, tag.tid === ti.tagId)
.innerJoin(i, i.id === ti.itemId) .innerJoin(i, i.id === ti.itemId)
val tagCloud =
findItemsBase(q, 0).unwrap findItemsBase(q, 0).unwrap
.withSelect(select(tag.all).append(count(i.id).as("num"))) .withSelect(select(tag.all).append(count(i.id).as("num")))
.changeFrom(_.prepend(tagFrom)) .changeFrom(_.prepend(tagFrom))
@ -258,6 +259,13 @@ object QItem {
.build .build
.query[TagCount] .query[TagCount]
.to[List] .to[List]
// the previous query starts from tags, so items with tag-count=0
// are not included they are fetched separately
for {
existing <- tagCloud
other <- RTag.findOthers(q.account.collective, existing.map(_.tag.tagId))
} yield existing ++ other.map(TagCount(_, 0))
} }
def searchCountSummary(q: Query): ConnectionIO[Int] = def searchCountSummary(q: Query): ConnectionIO[Int] =

View File

@ -135,6 +135,19 @@ object RTag {
} }
} }
def findOthers(coll: Ident, excludeTags: List[Ident]): ConnectionIO[List[RTag]] = {
val excl =
NonEmptyList
.fromList(excludeTags)
.map(nel => T.tid.notIn(nel))
Select(
select(T.all),
from(T),
T.cid === coll &&? excl
).orderBy(T.name.asc).build.query[RTag].to[List]
}
def delete(tagId: Ident, coll: Ident): ConnectionIO[Int] = def delete(tagId: Ident, coll: Ident): ConnectionIO[Int] =
DML.delete(T, T.tid === tagId && T.cid === coll) DML.delete(T, T.tid === tagId && T.cid === coll)
} }