diff --git a/netbox/netbox/constants.py b/netbox/netbox/constants.py index 776938a97..c8054b3b0 100644 --- a/netbox/netbox/constants.py +++ b/netbox/netbox/constants.py @@ -1,5 +1,2 @@ # Prefix for nested serializers NESTED_SERIALIZER_PREFIX = 'Nested' - -# Max results per object type -SEARCH_MAX_RESULTS = 15 diff --git a/netbox/netbox/search/__init__.py b/netbox/netbox/search/__init__.py index 24cabc2cd..edd31b2b0 100644 --- a/netbox/netbox/search/__init__.py +++ b/netbox/netbox/search/__init__.py @@ -20,15 +20,6 @@ class LookupTypes: ENDSWITH = 'iendswith' -def get_registry(): - r = {} - for app_label, models in registry['search'].items(): - for model_name, idx in models.items(): - r[f'{app_label}.{model_name}'] = idx - - return r - - class SearchIndex: """ Base class for building search indexes. @@ -88,14 +79,14 @@ class SearchIndex: return values -class SearchResult: +def get_indexer(model): """ - Represents a single result returned by a search backend's search() method. + Get the search indexer class for the given model. """ - def __init__(self, obj, field=None, value=None): - self.object = obj - self.field = field - self.value = value + app_label = model._meta.app_label + model_name = model._meta.model_name + + return registry['search'][app_label][model_name] def register_search(): diff --git a/netbox/netbox/search/backends.py b/netbox/netbox/search/backends.py index d48ee4356..a033f5f89 100644 --- a/netbox/netbox/search/backends.py +++ b/netbox/netbox/search/backends.py @@ -10,29 +10,13 @@ from django.db.models.signals import post_delete, post_save from extras.models import CachedValue from extras.registry import registry -from netbox.constants import SEARCH_MAX_RESULTS from utilities.querysets import RestrictedPrefetch from utilities.templatetags.builtins.filters import bettertitle -from . import FieldTypes, LookupTypes, SearchResult, get_registry - -# The cache for the initialized backend. -_backends_cache = {} +from . import FieldTypes, LookupTypes, get_indexer DEFAULT_LOOKUP_TYPE = LookupTypes.PARTIAL -def get_indexer(model): - app_label = model._meta.app_label - model_name = model._meta.model_name - - return registry['search'][app_label][model_name] - - -class SearchEngineError(Exception): - """Something went wrong with a search engine.""" - pass - - class SearchBackend: """A search engine capable of performing multi-table searches.""" _search_choice_options = tuple() @@ -111,54 +95,6 @@ class SearchBackend: raise NotImplementedError -class FilterSetSearchBackend(SearchBackend): - """ - Legacy search backend. Performs a discrete database query for each registered object type, using the FilterSet - class specified by the index for each. - """ - def search(self, request, value, object_types=None, lookup=DEFAULT_LOOKUP_TYPE): - results = [] - - search_registry = get_registry() - if object_types is not None: - keys = [f'{ct.app_label}.{ct.name}' for ct in object_types] - else: - keys = search_registry.keys() - - for obj_type in keys: - - queryset = getattr(search_registry[obj_type], 'queryset', None) - if not queryset: - continue - - # Restrict the queryset for the current user - if hasattr(queryset, 'restrict'): - queryset = queryset.restrict(request.user, 'view') - - filterset = getattr(search_registry[obj_type], 'filterset', None) - if not filterset: - # This backend requires a FilterSet class for the model - continue - - queryset = filterset({'q': value}, queryset=queryset).qs[:SEARCH_MAX_RESULTS] - - results.extend([ - SearchResult(obj) for obj in queryset - ]) - - return results - - @classmethod - def cache(cls, instance, data): - # This backend does not utilize a cache - pass - - @classmethod - def remove(cls, instance): - # This backend does not utilize a cache - pass - - class CachedValueSearchBackend(SearchBackend): def search(self, request, value, object_types=None, lookup=None):