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

2024-05-15 08:08:58 发布

您现在位置:Python中文网/ 问答频道 /正文

  1. SQLite的FTS5支持highlight()。该辅助函数返回全文搜索查询结果的标记:请参见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代码中如何将FTS5highlight()与Peewee一起使用?(如果有必要的话,我用的是Django。)


Tags: 函数代码insqliteprefixcolsuffix全文
1条回答
网友
1楼 · 发布于 2024-05-15 08:08:58

是的,只需在全文搜索查询中选择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作为第一个参数,以避免使用表的别名-Sqlite特别注意显式使用FTS表名

印刷品:

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

相关问题 更多 >