Site Search¶
Elasticsearch are used to search and aggregate results when users queries the content stored in Lego. We watch model changes using django signals and indexes changes in async celery tasks.
The application exposes two endpoints used to search content:
/api/v1/search-search/that returns the result of an actual search query used to populate a results page./api/v1/search-autocomplete/that returns a list of possible items to fill in a dropdown.
You have to implement a SearchIndex to make a model searchable. Please use a new
SearchSerializer when implementing an index, this makes things much easier to debug.
SearchIndex¶
- class lego.apps.search.index.SearchIndex¶
Bases:
objectBase class for search indexes. Implement this class to make a model searchable. Remember to use the register function to register the index. Searches query the live database rows using Postgres full text search, so there is no separate index to keep in sync.
- autocomplete(query: str) QuerySet¶
Autocomplete on the model using the database. Matches per-word prefixes across the autocomplete fields (so “aleks nyg” matches first name + last name) or, for typo tolerance, trigram word similarity. Results are ordered by similarity with autocomplete_ordering as tiebreaker. Only works for PostgreSQL.
- clean_query(query: str) str¶
Clean search query to prepare for pg search. Removes characters like &, | and other chars used in pg full text search.
- get_autocomplete_result_fields() Sequence[str]¶
Returns a list of fields attached to the autocomplete result.
- get_queryset() QuerySet¶
Get the queryset that should be searched. Override this method or set a queryset attribute on this class.