summaryrefslogtreecommitdiff
path: root/searx/webapp.py
diff options
context:
space:
mode:
authorMarkus Heiser <markus.heiser@darmarit.de>2021-04-03 18:18:50 +0200
committerMarkus Heiser <markus.heiser@darmarit.de>2021-04-03 18:18:50 +0200
commit169438137ffe3d28bd855090960751e6804adea9 (patch)
tree56a51960d5467c70951aee34365fea3d3904a336 /searx/webapp.py
parentac0fdc3b9670168de8061ed0e656dd66a567d741 (diff)
downloadsearxng-169438137ffe3d28bd855090960751e6804adea9.tar.gz
searxng-169438137ffe3d28bd855090960751e6804adea9.zip
[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:: [ "<query>", [ "<term 1>", "<term 2>", ... "<term n>" ], [ "<content 1>", "<content 2>", ..., "<content n>" ], [ "<url 1>", "<url 2>", ..., "<url n>" ] ] - 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:: [ "<term 1>", "<term 2>", ..., "<term n>" ] - 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 <markus.heiser@darmarit.de>
Diffstat (limited to 'searx/webapp.py')
-rwxr-xr-xsearx/webapp.py25
1 files changed, 17 insertions, 8 deletions
diff --git a/searx/webapp.py b/searx/webapp.py
index b4466c718..cb5183d06 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'])