[英]Can Peewee use highlight(), which is SQLite's FTS5 (full text search) auxiliary function?
SQLite 的 FTS5 支持highlight()
。 該輔助函數返回全文搜索查詢結果的標簽:請參閱官方文檔。
Peewee 在 Github 上的代碼,在<sqlite_ext.py>模塊中也提到了highlight()
,盡管順便說一下。
內置輔助功能:
- bm25(tbl[, weight_0, ... weight_n])
- 突出顯示(tbl,col_idx,前綴,后綴)
- 片段(tbl,col_idx,前綴,后綴,?,max_tokens)
我發現Peewee的文檔中沒有示例代碼或引用highlight()
即使Peewee已經具備了支持bm25()
和rank()
作為目前FTS5輔助功能。
除非我錯過了什么,否則我如何在 Python 代碼中將 FTS5 highlight()
與 Peewee 結合使用? (如果這很重要,我正在使用 Django。)
是的,只需在全文搜索查詢中選擇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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.