Skip to content

Instantly share code, notes, and snippets.

@mathjazz
Created September 5, 2017 18:40
Show Gist options
  • Select an option

  • Save mathjazz/e6d60666a40ad4a07bc857c45402a9af to your computer and use it in GitHub Desktop.

Select an option

Save mathjazz/e6d60666a40ad4a07bc857c45402a9af to your computer and use it in GitHub Desktop.
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