-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
46 lines (32 loc) · 1.23 KB
/
search.py
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
41
42
43
44
45
46
import cgi
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext.webapp import template
from whoosh import store
from whoosh.fields import Schema, STORED, ID, KEYWORD, TEXT
from whoosh.index import getdatastoreindex
from whoosh.qparser import QueryParser, MultifieldParser
import logging
SEARCHSCHEMA = Schema(title=TEXT(stored=True), path=ID(stored=True), content=TEXT(stored=True))
class SearchPage(webapp.RequestHandler):
def get(self):
ix = getdatastoreindex("hello", schema=SEARCHSCHEMA)
parser = QueryParser("content", schema = ix.schema)
q = parser.parse(self.request.get('query'))
results = ix.searcher().search(q)
r=[]
for result in results:
rr={"name":result['title'], "id":result['path']}
r.append(rr)
template_values = {"results":r}
path = os.path.join(os.path.dirname(__file__), 'search.html')
self.response.out.write(template.render(path, template_values))
application = webapp.WSGIApplication(
[
('/search', SearchPage)],
debug=True)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()