Sample up to 3 top picks per friend for friend restaurant queries

Previously ordered by friend_beli_user_id first, returning 15 results
from whoever sorts first. Now fetches top-ranked rows globally and
caps at 3 per friend, giving a diverse cross-friend sample.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-09-17 22:49:34 -04:00
parent 98b4b46b11
commit 70475865cc
+22 -6
View File
@@ -1369,13 +1369,29 @@ async def beli_search_restaurants(
if name:
qs = qs.filter(name__icontains=name)
restaurants = (
await qs.order_by("friend_beli_user_id", "rank_index", "name")
.limit(15)
.all()
)
if friends_only:
# Fetch broadly ordered by rank, then sample up to 3 top picks per friend
rows = await qs.order_by("rank_index", "name").limit(500).all()
if cuisine:
cuisine_lower = cuisine.lower()
rows = [
r
for r in rows
if any(cuisine_lower in c.lower() for c in (r.cuisine or []))
]
seen: dict[str, int] = {}
restaurants = []
for r in rows:
fid = r.friend_beli_user_id
if seen.get(fid, 0) < 3:
restaurants.append(r)
seen[fid] = seen.get(fid, 0) + 1
if len(restaurants) >= 15:
break
else:
restaurants = await qs.order_by("rank_index", "name").limit(15).all()
if cuisine:
if cuisine and not friends_only:
cuisine_lower = cuisine.lower()
restaurants = [
r