csv-library-website/library/search.py

22 lines
691 B
Python
Raw Normal View History

2023-07-14 21:16:30 +02:00
import os
2023-12-03 14:19:53 +01:00
from application.csvparser import CsvParser
2023-07-14 21:16:30 +02:00
from whoosh.fields import *
from whoosh.index import open_dir
from whoosh.qparser import QueryParser
SCRIPT_DIR = os.path.dirname(__file__)
DATA_DIR = os.path.abspath(os.path.join(SCRIPT_DIR, "data"))
def search(searchinput):
"""search and get search result titles and return them as book ids"""
ix = open_dir(DATA_DIR)
with ix.searcher() as searcher:
query = QueryParser("content", ix.schema).parse(searchinput)
search_results = searcher.search(query)
searched_book_ids = []
for book in search_results:
searched_book_ids.append(book["title"])
return searched_book_ids