# HG changeset patch # User Ludovic Chabant # Date 1443152010 25200 # Node ID d379a6a601b15918066da63f12af1115d152fb2f # Parent 4b3f867a98b0c689e887ee1e9ca7fec15d46e672 search: Display non-HTML highlighting in command-line. diff -r 4b3f867a98b0 -r d379a6a601b1 wikked/commands/query.py --- 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 diff -r 4b3f867a98b0 -r d379a6a601b1 wikked/indexer/elastic.py --- 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 { diff -r 4b3f867a98b0 -r d379a6a601b1 wikked/indexer/whooshidx.py --- 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: