distribusi-verse/verse/search/search.py

55 lines
1.7 KiB
Python
Raw Normal View History

2024-06-06 19:42:06 +02:00
import os
2024-06-29 15:21:47 +02:00
from flask import Blueprint, render_template
2024-06-06 19:42:06 +02:00
from whoosh.fields import *
from whoosh.index import open_dir
from whoosh.qparser import QueryParser
2024-06-29 15:21:47 +02:00
from search.forms.searchform import SearchForm
searchpages = Blueprint(
"search",
__name__,
template_folder="templates/search",
static_folder="static",
)
2024-06-06 19:42:06 +02:00
SCRIPT_DIR = os.path.dirname(__file__)
2024-06-29 15:21:47 +02:00
SEARCH_DATA_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, "searchdata"))
2024-06-29 15:41:31 +02:00
2024-06-29 15:21:47 +02:00
@searchpages.route("/", methods=["GET", "POST"])
def searchpage():
searchform = SearchForm()
found_distribusis = []
2024-07-08 21:07:00 +02:00
found_distribusi_files = []
2024-06-29 15:21:47 +02:00
if searchform.validate_on_submit():
2024-07-08 21:07:00 +02:00
found_distribusis, found_distribusi_files = search(
searchform.searchfield.data
)
2024-06-29 15:21:47 +02:00
template = render_template(
"search.html",
searchform=searchform,
found_distribusis=found_distribusis,
2024-07-08 21:07:00 +02:00
found_distribusi_files=found_distribusi_files,
2024-06-29 15:21:47 +02:00
)
return template
def search(searchinput):
"""search and get search result titles and return them as distribusi ids"""
2024-06-29 15:41:31 +02:00
ix = open_dir(SEARCH_DATA_DIR)
2024-06-29 15:21:47 +02:00
with ix.searcher() as searcher:
query = QueryParser("content", ix.schema).parse(searchinput)
search_results = searcher.search(query)
2024-07-08 21:07:00 +02:00
for result in search_results:
print(result["title"])
print(result["path"])
2024-06-29 15:21:47 +02:00
found_distribusis = []
2024-07-08 21:07:00 +02:00
found_distribusi_files = []
2024-06-29 15:21:47 +02:00
for result in search_results:
2024-07-08 21:07:00 +02:00
if result["path"] == "/a":
found_distribusis.append(result["title"])
if result["path"] == "/b":
found_distribusi_files.append(result["title"])
return found_distribusis, found_distribusi_files