Skip to content

Display additional object attributes in global search results #14134

@jeremystretch

Description

@jeremystretch

NetBox version

v3.6.4

Feature type

Change to existing functionality

Proposed functionality

This FR proposes extending NetBox's global search feature to include certain additional object attributes when display global search results. (This does not impact model-specific filters.) Several extensions are necessary to implement this in a feasible manner.

screenshot

1. Define display attributes on each SearchIndex class

The display_attrs attribute will define the additional attributes (if any) to include alongside search results. For example:

@register_search
class DeviceIndex(SearchIndex):
    model = models.Device
    fields = (
        ('asset_tag', 50),
        ('serial', 60),
        ('name', 100),
        ('description', 500),
        ('comments', 5000),
    )
    display_attrs = ('site', 'location', 'rack', 'description')

This tells NetBox to also include the site, location, rack, and description for each global search result returning a device. Attributes will be included only if the object has a non-empty value for the specified field.

Although these attributes don't directly impact the search engine itself, IMO we should set them on the indexers (as opposed to the models) to maintain a clean API.

2. Extend search backend to support prefetching of display attributes

Prefetching related objects is necessary to ensure a reasonably performant implementation. Using the example above, each matching device would trigger up to three discrete SQL queries (on each for site, location, and device) without prefetching enabled. (This is necessary only for related objects: Concrete fields will have already been populated while retrieving the object associated with each result.)

We should be able to employ Django's prefetch_related_objects() utility function to attach prefetched objects to the results list after it has been retrieved. However, because global search results are heterogeneous, we must manually attach prefetched objects per result type. This will require calling prefetch_related_objects() on subsets of the results list organized by content type.

3. Determine how to display the additional attributes

Assuming we can efficiently retrieve the attributes, we still need to determine the best way to incorporate them into the search results. I see several options:

  1. Introduce a single column to hold all attributes per result.
  2. List all results in a separate row beneath each result.
  3. Dynamically add a table column for each attribute across all results.

There are pros and cons to each approach, however I don't consider option 3 truly viable, as it could generate very large tables when many types of objects are returned, and would be very difficult for the user to read. Option 1 (shown above) is probably the simplest.

Use case

These attributes are intended to convey additional context to the user where readily distinguishing between similar search results may be difficult. For example, multiple results might each reference a "VLAN 100," each of which is assigned to a different site. This enhancement provides the context necessary for the user to see the site to which each VLAN is assigned without needing to navigate to each object individually.

Database changes

I don't believe any changes to the database schema are necessary to support this functionality. We probably won't even need to trigger a re-indexing of cached values.

External dependencies

None

Metadata

Metadata

Assignees

Labels

status: acceptedThis issue has been accepted for implementationtype: featureIntroduction of new functionality to the application

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions