1

我一直在尝试使用 FTS 表的新 Android 的 Room 2.1 功能来启用搜索功能。

实体:

 @Entity
    class ChatMessageEntity(
            @PrimaryKey
            var messageId: String,
            var messageUser: String,
            var messageText: String,
            )

FTS 实体:

@Fts4(contentEntity = ChatMessageEntity::class)
@Entity
class ChatMessageFts(
        var messageText: String //only want messageText searchable
)

数据库:

@Database(entities = [ChatMessageEntity::class, ChatMessageFts::class], version = 1)
abstract class ChatMessageDatabase : RoomDatabase() {

    abstract fun chatMessageDatabase(): ChatMessageDao

    companion object {
        fun create(context: Context) = Room.databaseBuilder(context, ChatMessageDatabase::class.java, DB_NAME).build()
    }
}

道:

@Query("INSERT INTO ChatMessageFts(ChatMessageFts) VALUES('rebuild')") //works
    suspend fun ftsRebuild()

@Query("INSERT INTO ChatMessageFts(docid, messageText) VALUES(:id, :text)") //compiles but doesn't work
    suspend fun saveFtsMessage(id: Long, text: String)

当我还在原始表中添加行时,我希望在 FTS 中添加每一行(ChatMessageEntity - 未显示)。从头开始重建 FTS 表时,它工作得很好(ftsRebuild()上图)。

但是,我希望单独添加每个输入。我已经尝试了很多东西(例如saveFtsMessage()上面显示的) - 添加没有 docId,尝试使用 rowId,...我什至尝试添加一个新对象ChatMessageEntity作为ChatMessageFts输入,但由于错误我没有设法编译它。

任何关于如何在 FTS 表中添加个人输入的建议都非常受欢迎!

4

1 回答 1

3
@Fts4(contentEntity = ChatMessageEntity::class)

注释属性的神奇之处contentEntity在于 Room 设置触发器以使您的 FTS 表与相应的实体表保持同步:

"contentSyncTriggers": [
  "CREATE TRIGGER IF NOT EXISTS room_fts_content_sync_paragraphsFts_BEFORE_UPDATE BEFORE UPDATE ON `paragraphs` BEGIN DELETE FROM `paragraphsFts` WHERE `docid`=OLD.`rowid`; END",
  "CREATE TRIGGER IF NOT EXISTS room_fts_content_sync_paragraphsFts_BEFORE_DELETE BEFORE DELETE ON `paragraphs` BEGIN DELETE FROM `paragraphsFts` WHERE `docid`=OLD.`rowid`; END",
  "CREATE TRIGGER IF NOT EXISTS room_fts_content_sync_paragraphsFts_AFTER_UPDATE AFTER UPDATE ON `paragraphs` BEGIN INSERT INTO `paragraphsFts`(`docid`, `prose`) VALUES (NEW.`rowid`, NEW.`prose`); END",
  "CREATE TRIGGER IF NOT EXISTS room_fts_content_sync_paragraphsFts_AFTER_INSERT AFTER INSERT ON `paragraphs` BEGIN INSERT INTO `paragraphsFts`(`docid`, `prose`) VALUES (NEW.`rowid`, NEW.`prose`); END"
]

(来自本书这个示例应用程序

只要您插入、更新和删除实体,FTS 表就会自动维护。您只需将 FTS 表用于查询目的。

如果您跳过contentEntity注释 - 并假设它有效 - 那么您可能需要独立维护您的 FTS 表。

于 2020-02-17T12:58:36.100 回答