Peewee可以使用sqlite的FTS5(全文搜索)辅助功能highlight()吗?

Can Peewee use highlight(), which is SQLite's FTS5 (full text search) auxiliary function?

  1. SQLite 的 FTS5 支持 highlight()。全文搜索查询结果的辅助功能 returns 标签:请参阅 official documentation.

  2. Peewee 的 code on Github, in the <sqlite_ext.py> module 也提到了 highlight(),虽然是顺便提一下。

    Built-in auxiliary functions:

    • bm25(tbl[, weight_0, ... weight_n])
    • highlight(tbl, col_idx, prefix, suffix)
    • snippet(tbl, col_idx, prefix, suffix, ?, max_tokens)
  3. 我在 Peewee 的 highlight() 文档中没有找到示例代码或参考,即使 Peewee 已经支持 bm25()rank() 作为目前的 FTS5辅助功能。

  4. 除非我遗漏了什么,否则如何在我的 Python 代码中将 FTS5 highlight() 与 Peewee 一起使用? (如果重要的话,我正在使用 Django。)

是的,在全文搜索查询中 select fn.highlight(...)

class Post(FTS5Model):
    title = SearchField()
    content = SearchField()
    class Meta:
        database = db

db.create_tables([Post])

Post.create(title='alpha', content='this is post alpha')
Post.create(title='beta', content='this is post beta')
Post.create(title='delta', content='this is post delta')

query = (Post
         .select(fn.highlight(Post._meta.entity, 1, '[', ']').alias('hi'))
         .where(Post.match('post')))
print(query.sql())
for row in query:
    print(row.hi)

请注意,我们必须使用 Post._meta.entity 作为第一个参数,以避免使用 table 的别名 - Sqlite 明确地使用 FTS table 名称。

打印:

this is [post] alpha
this is [post] beta
this is [post] delta