diff options
Diffstat (limited to 'src/index_service.py')
-rw-r--r-- | src/index_service.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/index_service.py b/src/index_service.py new file mode 100644 index 0000000..5038fce --- /dev/null +++ b/src/index_service.py | |||
@@ -0,0 +1,27 @@ | |||
1 | from whoosh import index | ||
2 | from whoosh.qparser import MultifieldParser | ||
3 | |||
4 | import bottle | ||
5 | from bottle import route, run, request | ||
6 | |||
7 | |||
8 | @route('/search') | ||
9 | def search(): | ||
10 | term = request.query.get("q") | ||
11 | |||
12 | if not term: | ||
13 | return { 'error': 'No search term specified' } | ||
14 | |||
15 | idx = index.open_dir("indexdir") | ||
16 | parser = MultifieldParser( | ||
17 | ["track_name", "artist", "album"], schema=idx.schema) | ||
18 | |||
19 | with idx.searcher() as searcher: | ||
20 | results = searcher.search(parser.parse(term)) | ||
21 | return { 'results': [dict(result) for result in results] } | ||
22 | |||
23 | bottle.debug(True) | ||
24 | app = bottle.default_app() | ||
25 | |||
26 | #if __name__ == "__main__": | ||
27 | run(host='localhost', port=8080) | ||