-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement an improved global search engine #10560
Comments
I know next to nothing about Redis other than it's an "in memory database" but would holding this table or parts of it in Redis rather than in a PostgreSQL table somehow offer anything? |
Second on the redis comment, redis tends to be very good at this type of lookup, it would also have the advantage of not growing the "main" databases size (assuming that this new caching table would exist in the same db) If the decision is made to build a caching table, would it make sense to fork one of djangos existing caching backend modules to leverage this? ( eg removing the MAX_ENTRIES limits ) |
Potentially. At this early stage I'm primarily interested in proving viability of the approach, and less concerned with performance. Once we have a working prototype in place using PostgreSQL, we should have a better idea of whether it's feasible to swap in Redis for the cache and what the trade-offs will be. |
Just curious, rather than inventing your own search .. what is the reluctance to leverage an existing extension, dependency or feature set from another tool to accomplish this? In the last community call you mentioned this global search and discussed the fact that some users have environments that prevent them from installing additional dependencies. If that is the case, why should the entire user base not be able to take advantage because some environments are more restrictive than others? Is there any way to make this feature something that can be 'turned on' or 'turned off' either by settings, or the detection of these additional dependencies? If you don't have them, search continues to function the way it does now. If you do, then you get to take advantage of global search? |
I've made excellent progress with the new search implementation, and performance testing has been very encouraging. (Searching against 1+ million cached values for a partial match takes ~140ms on my local instance.) For now at least, we'll keep the cache in PostgreSQL as this affords advanced querying functionality not available with Redis. Still to do:
|
How might the search work syntax wise? |
As an addendum to the above. Would glob or regex based searching be possible? |
The initial implementation (pre-beta) does not introduce any sort of advanced query language. The search backend supports specification of a general lookup logic (e.g. partial vs. exact match) but nothing beyond that. While we can certainly consider something, it would be best to do so under a separate FR. |
* 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
Closing this out as the initial implementation of the new backend has been completed. We're likely to continue iterating on it up to and throughout the beta release. |
Looking forward to this! |
NetBox version
v3.3.4
Feature type
Change to existing functionality
Proposed functionality
This issue is an evolution of the proposal initially outlined in #7016 to improve NetBox's global search functionality, in conjunction with dynamic registration per #8927. This proposal suggests the introduction of a new global search cache, allowing a single database per search query. As this is a fairly complex topic, I've outlined some core areas of focus below. While not a complete implementation plan, it should be sufficient to get started and generate additional discussion.
Caching
Each registered model will declare which of its fields should be cached for search. (This could be done similar to what we currently do with
clone_fields
and theclone()
method.) Fields would be prescribed by name and numeric weighting. For example:On
save()
, the model'scache()
method (name TBD) would be called via apost_save
signal handler to generate cache data. A new low-priority background task would then be created to feed this data into the search results table. (Any existing results for the referenced object would first be deleted.) Similarly, search results will be automatically deleted in response to apost_delete
signal.Database Schema
Cached search data from all models would be written to a single table:
The
object_type
andobject_id
fields would serve a GenericForeignKey namedobject
, which references the cached object.A populated table might look like this:
Searching for "akron" would return four rows. We can append
.distinct('object_type', 'object_id')
to ensure only a single row is returned per object, and we can use.order_by('-weight')
to favor the most important result for each object. (We might further order by object type for consistency among objects with identical weights.)Matching Logic
We could potentially add an
exact
boolean column to the table, indicating whether each result requires an exact (vs. partial) match. This could be useful for e.g. integer values, where partial matching is typically of little value. For example, we might only want to find exact matches for a device'sserial
orasset_tag
values. Such a query would look like this:It remains to be seen what the performance penalty of this approach looks like. We could also expose exactness as a toggle, enabling the user to search only for exact matches.
Displaying Results
Each matching result will include several attributes:
These can be displayed to the user to convey a succinct understanding of why each object was included in the results. Although resolving the object required a GenericForeignKey lookup, this should be automatically reduced via
prefetch_related()
to a single additional query per type of object returned.Handling Model Migrations
Some housekeeping will be necessary to delete from the cache search results which reference fields which have been removed from their models. We should be able to hook into the
post_migrate
signal to detect when migrations have been applied, and bulk delete any entries which reference a field that is no longer referenced under its model'ssearch_fields
attribute. A similar approach may be used to detect removed models (e.g. because a plugin was uninstalled).Considerations
Advantages
timestamp
column can be compared against an object'slast_updated
time to identify stale results.Disadvantages
Use case
The overall goal here is to provide more robust general purpose search for both core NetBox models as well as those delivered via plugins. Performance, while important, is probably less important than implementing a consistent, flexible search engine.
Database changes
Defined above for clarity
External dependencies
None
The text was updated successfully, but these errors were encountered: