mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-25 00:36:11 -06:00
Refactoring search methods
This commit is contained in:
parent
81876626f6
commit
b216526e01
@ -60,9 +60,10 @@ class SearchIndex:
|
|||||||
for name, weight in cls.fields:
|
for name, weight in cls.fields:
|
||||||
type_ = cls.get_field_type(instance, name)
|
type_ = cls.get_field_type(instance, name)
|
||||||
value = cls.get_field_value(instance, name)
|
value = cls.get_field_value(instance, name)
|
||||||
values.append(
|
if type_ and value:
|
||||||
ObjectFieldValue(name, type_, weight, value)
|
values.append(
|
||||||
)
|
ObjectFieldValue(name, type_, weight, value)
|
||||||
|
)
|
||||||
|
|
||||||
# Capture custom fields
|
# Capture custom fields
|
||||||
if hasattr(instance, 'custom_field_data'):
|
if hasattr(instance, 'custom_field_data'):
|
||||||
@ -70,11 +71,10 @@ class SearchIndex:
|
|||||||
type_ = cf.search_type
|
type_ = cf.search_type
|
||||||
value = instance.custom_field_data.get(cf.name)
|
value = instance.custom_field_data.get(cf.name)
|
||||||
weight = cf.search_weight
|
weight = cf.search_weight
|
||||||
if not type_ or not value or not weight:
|
if type_ and value and weight:
|
||||||
continue
|
values.append(
|
||||||
values.append(
|
ObjectFieldValue(f'cf_{cf.name}', type_, weight, value)
|
||||||
ObjectFieldValue(f'cf_{cf.name}', type_, weight, value)
|
)
|
||||||
)
|
|
||||||
|
|
||||||
return values
|
return values
|
||||||
|
|
||||||
|
@ -60,28 +60,17 @@ class SearchBackend:
|
|||||||
"""
|
"""
|
||||||
Receiver for the post_save signal, responsible for caching object creation/changes.
|
Receiver for the post_save signal, responsible for caching object creation/changes.
|
||||||
"""
|
"""
|
||||||
try:
|
cls.cache(instance)
|
||||||
indexer = get_indexer(instance)
|
|
||||||
except KeyError:
|
|
||||||
# No indexer has been registered for this model
|
|
||||||
return
|
|
||||||
data = indexer.to_cache(instance)
|
|
||||||
cls.cache(instance, data)
|
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def removal_handler(cls, sender, instance, **kwargs):
|
def removal_handler(cls, sender, instance, **kwargs):
|
||||||
"""
|
"""
|
||||||
Receiver for the post_delete signal, responsible for caching object deletion.
|
Receiver for the post_delete signal, responsible for caching object deletion.
|
||||||
"""
|
"""
|
||||||
try:
|
|
||||||
get_indexer(instance)
|
|
||||||
except KeyError:
|
|
||||||
# Avoid attempting to query for non-cacheable objects
|
|
||||||
return
|
|
||||||
cls.remove(instance)
|
cls.remove(instance)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def cache(cls, instance, data):
|
def cache(cls, instance):
|
||||||
"""
|
"""
|
||||||
Create or update the cached representation of an instance.
|
Create or update the cached representation of an instance.
|
||||||
"""
|
"""
|
||||||
@ -140,8 +129,15 @@ class CachedValueSearchBackend(SearchBackend):
|
|||||||
]
|
]
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def cache(cls, instance, data):
|
def cache(cls, instance):
|
||||||
|
try:
|
||||||
|
indexer = get_indexer(instance)
|
||||||
|
except KeyError:
|
||||||
|
# No indexer has been registered for this model
|
||||||
|
return
|
||||||
|
|
||||||
ct = ContentType.objects.get_for_model(instance)
|
ct = ContentType.objects.get_for_model(instance)
|
||||||
|
data = indexer.to_cache(instance)
|
||||||
|
|
||||||
# Wipe out any previously cached values for the object
|
# Wipe out any previously cached values for the object
|
||||||
cls.remove(instance)
|
cls.remove(instance)
|
||||||
@ -149,8 +145,6 @@ class CachedValueSearchBackend(SearchBackend):
|
|||||||
# Record any new non-empty values
|
# Record any new non-empty values
|
||||||
cached_values = []
|
cached_values = []
|
||||||
for field in data:
|
for field in data:
|
||||||
if not field.value:
|
|
||||||
continue
|
|
||||||
cached_values.append(
|
cached_values.append(
|
||||||
CachedValue(
|
CachedValue(
|
||||||
object_type=ct,
|
object_type=ct,
|
||||||
@ -165,6 +159,12 @@ class CachedValueSearchBackend(SearchBackend):
|
|||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def remove(cls, instance):
|
def remove(cls, instance):
|
||||||
|
# Avoid attempting to query for non-cacheable objects
|
||||||
|
try:
|
||||||
|
get_indexer(instance)
|
||||||
|
except KeyError:
|
||||||
|
return
|
||||||
|
|
||||||
ct = ContentType.objects.get_for_model(instance)
|
ct = ContentType.objects.get_for_model(instance)
|
||||||
CachedValue.objects.filter(object_type=ct, object_id=instance.pk).delete()
|
CachedValue.objects.filter(object_type=ct, object_id=instance.pk).delete()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user