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
This commit is contained in:
capric98 2022-04-12 18:00:06 +08:00 committed by mrpaulblack
parent e22dc2ba36
commit 92c9ee31a0
4 changed files with 38 additions and 2 deletions

View file

@ -18,6 +18,12 @@ from searx.exceptions import SearxEngineResponseException
# a fetch_supported_languages() for XPath engines isn't available right now # a fetch_supported_languages() for XPath engines isn't available right now
# _brave = ENGINES_LANGUAGES['brave'].keys() # _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): def get(*args, **kwargs):
if 'timeout' not in 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): def search_autocomplete(backend_name, query, lang):
backend = backends.get(backend_name) backend = backends.get(backend_name)
if backend is None: if backend is None or not meet_limit(backend_name, query):
return [] return []
try: try:

View file

@ -47,6 +47,20 @@ server:
base_url: false # Possible values: false or "https://example.org/location". 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 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 # If your instance owns a /etc/searxng/settings.yml file, then set the following
# values there. # values there.

View file

@ -170,6 +170,7 @@ SCHEMA = {
'http_protocol_version': SettingsValue(('1.0', '1.1'), '1.0'), 'http_protocol_version': SettingsValue(('1.0', '1.1'), '1.0'),
'method': SettingsValue(('POST', 'GET'), 'POST'), 'method': SettingsValue(('POST', 'GET'), 'POST'),
'default_http_headers': SettingsValue(dict, {}), 'default_http_headers': SettingsValue(dict, {}),
'autocomplete_limit': SettingsValue(dict, {"min":0, "max": 50}),
}, },
'redis': { 'redis': {
'url': SettingsValue(str, 'unix:///usr/local/searxng-redis/run/redis.sock?db=0'), 'url': SettingsValue(str, 'unix:///usr/local/searxng-redis/run/redis.sock?db=0'),

View file

@ -68,7 +68,7 @@
"Content-type": "application/x-www-form-urlencoded", "Content-type": "application/x-www-form-urlencoded",
"X-Requested-With": "XMLHttpRequest" "X-Requested-With": "XMLHttpRequest"
}, },
MinChars: 4, MinChars: 0,
Delay: 300, Delay: 300,
_Position: function () {}, _Position: function () {},
_Open: function () { _Open: function () {