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 = [] found_distribusi_files = [] if searchform.validate_on_submit(): found_distribusis, found_distribusi_files = search( searchform.searchfield.data ) template = render_template( "search.html", searchform=searchform, found_distribusis=found_distribusis, found_distribusi_files=found_distribusi_files, ) 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) for result in search_results: print(result["title"]) print(result["path"]) found_distribusis = [] found_distribusi_files = [] for result in search_results: 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