Skip to main content
Community

Search

Plugins can define and register their own models to extend NetBox's core search functionality. Typically, a plugin will include a file named search.py, which holds all search indexes for its models.

search.py
# search.py
from netbox.search import SearchIndex, register_search

from .models import MyModel

@register_search
class MyModelIndex(SearchIndex):
model = MyModel
fields = (
('name', 100),
('description', 500),
('comments', 5000),
)
display_attrs = ('site', 'device', 'status', 'description')

Decorate each SearchIndex subclass with @register_search to register it with NetBox. When using the default search.py module, no additional indexes = [...] list is required.

Fields listed in display_attrs are not cached for matching, but they are displayed alongside the object in global search results to provide additional context.

tip

The legacy indexes = [...] list remains supported via PluginConfig.search_indexes for backward compatibility and custom loading patterns.

SearchIndex

Base class for building search indexes.

Attributes:

NameTypeDescription
modelAnyThe model class for which this index is used.
categoryAnyThe label of the group under which this indexer is categorized (for form field display). If none, the name of the model's app will be used.
fieldsAnyAn iterable of two-tuples defining the model fields to be indexed and the weight associated with each.
display_attrsAnyAn iterable of additional object attributes to include when displaying search results.

get_field_type(instance, field_name)

Return the data type of the specified model field.

Parameters:

NameTypeDescriptionDefault
instanceAnyNo description available-
field_nameAnyNo description available-

get_attr_type(instance, field_name)

Return the data type of the specified object attribute.

Parameters:

NameTypeDescriptionDefault
instanceAnyNo description available-
field_nameAnyNo description available-

get_field_value(instance, field_name)

Return the value of the specified model field as a string (or None).

Parameters:

NameTypeDescriptionDefault
instanceAnyNo description available-
field_nameAnyNo description available-

to_cache(cls, instance, custom_fields=None)

Return a list of ObjectFieldValue representing the instance fields to be cached.

Parameters:

NameTypeDescriptionDefault
clsAnyNo description available-
instanceAnyThe instance being cached.-
custom_fieldsAnyAn iterable of CustomFields to include when caching the instance. If None, all custom fields defined for the model will be included. (This can also be provided during bulk caching to avoid looking up the available custom fields for each instance.)None