distribusi-verse/verse/search/search_index.py

67 lines
2.0 KiB
Python
Raw Normal View History

2024-06-29 15:21:47 +02:00
import os
from whoosh.fields import *
2024-07-11 19:46:20 +02:00
from whoosh.index import create_in, open_dir
2024-06-29 15:21:47 +02:00
from whoosh.qparser import QueryParser
from models.distribusi_model import Distribusis
2024-07-08 21:07:00 +02:00
from models.distribusi_file_model import DistribusiFiles
2024-06-29 15:21:47 +02:00
import flask_apscheduler
SCRIPT_DIR = os.path.dirname(__file__)
SEARCH_DATA_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, "searchdata"))
2024-06-29 15:25:28 +02:00
2024-06-29 15:21:47 +02:00
def init_search_index(APP):
scheduler = flask_apscheduler.APScheduler()
scheduler.api_enabled = False
scheduler.init_app(APP)
scheduler.start()
schema = Schema(
title=TEXT(stored=True), path=ID(stored=True), content=TEXT
)
ix = create_in(SEARCH_DATA_DIR, schema)
writer = ix.writer()
2024-07-08 21:07:00 +02:00
index_distribusis(APP, writer)
index_distribusi_files(APP, writer)
writer.commit(optimize=True)
@scheduler.task("interval", id="update", minutes=60)
def update_search_index():
2024-07-11 19:46:20 +02:00
ix = open_dir(SEARCH_DATA_DIR)
2024-07-08 21:07:00 +02:00
update_writer = ix.writer()
index_distribusis(APP, update_writer)
index_distribusi_files(APP, update_writer)
update_writer.commit(optimize=True)
def index_distribusis(APP, writer):
2024-06-29 15:21:47 +02:00
distribusis = _visible_distribusis(APP)
for distribusi in distribusis:
2024-06-29 15:25:28 +02:00
writer.add_document(
title=distribusi.distribusiname,
path="/a",
content=distribusi.description,
)
2024-06-29 15:21:47 +02:00
2024-07-08 21:07:00 +02:00
def index_distribusi_files(APP, writer):
with APP.app_context():
for distribusi_file in DistribusiFiles.query.all():
APP.logger.info(
f"adding distribusi file {distribusi_file.path} to search index"
)
writer.add_document(
title=distribusi_file.path,
path="/b",
content=distribusi_file.description,
)
2024-06-29 15:21:47 +02:00
def _visible_distribusis(APP):
with APP.app_context():
distribusis = Distribusis.query.filter(
Distribusis.visible.isnot(False)
).all()
return distribusis