Skip to main content

Provides a nice abstraction layer on top of the django-watson search library.

Reason this release was yanked:

broken

Project description

Giant Search

This library provides a nice abstraction layer on top of the django-watson search library. It allows developers to easily index and search across Django CMS Page Title objects without any additional configuration, as well as a simple mixin class to make indexing other model types, such as CMS plugin classes simple.

Installation

Install giant-search via your chosen Python dependency manager, poetry, pip etc.

Configuration

  1. Add watson to INSTALLED_APPS in settings.py
  2. Add giant_search to INSTALLED_APPS in settings.py
  3. Add the search application URLs to your project's urls.py, for example: path("search/", include("giant_search.urls", namespace="search")),

Registering items as searchable

Django CMS Page Titles

Short version: you don't need to do anything.

The library will index all published Title objects. This allows end users to find pages via their title. This behaviour cannot currently be overridden, however in a future version, we might check if the Page has the NoIndex Page Extension and honour the setting within.

Other models

We provide a convenient mixin class, SearchableMixin that you can add to any model to allow it to be searched. Don't forget to add the import line from giant_search.mixins import SearchableMixin at the top of the models file.

As a developer, there are several configuration options that you can define to customise what gets indexed, and the data that is presented in the search result listing:

Third party models

While the SearchableMixin takes care of first party models, you usually can't implement this on third party models. However, you can still make them searchable.

In one of your main apps (usually core), add a call to register the model. Here is an example:

from django.apps import AppConfig

from giant_search.utils import register_for_search

class CoreAppConfig(AppConfig):
   name = "core"
   verbose_name = "Core"

   def ready(self):
      from third_party_library.models import ThirdPartyModel
      register_for_search(ThirdPartyModel)

Third party models will always have their string representation set as the search result title. The model must implement the get_absolute_url method, otherwise, the search result will not have a valid URL and the model will be indexed, but will not show up in search results.

Overriding the search QuerySet

By default, giant-search will get all instances of a particular model to index.

You can override this in your model class, perhaps to return only published items:

@classmethod
def get_search_queryset(cls) -> QuerySet:
        return cls.objects.published()

If you want to define which fields on your model should be searched, you can implement a get_search_fields method on your model like so:

from giant_search.mixins import SearchableMixin


class ExampleModel(SearchableMixin, models.Model):
    name = models.CharField(max_length=255)
    content = models.CharField(max_legth=255)

    @staticmethod
    def get_search_fields() -> tuple:
        """
        Override this method to provide a tuple containing the fields to search.
        If the method returns an empty tuple, all text fields will be indexed as per Watson's defaults.
        """

        return "name", "content"

Defining search result title, description and URL

When Watson performs a search, it returns a list of SearchEntry instances, which has some fields that can be used on the front end to display search results to end users. For example, title, description and url.

The title field is the title of the search result, the description is optional and provides a bit more context about the search result and the URL is required and is where the user should be taken to upon clicking the search result.

In order to specify where Watson should get the values from for these fields, you can define the following on your model (remember, it must inherit from the SearchableMixin)

Here is an example:

from giant_search.mixins import SearchableMixin


class ExampleModel(SearchableMixin, models.Model):
    name = models.CharField(max_length=255)
    summary = models.CharField(max_length=255)
    content = RichTextField()
    
    def __str__(self):
        return self.name
        
    def get_search_result_title(self) -> str:
        return str(self)

    def get_search_result_description(self) -> str:
        return self.summary

The important parts in this example are get_search_result_title and get_search_result_description

Note that in this example, we don't define get_search_result_url. If you don't define get_search_result_url then Giant Search will call the get_absolute_url method on the model, if it has that method. If the model does not implement, get_absolute_url and does not implement get_search_result_url then it won't have a URL and will not be shown in the search results.

If your model is a Django CMS Plugin instance, you probably want to implement get_absolute_url() and have it call self.page.get_public_url().

def get_absolute_url(self) -> str:
    try:
        return self.page.get_public_url()
    except AttributeError:
        return ""

Pagination

By default the search results will render 10 items per page. If you want to customise this simply add GIANT_SEARCH_PAGINATE_BY to your project's settings, along with the desired integer number of items to paginate by. This assumes your project has a registered simple_tag entitled show_pagination containing pagination logic.

Existing Data

If implementing this library upon existing data, changes to search results will only take effect after the model instance is saved again.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

giant_search-1.1.2.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

giant_search-1.1.2-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file giant_search-1.1.2.tar.gz.

File metadata

  • Download URL: giant_search-1.1.2.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.9.17 Darwin/24.3.0

File hashes

Hashes for giant_search-1.1.2.tar.gz
Algorithm Hash digest
SHA256 c4c5032f770b9767322a40bb62f863d9a8f14d9a648a5cace2c3f4db71caf5e6
MD5 c8d8b40011be1641e1e6149a8c551775
BLAKE2b-256 082ab3586afb6400171fbcccb01e562fb5fabc156eb9842b238a07339758f92d

See more details on using hashes here.

File details

Details for the file giant_search-1.1.2-py3-none-any.whl.

File metadata

  • Download URL: giant_search-1.1.2-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.9.17 Darwin/24.3.0

File hashes

Hashes for giant_search-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 ec443f8cdb8421252518066b594872229173600aa45690f8fd87ede473b33eee
MD5 c6e7c6763e6f0bd228457f9bce37571a
BLAKE2b-256 ec88022036a1fc624bf6cd9511d9992d9bce499202ea47de8f132be3f89a3d00

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page