Commits

Anonymous committed c5f6345

added hit highlighting.

Comments (0)

Files changed (1)

 
 
 def search(ix, querystr):
+    from codecs import open
     from whoosh.qparser import QueryParser
+    from whoosh import highlight
 
     querystr = unicode(querystr)
     print u"Search '{0}'".format(querystr)
     with ix.searcher() as searcher:
         query = QueryParser("content", ix.schema).parse(querystr)
         results = searcher.search(query)
+        results.formatter = highlight.UppercaseFormatter()
         print results
-        for r in results:
-            print r
+        for hit in results:
+            path = hit['path']
+            print
+            print path
+            print hit.highlights("content",
+                                 open(path, 'r', 'utf-8').read())
 
 
 def main():