csv-library-website/library/application/search.py

23 lines
695 B
Python

import os
from whoosh.fields import *
from whoosh.index import open_dir
from whoosh.qparser import QueryParser
from application.csvparser import CsvParser
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