Пробую полнотекстовый поиск SQLite с помощью Python, ОRМ Peewee
# Подключаю БД
database = SqliteExtDatabase(f'{config.DIR}/database.sqlite3', pragmas=(
('cache_size', -1024 * 64), # 64MB page-cache.
('journal_mode', 'wal'), # Use WAL-mode (you should always use this!).
('foreign_keys', 1))) # Enforce foreign-key constraints.)
# Таблица которую индексируем
class Task(BaseModel):
id = BigIntegerField(primary_key=True)
text = CharField(default=None)
file_id = CharField(default=None, null=True)
def __repr__(self) -> str:
return f'<Task {self.id}>'
class Meta:
table_name = 'tasks'
# Таблица где индексируем
class DocumentIndex(FTSModel):
# Full-text search index.
rowid = RowIDField()
text = SearchField()
class Meta:
database = database
# Use the porter stemming algorithm to tokenize content.
options = {'tokenize': 'porter'}
def store_document(task):
DocumentIndex.insert({
DocumentIndex.rowid: task.id,
DocumentIndex.text: task.text}).execute()
# Когда создаю запись в Task, сразу добавляю в DocumentIndex
store_document(task)
def search_task(phrase) -> list[Task]:
query = (Task.select().join(DocumentIndex, on=(Task.id == DocumentIndex.rowid)).
where(DocumentIndex.match(phrase))
.order_by(DocumentIndex.bm25()))
return list(query)
# Поиск
search_task("Текст для поиска")
Traceback (most recent call last):
File "D:\Prog\ZigTask\venv\lib\site-packages\peewee.py", line 3237, in execute_sql
cursor.execute(sql, params or ())
sqlite3.OperationalError: unable to use function MATCH in the requested context
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Prog\ZigTask\bot\handlers\errors\error_handler.py", line 52, in errors_handler
raise exception
File "D:\Prog\ZigTask\venv\lib\site-packages\aiogram\dispatcher\dispatcher.py", line 1394, in process_response
response = task.result()
File "D:\Prog\ZigTask\bot\handlers\users\find_task.py", line 61, in _task_text
if tasks := search_task(find_text):
File "D:\Prog\ZigTask\venv\lib\site-packages\peewee.py", line 2063, in __len__
self._ensure_execution()
File "D:\Prog\ZigTask\venv\lib\site-packages\peewee.py", line 2045, in _ensure_execution
self.execute()
File "D:\Prog\ZigTask\venv\lib\site-packages\peewee.py", line 1962, in inner
return method(self, database, *args, **kwargs)
File "D:\Prog\ZigTask\venv\lib\site-packages\peewee.py", line 2033, in execute
return self._execute(database)
File "D:\Prog\ZigTask\venv\lib\site-packages\peewee.py", line 2206, in _execute
cursor = database.execute(self)
File "D:\Prog\ZigTask\venv\lib\site-packages\peewee.py", line 3250, in execute
return self.execute_sql(sql, params, commit=commit)
File "D:\Prog\ZigTask\venv\lib\site-packages\peewee.py", line 3244, in execute_sql
self.commit()
File "D:\Prog\ZigTask\venv\lib\site-packages\peewee.py", line 3010, in __exit__
reraise(new_type, new_type(exc_value, *exc_args), traceback)
File "D:\Prog\ZigTask\venv\lib\site-packages\peewee.py", line 192, in reraise
raise value.with_traceback(tb)
File "D:\Prog\ZigTask\venv\lib\site-packages\peewee.py", line 3237, in execute_sql
cursor.execute(sql, params or ())
peewee.OperationalError: unable to use function MATCH in the requested context