Skip to content

Commit 47c20c2

Browse files
mircealunguclaude
andcommitted
Fix matched search subscriptions using source_id and title matching
- Use source_id instead of article.id for tracking matches (handles article version swapping for different CEFR levels) - Also check organic results by matching search terms against article titles, so articles like "Ukraine war update" show the "Ukraine" search tag even if found organically 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent fba4692 commit 47c20c2

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

zeeguu/core/content_recommender/elastic_recommender.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def article_recommendations_for_user(
179179

180180
# Get articles based on Search preferences and track which search matched
181181
articles_from_searches = []
182-
search_matches = {} # Maps article.id -> list of matched search terms
182+
search_matches_by_source = {} # Maps source_id -> list of matched search terms
183183
for search in wanted_user_searches.split():
184184
search_results = article_and_video_search_for_user(
185185
user,
@@ -191,9 +191,10 @@ def article_recommendations_for_user(
191191
use_readability_priority=True,
192192
)
193193
for article in search_results:
194-
if article.id not in search_matches:
195-
search_matches[article.id] = []
196-
search_matches[article.id].append(search)
194+
if article.source_id not in search_matches_by_source:
195+
search_matches_by_source[article.source_id] = []
196+
if search not in search_matches_by_source[article.source_id]:
197+
search_matches_by_source[article.source_id].append(search)
197198
articles_from_searches += search_results
198199

199200
# Combine organic recommendations with search results, deduplicating by source_id
@@ -217,10 +218,22 @@ def article_recommendations_for_user(
217218
content.append(c)
218219
added_from_search += 1
219220

220-
# Attach matched searches to content objects
221+
# Attach matched searches to content objects (by source_id so it works even with version swapping)
222+
# Also check organic results by matching search terms against title/content
223+
search_terms_list = wanted_user_searches.split() if wanted_user_searches else []
221224
for c in content:
222-
if c.id in search_matches:
223-
c._matched_searches = search_matches[c.id]
225+
matched = []
226+
# First check if we found it via explicit search query
227+
if c.source_id in search_matches_by_source:
228+
matched = search_matches_by_source[c.source_id][:]
229+
# Also check title for organic results that may match search terms
230+
if hasattr(c, 'title') and c.title:
231+
title_lower = c.title.lower()
232+
for term in search_terms_list:
233+
if term.lower() in title_lower and term not in matched:
234+
matched.append(term)
235+
if matched:
236+
c._matched_searches = matched
224237

225238
# Filter out articles uploaded by the user themselves
226239
# (teachers shouldn't see their own uploaded texts in their recommendations)

0 commit comments

Comments
 (0)