From 92c9ee31a02ddbc1822122deb00f6ee98c09c633 Mon Sep 17 00:00:00 2001 From: capric98 <42015599+capric98@users.noreply.github.com> Date: Tue, 12 Apr 2022 18:00:06 +0800 Subject: [PATCH] change: themes.simple.autocompleter.MinChars 4 -> 0 add: settings.server.autocomplete_limit add settings.server.autocomplete_limit to SCHEMA add limit to autocomplete.py change: default value of autocomplete_limit 0-50 fix: pylint C0325 R1719 R1735 --- searx/autocomplete.py | 23 ++++++++++++++++++- searx/settings.yml | 14 +++++++++++ searx/settings_defaults.py | 1 + .../themes/simple/src/js/main/search.js | 2 +- 4 files changed, 38 insertions(+), 2 deletions(-) diff --git a/searx/autocomplete.py b/searx/autocomplete.py index ff299d184..779cd501b 100644 --- a/searx/autocomplete.py +++ b/searx/autocomplete.py @@ -18,6 +18,12 @@ from searx.exceptions import SearxEngineResponseException # a fetch_supported_languages() for XPath engines isn't available right now # _brave = ENGINES_LANGUAGES['brave'].keys() +_autocomplete_limit = settings['server']['autocomplete_limit'] +_autocomplete_limit_enabled = bool(_autocomplete_limit) +# Make sure _autocomplete_limit['overwrite'] is always a dict. +if not hasattr(_autocomplete_limit, 'get'): + _autocomplete_limit['overwrite'] = {} + def get(*args, **kwargs): if 'timeout' not in kwargs: @@ -158,9 +164,24 @@ backends = { } +def meet_limit(backend_name, query) -> bool: + if _autocomplete_limit_enabled: + backend_limit = _autocomplete_limit['overwrite'].get( + backend_name, _autocomplete_limit + ) + limit_min = backend_limit.get("min", 0) + limit_max = backend_limit.get("max", 0x7FFFFFFF) + + return limit_min <= len(query) <= limit_max + + # If autocomplete_limit is not configured, + # allow all autocomplete requests. + return True + + def search_autocomplete(backend_name, query, lang): backend = backends.get(backend_name) - if backend is None: + if backend is None or not meet_limit(backend_name, query): return [] try: diff --git a/searx/settings.yml b/searx/settings.yml index 2976f1c83..08be71162 100644 --- a/searx/settings.yml +++ b/searx/settings.yml @@ -47,6 +47,20 @@ server: base_url: false # Possible values: false or "https://example.org/location". limiter: false # rate limit the number of request on the instance, block some bots + # The autocompleter will only return suggestions when: + # min < len(query) < max + # otherwise it will simply return a null list. + # {{ backend_name }} can be any existing autocomplete backends. + # The values settings in this section will overwrite the default + # value when use the corresponding backend. + autocomplete_limit: + min: 0 + max: 50 + # overwrite: + # "{{ backend_name }}": + # min: 4 + # max: 16 + # If your instance owns a /etc/searxng/settings.yml file, then set the following # values there. diff --git a/searx/settings_defaults.py b/searx/settings_defaults.py index e3efb8544..2a24295c8 100644 --- a/searx/settings_defaults.py +++ b/searx/settings_defaults.py @@ -170,6 +170,7 @@ SCHEMA = { 'http_protocol_version': SettingsValue(('1.0', '1.1'), '1.0'), 'method': SettingsValue(('POST', 'GET'), 'POST'), 'default_http_headers': SettingsValue(dict, {}), + 'autocomplete_limit': SettingsValue(dict, {"min":0, "max": 50}), }, 'redis': { 'url': SettingsValue(str, 'unix:///usr/local/searxng-redis/run/redis.sock?db=0'), diff --git a/searx/static/themes/simple/src/js/main/search.js b/searx/static/themes/simple/src/js/main/search.js index 798c9b2d3..cb5f64052 100644 --- a/searx/static/themes/simple/src/js/main/search.js +++ b/searx/static/themes/simple/src/js/main/search.js @@ -68,7 +68,7 @@ "Content-type": "application/x-www-form-urlencoded", "X-Requested-With": "XMLHttpRequest" }, - MinChars: 4, + MinChars: 0, Delay: 300, _Position: function () {}, _Open: function () {