1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
#!/usr/bin/python2.6
# coding: utf-8
from whoosh.index import open_dir
from whoosh.qparser import QueryParser
import whoosh.fields as fields
import whoosh.analysis as analysis
from whoosh import highlight
import flask
from flask import Flask
app = Flask("booksearch")
index = open_dir(u"index", mapped=False)
searcher = index.searcher()
@app.route("/search/skip=<int:skip>/<path:term>",methods=["GET"])
@app.route("/search/<path:term>",methods=["GET"])
def do_search(skip=0,term=None):
query = QueryParser("content").parse(term)
results = searcher.search(query, limit=skip+5)
terms = [text for fieldname, text in query.all_terms()
if fieldname == "content"]
objects = []
for result in results[skip:skip+5]:
title = result.get("title")
path = result.get("path")
# high = highlight.highlight(result.get("content"),
# terms,
# analysis.StandardAnalyzer(),
# highlight.SimpleFragmenter(),
# highlight.HtmlFormatter())
objects.append({ 'title' : title, 'path' : path, 'excerpt' : 'TODO' })
return flask.render_template('search.html', objects=objects, term=term, skip=skip)
if __name__ == "__main__":
app.debug = True
app.run(host="0.0.0.0")
|