From 169438137ffe3d28bd855090960751e6804adea9 Mon Sep 17 00:00:00 2001 From: Markus Heiser Date: Sat, 3 Apr 2021 18:18:50 +0200 Subject: [PATCH] [fix] url bar autocomplete (opensearch suggestions) Since #2593 is merged the OpenSearch-Format is buggy. The loop in [1] will change raw_text_query object and this will change also the value of `raw_text_query.query` on every `raw_text_query.changeQuery(result)`. This patch fixes this issue by storing the initial query value in `sug_prefix`. [1] https://github.com/searx/searx/blob/ac0fdc3b9670168de8061ed0e656dd66a567d741/searx/webapp.py#L804-L806 OpenSearch-Format:: [ "", [ "", "", ... "" ], [ "", "", ..., "" ], [ "", "", ..., "" ] ] - https://www.google.com/support/enterprise/static/gsa/docs/admin/current/gsa_doc_set/xml_reference/query_suggestion.html#1080002 - https://developer.mozilla.org/en-US/docs/Archive/Add-ons/Supporting_search_suggestions_in_search_plugins#implementing_search_suggestion_support_on_the_server Legacy-Format:: [ "", "", ..., "" ] - https://www.google.com/support/enterprise/static/gsa/docs/admin/current/gsa_doc_set/xml_reference/query_suggestion.html#1081079 Signed-off-by: Markus Heiser --- searx/webapp.py | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/searx/webapp.py b/searx/webapp.py index b4466c71..cb5183d0 100755 --- a/searx/webapp.py +++ b/searx/webapp.py @@ -785,20 +785,26 @@ def autocompleter(): # parse query raw_text_query = RawTextQuery(request.form.get('q', ''), disabled_engines) + sug_prefix = raw_text_query.getQuery() # normal autocompletion results only appear if no inner results returned # and there is a query part - if len(raw_text_query.autocomplete_list) == 0 and len(raw_text_query.getQuery()) > 0: + if len(raw_text_query.autocomplete_list) == 0 and len(sug_prefix) > 0: + # get language from cookie language = request.preferences.get_value('language') if not language or language == 'all': language = 'en' else: language = language.split('-')[0] + # run autocompletion - raw_results = search_autocomplete(request.preferences.get_value('autocomplete'), - raw_text_query.getQuery(), language) + raw_results = search_autocomplete( + request.preferences.get_value('autocomplete'), sug_prefix, language + ) for result in raw_results: + # attention: this loop will change raw_text_query object and this is + # the reason why the sug_prefix was stored before (see above) results.append(raw_text_query.changeQuery(result).getFullQuery()) if len(raw_text_query.autocomplete_list) > 0: @@ -809,13 +815,16 @@ def autocompleter(): for answer in answers: results.append(str(answer['answer'])) - # return autocompleter results if request.headers.get('X-Requested-With') == 'XMLHttpRequest': - return Response(json.dumps(results), - mimetype='application/json') + # the suggestion request comes from the searx search form + suggestions = json.dumps(results) + mimetype = 'application/json' + else: + # the suggestion request comes from browser's URL bar + suggestions = json.dumps([sug_prefix, results]) + mimetype = 'application/x-suggestions+json' - return Response(json.dumps([raw_text_query.query, results]), - mimetype='application/x-suggestions+json') + return Response(suggestions, mimetype=mimetype) @app.route('/preferences', methods=['GET', 'POST'])