import os from flask import Blueprint, render_template from whoosh.fields import * from whoosh.index import open_dir from whoosh.qparser import QueryParser from search.forms.searchform import SearchForm searchpages = Blueprint( "search", __name__, template_folder="templates/search", static_folder="static", ) SCRIPT_DIR = os.path.dirname(__file__) SEARCH_DATA_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, "searchdata")) @searchpages.route("/", methods=["GET", "POST"]) def searchpage(): searchform = SearchForm() found_distribusis = [] if searchform.validate_on_submit(): found_distribusis = search(searchform.searchfield.data) template = render_template( "search.html", searchform=searchform, found_distribusis=found_distribusis, ) return template def search(searchinput): """search and get search result titles and return them as distribusi ids""" ix = open_dir(SEARCH_DATA_DIR) with ix.searcher() as searcher: query = QueryParser("content", ix.schema).parse(searchinput) search_results = searcher.search(query) found_distribusis = [] for result in search_results: found_distribusis.append(result["title"]) return found_distribusis