mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
* Initial work on new search backend * Clean up search backends * Return only the most relevant result per object * Clear any pre-existing cached entries on cache() * #6003: Implement global search functionality for custom field values * Tweak field weights & document guidance * Extend search() to accept a lookup type * Move get_registry() out of SearchBackend * Enforce object permissions when returning search results * Add indexers for remaining models * Avoid calling remove() on non-cacheable objects * Use new search backend by default * Extend search backend to filter by object type * Clean up search view form * Enable specifying lookup logic * Add indexes for value field * Remove object type selector from search bar * Introduce SearchTable and enable HTMX for results * Enable pagination * Remove legacy search backend * Cleanup * Use a UUID for CachedValue primary key * Refactoring search methods * Define max search results limit * Extend reindex command to support specifying particular models * Add clear() and size to SearchBackend * Optimize bulk caching performance * Highlight matched portion of field value * Performance improvements for reindexing * Started on search tests * Cleanup & docs * Documentation updates * Clean up SearchIndex * Flatten search registry to register by app_label.model_name * Clean up search backend classes * Clean up RestrictedGenericForeignKey and RestrictedPrefetch * Resolve migrations conflict
38 lines
2.2 KiB
Markdown
38 lines
2.2 KiB
Markdown
# Search
|
|
|
|
NetBox v3.4 introduced a new global search mechanism, which employs the `extras.CachedValue` model to store discrete field values from many models in a single table.
|
|
|
|
## SearchIndex
|
|
|
|
To enable search support for a model, declare and register a subclass of `netbox.search.SearchIndex` for it. Typically, this will be done within an app's `search.py` module.
|
|
|
|
```python
|
|
from netbox.search import SearchIndex, register_search
|
|
|
|
@register_search
|
|
class MyModelIndex(SearchIndex):
|
|
model = MyModel
|
|
fields = (
|
|
('name', 100),
|
|
('description', 500),
|
|
('comments', 5000),
|
|
)
|
|
```
|
|
|
|
A SearchIndex subclass defines both its model and a list of two-tuples specifying which model fields to be indexed and the weight (precedence) associated with each. Guidance on weight assignment for fields is provided below.
|
|
|
|
### Field Weight Guidance
|
|
|
|
| Weight | Field Role | Examples |
|
|
|--------|--------------------------------------------------|----------------------------------------------------|
|
|
| 50 | Unique serialized attribute | Device.asset_tag |
|
|
| 60 | Unique serialized attribute (per related object) | Device.serial |
|
|
| 100 | Primary human identifier | Device.name, Circuit.cid, Cable.label |
|
|
| 110 | Slug | Site.slug |
|
|
| 200 | Secondary identifier | Provider.account, DeviceType.part_number |
|
|
| 300 | Highly unique descriptive attribute | CircuitTermination.xconnect_id, IPAddress.dns_name |
|
|
| 500 | Description | Site.description |
|
|
| 1000 | Custom field default | - |
|
|
| 2000 | Other discrete attribute | CircuitTermination.port_speed |
|
|
| 5000 | Comment field | Site.comments |
|