2019-11-19 15:37:53 +03:00
|
|
|
from mods.db import db, Catalog, CatalogIndex
|
2019-12-01 16:22:43 +03:00
|
|
|
import re
|
2019-11-23 23:36:26 +03:00
|
|
|
|
|
|
|
def add_catalog(name):
|
|
|
|
with db.atomic():
|
|
|
|
catalog = Catalog.create(name=name)
|
|
|
|
CatalogIndex.insert(
|
|
|
|
{
|
|
|
|
CatalogIndex.rowid: catalog.id,
|
|
|
|
CatalogIndex.text: name
|
|
|
|
}
|
|
|
|
).execute()
|
2019-11-12 21:39:26 +03:00
|
|
|
return catalog
|
|
|
|
|
|
|
|
def search_catalogs(q):
|
2019-12-01 16:22:43 +03:00
|
|
|
q = re.sub(r'\s+', ' ', q).strip()
|
|
|
|
q = ' '.join([ f'{x}*' for x in q.split(' ')])
|
2019-11-19 15:37:53 +03:00
|
|
|
with db.atomic():
|
|
|
|
return (Catalog.select()
|
|
|
|
.join(
|
|
|
|
CatalogIndex,
|
|
|
|
on=(Catalog.id == CatalogIndex.rowid))
|
|
|
|
.where(CatalogIndex.match(q))
|
|
|
|
.order_by(CatalogIndex.bm25()))
|
2019-11-12 21:39:26 +03:00
|
|
|
|