SQLite's FTS5 supports highlight()
. That auxiliary function returns tags for results from full text search queries: see the official documentation.
Peewee's code on Github, in the <sqlite_ext.py> module also mentions highlight()
, although in passing.
Built-in auxiliary functions:
- bm25(tbl[, weight_0, ... weight_n])
- highlight(tbl, col_idx, prefix, suffix)
- snippet(tbl, col_idx, prefix, suffix, ?, max_tokens)
I've found no example code or reference in Peewee's documentation to highlight()
, even though Peewee already has support for bm25()
and rank()
as present FTS5 auxiliary functions.
Unless I've missed something, how do I use FTS5 highlight()
with Peewee in my Python code? (I'm using Django, if that matters.)
Yeah, just select fn.highlight(...)
within a full-text search query.
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)
Note that we have to use Post._meta.entity
as the first argument, to avoid using the table's alias - Sqlite is particular about using the FTS table name explicitly.
Prints:
this is [post] alpha
this is [post] beta
this is [post] delta