Created
September 5, 2017 18:40
-
-
Save mathjazz/e6d60666a40ad4a07bc857c45402a9af to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git a/pontoon/base/models.py b/pontoon/base/models.py | |
| index f28c0ec..03437a9 100644 | |
| --- a/pontoon/base/models.py | |
| +++ b/pontoon/base/models.py | |
| @@ -2026,14 +2026,30 @@ class Entity(DirtyFieldsMixin, models.Model): | |
| if search: | |
| # https://docs.djangoproject.com/en/dev/topics/db/queries/#spanning-multi-valued-relationships | |
| search_query = (search, locale.db_collation) | |
| - entities = ( | |
| - Entity.objects.filter( | |
| - Q(translation__string__icontains_collate=search_query, translation__locale=locale) | Q(string__icontains=search) | Q(string_plural__icontains=search) | Q(comment__icontains=search) | Q(key__icontains=search), | |
| - pk__in=entities | |
| + | |
| + translation_matches = ( | |
| + entities.filter( | |
| + translation__string__icontains_collate=search_query, | |
| + translation__locale=locale, | |
| ) | |
| .distinct() | |
| ) | |
| + entity_matches = ( | |
| + entities.filter( | |
| + Q(string__icontains=search) | | |
| + Q(string_plural__icontains=search) | | |
| + Q(comment__icontains=search) | | |
| + Q(key__icontains=search) | |
| + ) | |
| + .distinct() | |
| + ) | |
| + | |
| + # QuerySet union is faster than DB join | |
| + entities = Entity.objects.filter( | |
| + pk__in=set(e.pk for e in list(translation_matches) + list(entity_matches)) | |
| + ) | |
| + | |
| entities = entities.prefetch_resources_translations(locale) | |
| if exclude: |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment