geminispace.info

gemini search engine
git clone https://git.clttr.info/geminispace.info.git
Log (Feed) | Files | Refs (Tags) | README | LICENSE

commit 075832925cf8cf5743603aef3aed10ba5cce0d0f
parent 02d9691d378fbfde7c672419046fc7cbb40c3a1a
Author: Natalie Pendragon <natpen@natpen.net>
Date:   Wed,  4 Mar 2020 08:08:48 -0500

Add search suggestions

Diffstat:
Mgus/serve.py | 41++++++++++++++++++++++++++++++++++++-----
1 file changed, 36 insertions(+), 5 deletions(-)

diff --git a/gus/serve.py b/gus/serve.py @@ -73,6 +73,25 @@ def _search_index(query): ) +def _get_search_suggestions(query): + suggestions = [] + corrector = searcher.corrector("content") + for query_part in query.split(" "): + suggestions.extend(corrector.suggest(query_part, limit=3)) + return suggestions + +def _render_search_suggestions(suggestions): + data = [] + if len(suggestions) == 0: + data.append("No results, which means it's time") + data.append("to go create some hot new content!") + return data + data.append("Search suggestions:") + for suggestion in suggestions: + data.append("=> gemini://gus.guru/search?{} GUS: {}".format(suggestion, suggestion)) + return data + + def _render_results(results): data = [] for i, result in enumerate(results): @@ -93,22 +112,34 @@ def _render_results_header(query, num_results): def _render_results_footer(num_results): - return [ + data = [ "", "==========================", - "Page 1 of {} (paging coming later)".format(math.ceil(num_results / 10)) ] + num_pages = math.ceil(num_results / 10) + current_page = 1 + if num_results == 0: + current_page = 0 + paging_feature_note = "" + if num_pages > 1: + paging_feature_note = " (paging coming soon)" + data.append("Page {} of {}{}".format(current_page, num_pages, paging_feature_note)) + return data @app.route("/search") def search(request): data = _render_header() if request.query: + # TODO(np): do we need any query cleansing? num_results, results = _search_index(request.query) - print(num_results) - print(results) data.extend(_render_results_header(request.query, num_results)) - data.extend(_render_results(results)) + if num_results > 0: + data.extend(_render_results(results)) + else: + search_suggestions = _get_search_suggestions(request.query) + data.extend(_render_search_suggestions(search_suggestions)) + data.extend(_render_results_footer(num_results)) data.extend(_render_footer())