Pass language when updating fulltext index

This commit is contained in:
eikek 2022-03-20 13:14:07 +01:00
parent f2a2d15e7f
commit b2add008ed
7 changed files with 72 additions and 22 deletions

View File

@ -8,7 +8,6 @@ package docspell.backend.fulltext
import cats.data.NonEmptyList import cats.data.NonEmptyList
import cats.effect._ import cats.effect._
import docspell.common._ import docspell.common._
import docspell.ftsclient.FtsClient import docspell.ftsclient.FtsClient
import docspell.ftsclient.TextData import docspell.ftsclient.TextData
@ -62,7 +61,14 @@ object CreateIndex {
val items = store val items = store
.transact(QItem.allNameAndNotes(collective, itemIds, chunkSize)) .transact(QItem.allNameAndNotes(collective, itemIds, chunkSize))
.map(nn => .map(nn =>
TextData.item(nn.id, nn.collective, nn.folder, Option(nn.name), nn.notes) TextData.item(
nn.id,
nn.collective,
nn.folder,
Option(nn.name),
nn.notes,
nn.language
)
) )
fts.indexData(logger, attachs ++ items) fts.indexData(logger, attachs ++ items)

View File

@ -605,7 +605,14 @@ object OItem {
.transact(RItem.updateNotes(item, collective, notes)) .transact(RItem.updateNotes(item, collective, notes))
) )
.flatTap( .flatTap(
onSuccessIgnoreError(fts.updateItemNotes(logger, item, collective, notes)) onSuccessIgnoreError {
store
.transact(RCollective.findLanguage(collective))
.map(_.getOrElse(Language.English))
.flatMap(lang =>
fts.updateItemNotes(logger, item, collective, lang, notes)
)
}
) )
def setName(item: Ident, name: String, collective: Ident): F[UpdateResult] = def setName(item: Ident, name: String, collective: Ident): F[UpdateResult] =
@ -615,7 +622,14 @@ object OItem {
.transact(RItem.updateName(item, collective, name)) .transact(RItem.updateName(item, collective, name))
) )
.flatTap( .flatTap(
onSuccessIgnoreError(fts.updateItemName(logger, item, collective, name)) onSuccessIgnoreError {
store
.transact(RCollective.findLanguage(collective))
.map(_.getOrElse(Language.English))
.flatMap(lang =>
fts.updateItemName(logger, item, collective, lang, name)
)
}
) )
def setNameMultiple( def setNameMultiple(
@ -733,10 +747,17 @@ object OItem {
) )
.flatTap( .flatTap(
onSuccessIgnoreError( onSuccessIgnoreError(
OptionT(store.transact(RAttachment.findItemId(attachId))) OptionT(store.transact(RAttachment.findItemAndLanguage(attachId)))
.semiflatMap(itemId => .semiflatMap { case (itemId, lang) =>
fts.updateAttachmentName(logger, itemId, attachId, collective, name) fts.updateAttachmentName(
) logger,
itemId,
attachId,
collective,
lang.getOrElse(Language.English),
name
)
}
.fold(())(identity) .fold(())(identity)
) )
) )

View File

@ -68,19 +68,24 @@ trait FtsClient[F[_]] {
logger: Logger[F], logger: Logger[F],
itemId: Ident, itemId: Ident,
collective: Ident, collective: Ident,
language: Language,
name: String name: String
): F[Unit] = ): F[Unit] =
updateIndex(logger, TextData.item(itemId, collective, None, Some(name), None)) updateIndex(
logger,
TextData.item(itemId, collective, None, Some(name), None, language)
)
def updateItemNotes( def updateItemNotes(
logger: Logger[F], logger: Logger[F],
itemId: Ident, itemId: Ident,
collective: Ident, collective: Ident,
language: Language,
notes: Option[String] notes: Option[String]
): F[Unit] = ): F[Unit] =
updateIndex( updateIndex(
logger, logger,
TextData.item(itemId, collective, None, None, Some(notes.getOrElse(""))) TextData.item(itemId, collective, None, None, Some(notes.getOrElse("")), language)
) )
def updateAttachmentName( def updateAttachmentName(
@ -88,6 +93,7 @@ trait FtsClient[F[_]] {
itemId: Ident, itemId: Ident,
attachId: Ident, attachId: Ident,
collective: Ident, collective: Ident,
language: Language,
name: Option[String] name: Option[String]
): F[Unit] = ): F[Unit] =
updateIndex( updateIndex(
@ -97,7 +103,7 @@ trait FtsClient[F[_]] {
attachId, attachId,
collective, collective,
None, None,
Language.English, language,
Some(name.getOrElse("")), Some(name.getOrElse("")),
None None
) )

View File

@ -18,6 +18,8 @@ sealed trait TextData {
def folder: Option[Ident] def folder: Option[Ident]
def language: Language
final def fold[A](f: TextData.Attachment => A, g: TextData.Item => A): A = final def fold[A](f: TextData.Attachment => A, g: TextData.Item => A): A =
this match { this match {
case a: TextData.Attachment => f(a) case a: TextData.Attachment => f(a)
@ -32,7 +34,7 @@ object TextData {
attachId: Ident, attachId: Ident,
collective: Ident, collective: Ident,
folder: Option[Ident], folder: Option[Ident],
lang: Language, language: Language,
name: Option[String], name: Option[String],
text: Option[String] text: Option[String]
) extends TextData { ) extends TextData {
@ -57,7 +59,8 @@ object TextData {
collective: Ident, collective: Ident,
folder: Option[Ident], folder: Option[Ident],
name: Option[String], name: Option[String],
notes: Option[String] notes: Option[String],
language: Language
) extends TextData { ) extends TextData {
val id = Ident.unsafe("item") / item val id = Ident.unsafe("item") / item
@ -69,8 +72,9 @@ object TextData {
collective: Ident, collective: Ident,
folder: Option[Ident], folder: Option[Ident],
name: Option[String], name: Option[String],
notes: Option[String] notes: Option[String],
lang: Language
): TextData = ): TextData =
Item(item, collective, folder, name, notes) Item(item, collective, folder, name, notes, lang)
} }

View File

@ -47,9 +47,10 @@ object TextExtraction {
ctx.args.meta.collective, ctx.args.meta.collective,
ctx.args.meta.folderId, ctx.args.meta.folderId,
item.item.name.some, item.item.name.some,
None None,
ctx.args.meta.language
) )
_ <- fts.indexData(ctx.logger, (idxItem +: txt.map(_.td)).toSeq: _*) _ <- fts.indexData(ctx.logger, (idxItem +: txt.map(_.td)): _*)
dur <- start dur <- start
extractedTags = txt.flatMap(_.tags).distinct.toList extractedTags = txt.flatMap(_.tags).distinct.toList
_ <- ctx.logger.info(s"Text extraction finished in ${dur.formatExact}.") _ <- ctx.logger.info(s"Text extraction finished in ${dur.formatExact}.")

View File

@ -576,7 +576,8 @@ object QItem {
collective: Ident, collective: Ident,
folder: Option[Ident], folder: Option[Ident],
name: String, name: String,
notes: Option[String] notes: Option[String],
language: Language
) )
def allNameAndNotes( def allNameAndNotes(
coll: Option[Ident], coll: Option[Ident],
@ -584,10 +585,11 @@ object QItem {
chunkSize: Int chunkSize: Int
): Stream[ConnectionIO, NameAndNotes] = { ): Stream[ConnectionIO, NameAndNotes] = {
val i = RItem.as("i") val i = RItem.as("i")
val c = RCollective.as("c")
Select( Select(
select(i.id, i.cid, i.folder, i.name, i.notes), select(i.id, i.cid, i.folder, i.name, i.notes, c.language),
from(i) from(i).innerJoin(c, c.id === i.cid)
).where( ).where(
i.state.in(ItemState.validStates) &&? i.state.in(ItemState.validStates) &&?
itemIds.map(ids => i.id.in(ids)) &&? itemIds.map(ids => i.id.in(ids)) &&?

View File

@ -236,8 +236,18 @@ object RAttachment {
n3 <- DML.delete(T, T.id === attachId) n3 <- DML.delete(T, T.id === attachId)
} yield n0 + n1 + n2 + n3 } yield n0 + n1 + n2 + n3
def findItemId(attachId: Ident): ConnectionIO[Option[Ident]] = def findItemAndLanguage(
Select(T.itemId.s, from(T), T.id === attachId).build.query[Ident].option attachId: Ident
): ConnectionIO[Option[(Ident, Option[Language])]] = {
val a = RAttachment.as("a")
val m = RAttachmentMeta.as("m")
Select(
select(a.itemId, m.language),
from(a).leftJoin(m, m.id === a.id),
a.id === attachId
).build.query[(Ident, Option[Language])].option
}
def findAll( def findAll(
coll: Option[Ident], coll: Option[Ident],