changeset 372:d379a6a601b1

search: Display non-HTML highlighting in command-line.
author Ludovic Chabant <ludovic@chabant.com>
date Thu, 24 Sep 2015 20:33:30 -0700
parents 4b3f867a98b0
children 49953d5cef0f
files wikked/commands/query.py wikked/indexer/elastic.py wikked/indexer/whooshidx.py
diffstat 3 files changed, 16 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/wikked/commands/query.py	Thu Sep 24 20:22:20 2015 -0700
+++ b/wikked/commands/query.py	Thu Sep 24 20:33:30 2015 -0700
@@ -66,14 +66,19 @@
         self.description = "Searches the wiki."
 
     def setupParser(self, parser):
-        parser.add_argument('query',
+        parser.add_argument(
+                'query',
                 help="The search query",
                 nargs='+')
 
     def run(self, ctx):
         query = ' '.join(ctx.args.query)
-        hits = ctx.wiki.index.search(query)
-        logger.info(hits)
+        hits = ctx.wiki.index.search(query, highlight=False)
+        if not hits:
+            logger.info("No pages found.")
+        else:
+            for h in hits:
+                logger.info("[[%s]]: %s" % (h.url, h.hl_text))
 
 
 @register_command
--- a/wikked/indexer/elastic.py	Thu Sep 24 20:22:20 2015 -0700
+++ b/wikked/indexer/elastic.py	Thu Sep 24 20:33:30 2015 -0700
@@ -189,7 +189,7 @@
         for h in res['hits']['hits']:
             yield HitResult(h['fields']['url'], h['highlight']['title_preview'])
 
-    def search(self, query):
+    def search(self, query, highlight=False):
         body = {
                 'fields': ['url', 'title', 'text'],
                 'query': {
@@ -214,8 +214,9 @@
                 doc_type='page',
                 body=body)
         for h in res['hits']['hits']:
-            yield HitResult(h['fields']['url'], h['fields']['title'],
-                    h['highlight']['text'])
+            yield HitResult(h['fields']['url'],
+                            h['fields']['title'],
+                            h['highlight']['text'])
 
     def _get_body(self, page):
         return {
--- a/wikked/indexer/whooshidx.py	Thu Sep 24 20:22:20 2015 -0700
+++ b/wikked/indexer/whooshidx.py	Thu Sep 24 20:33:30 2015 -0700
@@ -5,7 +5,7 @@
 from whoosh.analysis import (StandardAnalyzer, StemmingAnalyzer,
         CharsetFilter, NgramFilter)
 from whoosh.fields import Schema, ID, TEXT, STORED
-from whoosh.highlight import WholeFragmenter
+from whoosh.highlight import WholeFragmenter, UppercaseFormatter
 from whoosh.index import create_in, open_dir
 from whoosh.qparser import QueryParser
 from whoosh.support.charset import accent_map
@@ -86,12 +86,14 @@
                 hits.append(hit)
             return hits
 
-    def search(self, query):
+    def search(self, query, highlight=True):
         with self.ix.searcher() as searcher:
             title_qp = QueryParser("title", self.ix.schema).parse(query)
             text_qp = QueryParser("text", self.ix.schema).parse(query)
             comp_query = title_qp | text_qp
             results = searcher.search(comp_query)
+            if not highlight:
+                results.formatter = UppercaseFormatter()
 
             hits = []
             for result in results: