Skip to content

Instantly share code, notes, and snippets.

@mozillalives
Created August 17, 2012 02:47
Show Gist options
  • Save mozillalives/3375468 to your computer and use it in GitHub Desktop.
Save mozillalives/3375468 to your computer and use it in GitHub Desktop.
Search and Serve
from whoosh import index, qparser
def search(blah, page, length):
ix = index.open_dir(INDEX_DIR)
with ix.searcher() as s:
q = qparser.QueryParser("content", ix.schema).parse(blah)
r = s.search_page(q, page, pagelen=length)
t = []
for res in r:
t.append({'field1': res['field1'],
'field2': res['field2'],
'highlights': res.highlights("content")})
return t
import search
class SearchHandler(RequestHandler):
@tornado.web.authenticated
def get(self):
query = self.get_argument("q")
page = int(self.get_argument("p", '1'))
results = search.search(query, page, 10)
self.render("templates/searchresults.html", results=results)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment