news: multi-term search ranks by term coverage, not recency
- Commit
273e09d4bc006479ae53a09333cdaf0c218b7044- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 11:43 UTC (4 days ago)
- Parents
66c815f
matches() treated 'contains the phrase OR any one term' as
full title relevance: for the query 'climate summit' a
newer one-term title ('Climate crisis deepens') and an older
exact-phrase title ('Climate summit opens') landed in the
same tier, sorted newest-first — and with limit=1 the exact
match was dropped entirely.
Within the unchanged tier structure, ranking now sorts by
distinct matched-term count over title+source+keywords
(desc), then exact-phrase-in-title, then newest — before
the limit is taken. Single-term behaviour is unchanged
(count uniformly 1). Two regression tests failed pre-fix on
exactly this ranking. Feed loading, story view and saved
state were audited and cleared. Verified:
:news:testDevUnitTest 77 tests and :news:lint BUILD
SUCCESSFUL.Diff
news/src/main/java/com/vayunmathur/news/domain/NewsSearch.kt
diff --git a/news/src/main/java/com/vayunmathur/news/domain/NewsSearch.kt b/news/src/main/java/com/vayunmathur/news/domain/NewsSearch.ktindex 7f5cd0b03c..ef40bb7bb1 100644--- a/news/src/main/java/com/vayunmathur/news/domain/NewsSearch.kt+++ b/news/src/main/java/com/vayunmathur/news/domain/NewsSearch.kt@@ -11,11 +11,23 @@ package com.vayunmathur.news.domain * no FTS index is warranted at this scale. If the cache cap ever grows * by an order of magnitude, move bodies to an FTS table instead. *- * Ranking tiers (newest first inside a tier):+ * Ranking tiers: * 0. title matches, 1. source-name / keyword matches, 2. body-only matches.+ * Inside a tier, a candidate matching more distinct query terms (across+ * title, source name and keywords) outranks one matching fewer — an exact+ * multi-term title beats a newer single-term title — an exact-phrase title+ * breaks a term-count tie, and recency breaks remaining ties (newest first). */ object NewsSearch { + /** Sort keys for one matched candidate (tier, term coverage, phrase, article). */+ private data class Ranked(+ val tier: Int,+ val matchedTerms: Int,+ val phraseInTitle: Boolean,+ val article: FeedArticle,+ )+ /** One searchable stub: a [FeedArticle] plus whether its cached body matched. */ data class Candidate( val article: FeedArticle,@@ -37,10 +49,20 @@ object NewsSearch { candidate.bodyHit -> 2 else -> return@mapNotNull null }- tier to article+ // Relevance within the tier: how many distinct query terms the+ // searchable text (title, source name, keywords) actually states.+ // Matching only one of several stated terms must not let recency+ // bury a candidate stating all of them (the limit below would+ // then drop the best match entirely).+ val searchable = listOf(article.title, candidate.sourceName) + article.keywords+ val matchedTerms = terms.count { term -> searchable.any { it.lowercase().contains(term) } }+ val phraseInTitle = article.title.lowercase().contains(phrase)+ Ranked(tier, matchedTerms, phraseInTitle, article) }.sortedWith(- compareBy<Pair<Int, FeedArticle>> { it.first }- .thenByDescending { it.second.publishedAtMillis },- ).take(limit).map { it.second }+ compareBy<Ranked> { it.tier }+ .thenByDescending { it.matchedTerms }+ .thenByDescending { it.phraseInTitle }+ .thenByDescending { it.article.publishedAtMillis },+ ).take(limit).map { it.article } } }
news/src/test/java/com/vayunmathur/news/domain/NewsSearchTest.kt
diff --git a/news/src/test/java/com/vayunmathur/news/domain/NewsSearchTest.kt b/news/src/test/java/com/vayunmathur/news/domain/NewsSearchTest.ktindex 1897c4f1e3..2288fa625f 100644--- a/news/src/test/java/com/vayunmathur/news/domain/NewsSearchTest.kt+++ b/news/src/test/java/com/vayunmathur/news/domain/NewsSearchTest.kt@@ -19,6 +19,31 @@ class NewsSearchTest { assertEquals(listOf("https://e/older-title", "https://e/newer-body"), ranked.map { it.url }) } + @Test fun exactPhraseTitleBeatsNewerSingleTermTitle() {+ // Query states two terms; the older article matching both (the exact+ // phrase) is more relevant than a newer title matching only one term.+ val ranked = NewsSearch.rank(+ "climate summit",+ listOf(+ candidate("https://e/newer-single", "Climate crisis deepens", 300),+ candidate("https://e/older-exact", "Climate summit opens", 100),+ ),+ )+ assertEquals(listOf("https://e/older-exact", "https://e/newer-single"), ranked.map { it.url })+ }++ @Test fun limitKeepsExactMatchOverNewerSingleTerm() {+ val ranked = NewsSearch.rank(+ "climate summit",+ listOf(+ candidate("https://e/newer-single", "Climate crisis deepens", 300),+ candidate("https://e/older-exact", "Climate summit opens", 100),+ ),+ limit = 1,+ )+ assertEquals(listOf("https://e/older-exact"), ranked.map { it.url })+ }+ @Test fun withinTierNewestFirstAndBlankQueryEmpty() { val ranked = NewsSearch.rank( "election",