Add summary for fulltext searches

This commit is contained in:
Eike Kettner
2020-12-17 00:11:33 +01:00
parent 8d7b3c7d74
commit 6346bf6a34
4 changed files with 100 additions and 20 deletions

View File

@ -7,12 +7,15 @@ import fs2.Stream
import docspell.backend.JobFactory
import docspell.backend.ops.OItemSearch._
import docspell.common._
import docspell.common.syntax.all._
import docspell.ftsclient._
import docspell.store.queries.{QFolder, QItem, SelectedItem}
import docspell.store.queue.JobQueue
import docspell.store.records.RJob
import docspell.store.{Store, qb}
import org.log4s.getLogger
trait OFulltext[F[_]] {
def findItems(maxNoteLen: Int)(
@ -34,6 +37,9 @@ trait OFulltext[F[_]] {
batch: qb.Batch
): F[Vector[OFulltext.FtsItemWithTags]]
def findIndexOnlySummary(account: AccountId, fts: OFulltext.FtsInput): F[SearchSummary]
def findItemsSummary(q: Query, fts: OFulltext.FtsInput): F[SearchSummary]
/** Clears the full-text index completely and launches a task that
* indexes all data.
*/
@ -46,6 +52,7 @@ trait OFulltext[F[_]] {
}
object OFulltext {
private[this] val logger = getLogger
case class FtsInput(
query: String,
@ -77,12 +84,14 @@ object OFulltext {
Resource.pure[F, OFulltext[F]](new OFulltext[F] {
def reindexAll: F[Unit] =
for {
_ <- logger.finfo(s"Re-index all.")
job <- JobFactory.reIndexAll[F]
_ <- queue.insertIfNew(job) *> joex.notifyAllNodes
} yield ()
def reindexCollective(account: AccountId): F[Unit] =
for {
_ <- logger.fdebug(s"Re-index collective: $account")
exist <- store.transact(
RJob.findNonFinalByTracker(DocspellSystem.migrationTaskTracker)
)
@ -107,6 +116,7 @@ object OFulltext {
FtsQuery.HighlightSetting(ftsQ.highlightPre, ftsQ.highlightPost)
)
for {
_ <- logger.ftrace(s"Find index only: ${ftsQ.query}/${batch}")
folders <- store.transact(QFolder.getMemberFolders(account))
ftsR <- fts.search(fq.withFolders(folders))
ftsItems = ftsR.results.groupBy(_.itemId)
@ -133,6 +143,32 @@ object OFulltext {
} yield res
}
def findIndexOnlySummary(
account: AccountId,
ftsQ: OFulltext.FtsInput
): F[SearchSummary] = {
val fq = FtsQuery(
ftsQ.query,
account.collective,
Set.empty,
Set.empty,
500,
0,
FtsQuery.HighlightSetting.default
)
for {
folder <- store.transact(QFolder.getMemberFolders(account))
itemIds <- fts
.searchAll(fq.withFolders(folder))
.flatMap(r => Stream.emits(r.results.map(_.itemId)))
.compile
.to(Set)
q = Query.empty(account).copy(itemIds = itemIds.some)
res <- store.transact(QItem.searchStats(q))
} yield res
}
def findItems(
maxNoteLen: Int
)(q: Query, ftsQ: FtsInput, batch: qb.Batch): F[Vector[FtsItem]] =
@ -167,6 +203,27 @@ object OFulltext {
.compile
.toVector
def findItemsSummary(q: Query, ftsQ: OFulltext.FtsInput): F[SearchSummary] =
for {
search <- itemSearch.findItems(0)(q, Batch.all)
fq = FtsQuery(
ftsQ.query,
q.account.collective,
search.map(_.id).toSet,
Set.empty,
500,
0,
FtsQuery.HighlightSetting.default
)
items <- fts
.searchAll(fq)
.flatMap(r => Stream.emits(r.results.map(_.itemId)))
.compile
.to(Set)
qnext = q.copy(itemIds = items.some)
res <- store.transact(QItem.searchStats(qnext))
} yield res
// Helper
private def findItemsFts[A: ItemId, B](