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:
object
Base class for search indexes. Implement this class to index a model. Remeber to use the register function to register the index.
- autocomplete(query)¶
Uses the model to search with autocomplete. This will use the database for search Only works for PostgreSQL
- clean_query(query)¶
Clean search query to prepare for pg search. Removes characters like &, | and other chars used in pg full text search.
- get_autocomplete(instance)¶
Implement this method to support autocomplete on the model. This function should return None, a string or a list of strings.
- get_autocomplete_result_fields()¶
Returns a list of fields attached to the autocomplete result.
- get_backend()¶
Return the backend used to handle changes by this index. You should know what you do if you override this function. The search interface only supports one backend.
- get_filter_fields()¶
Returns an array of allowed fields to filter on. Returns a empty list by default. Override this function or set the filter_fields variable on the class to change this.
- get_index_filter_fields()¶
Returns True if we want to index filter fields. Defaults to False.
- get_model()¶
Get the model this index is bound to.
- get_queryset()¶
Get the queryset that should be indexed. Override this method or set a queryset attribute on this class.
- get_result_fields()¶
Returns a list of fields attached to the search result.
- get_serializer(*args, **kwargs)¶
Return the serializer with args and kwargs.
- get_serializer_class()¶
Override this method or set the serializer_class attribute on the class to define the serializer.
- prepare(instance)¶
Prepare instance for indexing, this function returns a dict in a specific format. This is passed to the search-backend. The backend has to parse this and index it.
- remove_instance(pk)¶
Remove a single instance from the index. We use pks here because the instance may not exists in the database.
- search(query)¶
Uses the model to do a full search. This will use the database for search Only works for PostgreSQL
- should_update(instance)¶
Determine if an instance should be updated in the index.
- update()¶
Updates the entire index. We do this in batch to optimize performance. NB: Requires automatic IDs.
- update_instance(instance)¶
Update a given instance in the index.