Skip to content

Refactor Queries For Search #958

Description

@david-venhoff

Motivation

Right now, searching for a specific page/event/poi requires two sql queries, one for finding the matching translations and one to convert the object translations into the objects themselves. This could be done in one sql query.

Additional Context

#929 (comment):

In view of our performance problems, I even have a completely new suggestion: instead of implementing the search as a model method, we could define the search function as a QuerySet method which can be chained to other database filters and thus reducing the amount of queries used:

class EventQuerySet(models.QuerySet):
    def search(self, language_slug, query):
        return self.filter(
            translations__language__slug=language_slug,
            translations__title__icontains=query,
        )

class Event(models.Model):
    ...
    objects = models.Manager.from_queryset(EventQuerySet)()
    ...

Then, it could be used e.g. like this:

events = region.events.filter(archived=self.archived)
...
query = event_filter_form.cleaned_data["query"]
if query:
    events = events.search(language_slug, query)

and in the end, only one sql would be performed, since querysets are lazy...

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature:SearchIssues related to the Search feature in the Integreat CMSreadyThis issue is ready to be worked ontopic:PerformanceIssues related to performance of the CMStopic:RefactoringIssues related to refactoring

    Type

    Projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions