with open(filepath, "r", encoding='utf-8') as f:
# считываем содержимое файла
data = f.read()
# разделим файл на отдельные объекты JSON
json_objects = (data[1:-2])
json_objects = json_objects.split('}\n{')
# сформируем список объектов
parsed_data = []
for obj in json_objects:
obj = obj + '}'
obj = '{' + obj
parsed_data.append(json.loads(obj))
from playhouse.sqlite_ext import *
db = SqliteExtDatabase(':memory:')
class Document(Model):
# Canonical source of data, stored in a regular table.
title = TextField(null=False, unique=True)
content = TextField(null=False)
class Meta:
database = db
class DocumentIndex(FTSModel):
# Full-text search index.
rowid = RowIDField()
title = SearchField()
content = SearchField()
class Meta:
database = db
# Use the porter stemming algorithm to tokenize content.
options = {'tokenize': 'porter'}
db.create_tables([Document, DocumentIndex])
Document.create(title='title', content={'k1': 'v1'})
doc = Document.get(Document.title == 'title')
def store_document(document):
DocumentIndex.insert({
DocumentIndex.rowid: document.id,
DocumentIndex.title: document.title,
DocumentIndex.content: document.content}).execute()
store_document(doc)
def search(phrase) -> list[Document]:
# Query the search index and join the corresponding Document
# object on each search result.
query = (Document
.select()
.join(DocumentIndex, on=(Document.id == DocumentIndex.rowid))
.where(DocumentIndex.match(phrase))
.order_by(DocumentIndex.bm25()))
return list(query)
value = search('title')
print(value)
def search_task(phrase) -> list[Task]:
# Search index for "search phrase" and return results ranked
# by relevancy using the BM25 algorithm.
query = (TaskIndex.select().where(TaskIndex.match(phrase)).order_by(TaskIndex.bm25()))
for result in query:
print('Result: %s' % result.title)
return list(query)
Base = declarative_base()
async def create_async_database():
engine = create_async_engine(SQLALCHEMY_DATABASE_URI)
async_session = sessionmaker(
engine, expire_on_commit=False, class_=AsyncSession
)
async with async_session() as session:
async with session.begin():
return session