Skip to content

Commit

Permalink
Merge pull request #1195 from LibrePhotos/fix/improve-search-speed
Browse files Browse the repository at this point in the history
Search: Prefetch files and only load relevant data
  • Loading branch information
derneuere committed Apr 8, 2024
2 parents b788f9c + 091cd1d commit cb33d8a
Showing 1 changed file with 39 additions and 2 deletions.
41 changes: 39 additions & 2 deletions api/views/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from rest_framework.response import Response

from api.filters import SemanticSearchFilter
from api.models import Photo, User
from api.models import File, Photo, User
from api.serializers.photos import GroupedPhotosSerializer, PhotoSummarySerializer
from api.serializers.PhotosGroupedByDate import get_photos_ordered_by_date
from api.views.custom_api_view import ListViewSet
Expand Down Expand Up @@ -40,21 +40,58 @@ def list(self, request):
"id", "username", "first_name", "last_name"
),
),
Prefetch(
"main_file__embedded_media",
queryset=File.objects.only("hash"),
),
)
.order_by("-exif_timestamp")
.only(
"image_hash",
"aspect_ratio",
"video",
"main_file",
"search_location",
"dominant_color",
"public",
"rating",
"hidden",
"exif_timestamp",
"owner",
"video_length",
)
)
grouped_photos = get_photos_ordered_by_date(queryset)
serializer = GroupedPhotosSerializer(grouped_photos, many=True)
return Response({"results": serializer.data})
else:
queryset = self.filter_queryset(
Photo.visible.filter(Q(owner=self.request.user)).prefetch_related(
Photo.visible.filter(Q(owner=self.request.user))
.prefetch_related(
Prefetch(
"owner",
queryset=User.objects.only(
"id", "username", "first_name", "last_name"
),
),
Prefetch(
"main_file__embedded_media",
queryset=File.objects.only("hash"),
),
)
.only(
"image_hash",
"aspect_ratio",
"video",
"main_file",
"search_location",
"dominant_color",
"public",
"rating",
"hidden",
"exif_timestamp",
"owner",
"video_length",
)
)
serializer = PhotoSummarySerializer(queryset, many=True)
Expand Down

0 comments on commit cb33d8a

Please sign in to comment.