LEGO
1.0.0
  • Settings
  • Permissions
  • Mail
  • Authentication
  • Feeds
  • Events
  • External Sync
  • Files
  • Health Checks
  • Notifications
  • Site Search
    • SearchIndex
      • SearchIndex
        • SearchIndex.autocomplete()
        • SearchIndex.clean_query()
        • SearchIndex.get_autocomplete_result_fields()
        • SearchIndex.get_model()
        • SearchIndex.get_queryset()
        • SearchIndex.get_result_fields()
        • SearchIndex.get_serializer()
        • SearchIndex.get_serializer_class()
        • SearchIndex.search()
  • Logging
LEGO
  • Site Search
  • View page source

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 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_model() → type[django.db.models.base.Model]¶

Get the model this index is bound to.

get_queryset() → QuerySet¶

Get the queryset that should be searched. Override this method or set a queryset attribute on this class.

get_result_fields() → Sequence[str]¶

Returns a list of fields attached to the search result.

get_serializer(*args: Any, **kwargs: Any) → Serializer¶

Return the serializer with args and kwargs.

get_serializer_class() → type[rest_framework.serializers.Serializer]¶

Override this method or set the serializer_class attribute on the class to define the serializer.

search(query: str) → QuerySet¶

Full text search on the model using the database. The Norwegian config stems both the indexed text and the query, and results are ordered by relevance rank with search_ordering as tiebreaker. Only works for PostgreSQL.

Previous Next

© Copyright 2014 - 2020, Abakus Webkom.

Built with Sphinx using a theme provided by Read the Docs.