Skip to content

Commit ca2309e

Browse files
authored
Merge pull request #626 from kdroidFilter/feat/search-unified-bar
feat(search): one home search bar (go-to, text search, authors, books)
2 parents 44f469d + 438c287 commit ca2309e

13 files changed

Lines changed: 594 additions & 626 deletions

File tree

‎SeforimApp/src/commonMain/composeResources/files/ABOUT.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
אני מודה ל־JetBrains על יצירת Kotlin, ל־Google על מסגרת העבודה Compose, ול־JetBrains על התאמתה לסביבת הדסקטופ. אני מודה גם לקרן Apache על Lucene, שאפשרה לי לבנות מנוע חיפוש עוצמתי במיוחד, וכן לכל מפתחי הספריות שבהן עושה התוכנה שימוש. בלעדיהם, דבר מכל זה לא היה מתאפשר.
55

6+
אייקון היד הכותבת: Emoji One, ברישיון CC BY 4.0.
7+
68
בסיס הנתונים של זית נשען על העבודה המרשימה של פרויקטי Sefaria ו־אוצריא, ואני מודה להם בכנות על האיכות והפתיחות של מאגרי הנתונים שלהם. מתוך מקורות אלו, בניתי מחדש ועיצבתי מחדש את בסיס הנתונים באופן מלא, כך שיהיה יעיל הרבה יותר וקריא באמת עבור מחשב. הוא תוכנן לא כאוסף טקסטים פשוט, אלא כמבנה המותאם לחיפוש, לניווט מהיר וללימוד מעמיק.
79

810
בבסיס זה השתמשתי בטכנולוגיות החזקות ביותר הזמינות כיום, כדי לבנות מה שלדעתי הוא מנוע חיפוש יעיל במיוחד ללימוד התורה.

‎SeforimApp/src/commonMain/composeResources/values/strings.xml‎

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -287,10 +287,9 @@
287287
<string name="please_select_a_book">אנא בחר ספר</string>
288288

289289
<!-- Home Page View -->
290-
<string name="search_mode_reference">ספר</string>
291-
<string name="search_mode_text">טקסט</string>
292-
<string name="search_mode_reference_explicit">חיפוש לפי מקור</string>
293-
<string name="search_mode_text_explicit">חיפוש בטקסט</string>
290+
<string name="search_text_row">חיפוש במאגר: %1$s</string>
291+
<string name="author_books_count">%1$d ספרים</string>
292+
<string name="search_text_row_in_book">חיפוש בתוך %2$s: %1$s</string>
294293
<string name="search_by_category_or_book">חפש לפי קטגוריה או ספר</string>
295294
<string name="remove_selected_book">הסר ספר שנבחר</string>
296295

@@ -302,11 +301,6 @@
302301
<string name="reference_hint_5">תהילים פרק כג פסוק ד</string>
303302

304303
<!-- Book names only (for reference-mode first field) -->
305-
<string name="reference_book_hint_1">משנת יסודות התורה</string>
306-
<string name="reference_book_hint_2">שוע יוד</string>
307-
<string name="reference_book_hint_3">תהילים</string>
308-
<string name="reference_book_hint_4">ברכות</string>
309-
<string name="reference_book_hint_5">שוע אורח חיים</string>
310304

311305
<!-- TOC examples (for second field when no book yet) -->
312306
<string name="reference_toc_hint_1">פרק א</string>

‎SeforimApp/src/jvmMain/kotlin/io/github/kdroidfilter/seforimapp/core/presentation/tabs/TabsContent.kt‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ fun TabsContent() {
135135
HomeSearchCallbacks(
136136
onReferenceQueryChanged = searchHomeViewModel::onReferenceQueryChanged,
137137
onTocQueryChanged = searchHomeViewModel::onTocQueryChanged,
138-
onFilterChange = searchHomeViewModel::onFilterChange,
139138
onGlobalExtendedChange = searchHomeViewModel::onGlobalExtendedChange,
140139
onSubmitTextSearch = { query ->
141140
val tabId = latestCurrentTabId ?: return@HomeSearchCallbacks
@@ -148,6 +147,8 @@ fun TabsContent() {
148147
onPickCategory = searchHomeViewModel::onPickCategory,
149148
onPickBook = searchHomeViewModel::onPickBook,
150149
onPickToc = searchHomeViewModel::onPickToc,
150+
onPickAuthor = searchHomeViewModel::onPickAuthor,
151+
onClearAuthor = searchHomeViewModel::onClearAuthor,
151152
onOpenJump = { jump ->
152153
val tabId = latestCurrentTabId ?: return@HomeSearchCallbacks
153154
scope.launch { searchHomeViewModel.openJump(jump, tabId) }

‎SeforimApp/src/jvmMain/kotlin/io/github/kdroidfilter/seforimapp/features/bookcontent/ui/panels/bookcontent/BookContentPanel.kt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ fun BookContentPanel(
5252
HomeSearchCallbacks(
5353
onReferenceQueryChanged = {},
5454
onTocQueryChanged = {},
55-
onFilterChange = {},
5655
onGlobalExtendedChange = {},
5756
onSubmitTextSearch = {},
5857
onOpenReference = {},

‎SeforimApp/src/jvmMain/kotlin/io/github/kdroidfilter/seforimapp/features/bookcontent/ui/panels/bookcontent/views/HomeView.kt‎

Lines changed: 324 additions & 558 deletions
Large diffs are not rendered by default.

‎SeforimApp/src/jvmMain/kotlin/io/github/kdroidfilter/seforimapp/features/search/SearchFilter.kt‎

Lines changed: 0 additions & 9 deletions
This file was deleted.

‎SeforimApp/src/jvmMain/kotlin/io/github/kdroidfilter/seforimapp/features/search/SearchHomeViewModel.kt‎

Lines changed: 74 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,12 @@ import io.github.kdroidfilter.seforim.tabs.TabsDestination
88
import io.github.kdroidfilter.seforimapp.core.coroutines.runSuspendCatching
99
import io.github.kdroidfilter.seforimapp.core.deeplink.parseZayitDeepLink
1010
import io.github.kdroidfilter.seforimapp.core.settings.AppSettings
11+
import io.github.kdroidfilter.seforimapp.features.search.domain.reference.ReferenceParser
1112
import io.github.kdroidfilter.seforimapp.features.search.domain.reference.ReferenceResolver
1213
import io.github.kdroidfilter.seforimapp.features.search.domain.reference.RepositoryReferenceSource
1314
import io.github.kdroidfilter.seforimapp.features.search.domain.reference.ResolvedReference
1415
import io.github.kdroidfilter.seforimapp.framework.search.LuceneLookupSearchService
16+
import io.github.kdroidfilter.seforimapp.framework.search.LuceneLookupSearchService.AuthorHit
1517
import io.github.kdroidfilter.seforimapp.framework.search.MIN_BOOK_QUERY_LENGTH
1618
import io.github.kdroidfilter.seforimapp.framework.session.SearchPersistedState
1719
import io.github.kdroidfilter.seforimapp.framework.session.TabPersistedStateStore
@@ -95,14 +97,17 @@ data class TocSuggestionDto(
9597

9698
@Immutable
9799
data class SearchHomeUiState(
98-
val selectedFilter: SearchFilter = SearchFilter.TEXT,
99100
val globalExtended: Boolean = false,
100101
val suggestionsVisible: Boolean = false,
101102
val isReferenceLoading: Boolean = false,
102103
val categorySuggestions: List<CategorySuggestionDto> = emptyList(),
103104
val bookSuggestions: List<BookSuggestionDto> = emptyList(),
104105
/** Places the typed text points to (`חולין יב:`), listed above the book suggestions. */
105106
val jumpSuggestions: List<ResolvedReference> = emptyList(),
107+
/** Authors the typed text may name, listed before the books. */
108+
val authorSuggestions: List<AuthorHit> = emptyList(),
109+
/** The author picked: the books listed are then theirs. */
110+
val selectedScopeAuthor: AuthorHit? = null,
106111
val tocSuggestionsVisible: Boolean = false,
107112
val isTocLoading: Boolean = false,
108113
val tocSuggestions: List<TocSuggestionDto> = emptyList(),
@@ -131,6 +136,9 @@ class SearchHomeViewModel(
131136

132137
private val referenceResolver = ReferenceResolver(RepositoryReferenceSource(repository))
133138

139+
// The books of the picked author, filtered by what is typed next
140+
private var authorBooks: List<BookSuggestionDto> = emptyList()
141+
134142
private val referenceQuery = MutableStateFlow("")
135143
private val tocQuery = MutableStateFlow("")
136144

@@ -212,13 +220,21 @@ class SearchHomeViewModel(
212220
.distinctUntilChanged()
213221
.collectLatest { qRaw ->
214222
val q = qRaw.trim()
215-
if (q.isBlank()) {
223+
if (_uiState.value.selectedScopeAuthor != null) {
224+
_uiState.value =
225+
_uiState.value.copy(
226+
isReferenceLoading = false,
227+
bookSuggestions = authorBooks.filter { it.titleContains(q) },
228+
suggestionsVisible = true,
229+
)
230+
} else if (q.isBlank()) {
216231
_uiState.value =
217232
_uiState.value.copy(
218233
isReferenceLoading = false,
219234
categorySuggestions = emptyList(),
220235
bookSuggestions = emptyList(),
221236
jumpSuggestions = emptyList(),
237+
authorSuggestions = emptyList(),
222238
suggestionsVisible = false,
223239
)
224240
} else {
@@ -292,22 +308,26 @@ class SearchHomeViewModel(
292308
}
293309
}
294310

311+
val authorsDeferred =
312+
async(Dispatchers.Default) { lookup.suggestAuthors(q, limit = MAX_AUTHOR_SUGGESTIONS) }
313+
295314
val jumpsDeferred =
296315
async(Dispatchers.IO) {
297316
runSuspendCatching { referenceResolver.resolve(q) }.getOrDefault(emptyList())
298317
}
299318

300-
Triple(catsDeferred.await(), booksDeferred.await(), jumpsDeferred.await())
319+
Suggestions(catsDeferred.await(), booksDeferred.await(), jumpsDeferred.await(), authorsDeferred.await())
301320
}
302321
}
303322

304-
val (catSuggestions, bookSuggestions, jumpSuggestions) = result
323+
val (catSuggestions, bookSuggestions, jumpSuggestions, authorSuggestions) = result
305324
_uiState.value =
306325
_uiState.value.copy(
307326
isReferenceLoading = false,
308327
categorySuggestions = catSuggestions,
309328
bookSuggestions = bookSuggestions,
310329
jumpSuggestions = jumpSuggestions,
330+
authorSuggestions = authorSuggestions,
311331
suggestionsVisible = true,
312332
)
313333
}
@@ -406,10 +426,44 @@ class SearchHomeViewModel(
406426
)
407427
}
408428

429+
/** Lists the books of [author], to pick one of them next. */
430+
fun onPickAuthor(author: AuthorHit) {
431+
_uiState.value =
432+
_uiState.value.copy(
433+
selectedScopeAuthor = author,
434+
authorSuggestions = emptyList(),
435+
jumpSuggestions = emptyList(),
436+
bookSuggestions = emptyList(),
437+
isReferenceLoading = true,
438+
suggestionsVisible = true,
439+
)
440+
viewModelScope.launch {
441+
authorBooks =
442+
withContext(Dispatchers.IO) {
443+
runSuspendCatching { repository.searchBooksByAuthor(author.name) }
444+
.getOrDefault(emptyList())
445+
// The query matches the name anywhere: keep this author's books only
446+
.filter { book -> book.authors.any { it.id == author.id } }
447+
.sortedWith(compareByDescending<Book> { it.isBaseBook }.thenBy { it.order })
448+
.map { book -> BookSuggestionDto(book, buildCategoryPathTitlesCached(book.categoryId) + book.title) }
449+
}
450+
if (_uiState.value.selectedScopeAuthor?.id == author.id) {
451+
_uiState.value = _uiState.value.copy(bookSuggestions = authorBooks, isReferenceLoading = false)
452+
}
453+
}
454+
}
455+
456+
fun onClearAuthor() {
457+
authorBooks = emptyList()
458+
_uiState.value = _uiState.value.copy(selectedScopeAuthor = null, bookSuggestions = emptyList(), suggestionsVisible = false)
459+
}
460+
409461
fun onPickBook(book: Book) {
462+
authorBooks = emptyList()
410463
// Update synchronously first
411464
_uiState.value =
412465
_uiState.value.copy(
466+
selectedScopeAuthor = null,
413467
selectedScopeCategory = null,
414468
selectedScopeBook = book,
415469
selectedScopeToc = null,
@@ -502,10 +556,6 @@ class SearchHomeViewModel(
502556
)
503557
}
504558

505-
fun onFilterChange(filter: SearchFilter) {
506-
_uiState.value = _uiState.value.copy(selectedFilter = filter)
507-
}
508-
509559
fun onGlobalExtendedChange(extended: Boolean) {
510560
_uiState.value = _uiState.value.copy(globalExtended = extended)
511561
}
@@ -545,8 +595,10 @@ class SearchHomeViewModel(
545595
val selected = _uiState.value
546596
val scope =
547597
when {
548-
selected.selectedScopeToc != null ->
598+
// An alternative-TOC entry has no main-TOC scope: search its whole book
599+
selected.selectedScopeToc != null && !selected.selectedScopeToc.isAltTocEntry() ->
549600
SearchScope.Toc(bookId = selected.selectedScopeToc.bookId, tocId = selected.selectedScopeToc.id)
601+
selected.selectedScopeToc != null -> SearchScope.Book(selected.selectedScopeToc.bookId)
550602
selected.selectedScopeBook != null -> SearchScope.Book(selected.selectedScopeBook.id)
551603
selected.selectedScopeCategory != null -> SearchScope.Category(selected.selectedScopeCategory.id)
552604
else -> SearchScope.Global
@@ -726,3 +778,16 @@ private val ALT_TOC_LABELS =
726778
"Daf" to "דפים",
727779
"Contents" to "תוכן",
728780
)
781+
782+
private const val MAX_AUTHOR_SUGGESTIONS = 4
783+
784+
// Typed text against a title, both without nikud or quotes (שו"ת / שו״ת)
785+
private fun BookSuggestionDto.titleContains(typed: String): Boolean =
786+
typed.isBlank() || ReferenceParser.normalizeName(book.title).contains(ReferenceParser.normalizeName(typed))
787+
788+
private data class Suggestions(
789+
val categories: List<CategorySuggestionDto>,
790+
val books: List<BookSuggestionDto>,
791+
val jumps: List<ResolvedReference>,
792+
val authors: List<AuthorHit>,
793+
)
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package io.github.kdroidfilter.seforimapp.features.search.domain
2+
3+
/**
4+
* How an author's name is shown: respectfully, never bare (`יוסף קארו` → `הרב יוסף קארו`).
5+
*
6+
* The database keeps the upstream (Sefaria, Otzaria) names untouched: the honorific is display only.
7+
* Acronyms (`רמב"ם`, `מהר"ם פדובה`: a quote in the first word) and names already carrying a title
8+
* (`רבי…`, `חכם…`) stay as they are; every other name gets `הרב`. Names stored the catalog way
9+
* (`ליבשיץ, יחזקאל`) are put back in reading order; nikud, bidi marks and extra spaces go.
10+
*/
11+
object AuthorNames {
12+
private val NIKUD = Regex("[֑-ׇ]")
13+
private val BIDI_MARKS = Regex("[\u200E\u200F\u202A-\u202E]")
14+
private val QUOTES = Regex("[\"״]")
15+
private val TITLES =
16+
listOf(
17+
"רבי",
18+
"רב",
19+
"רבן",
20+
"רבנו",
21+
"רבינו",
22+
"הרב",
23+
"ר'",
24+
"ר׳",
25+
"חכם",
26+
"החכם",
27+
"הגאון",
28+
"מרן",
29+
"מורנו",
30+
"האדמו",
31+
)
32+
private const val HONORIFIC = "הרב"
33+
34+
fun display(raw: String): String {
35+
val name =
36+
readingOrder(
37+
raw
38+
.replace(NIKUD, "")
39+
.replace(BIDI_MARKS, "")
40+
.replace(Regex("\\s+"), " ")
41+
.trim(),
42+
)
43+
if (name.isEmpty() || isAcronym(name) || hasTitle(name)) return name
44+
return "$HONORIFIC $name"
45+
}
46+
47+
// `רמב"ם`, `מהר"ם פדובה`; but not `יוסף בן משה באב"ד`, whose quote is in a later word
48+
private fun isAcronym(name: String): Boolean = QUOTES.containsMatchIn(name.substringBefore(' '))
49+
50+
private fun hasTitle(name: String): Boolean =
51+
TITLES.any { title ->
52+
name == title ||
53+
name.startsWith("$title ") ||
54+
name.startsWith(title + "\"")
55+
}
56+
57+
// `ליבשיץ, יחזקאל בן הילל` → `יחזקאל בן הילל ליבשיץ`: a short family name before a single comma
58+
private fun readingOrder(name: String): String {
59+
val parts = name.split(", ")
60+
if (parts.size != 2 || parts[0].split(' ').size > 2 || parts[1].isBlank()) return name
61+
return "${parts[1]} ${parts[0]}"
62+
}
63+
}

‎SeforimApp/src/jvmMain/kotlin/io/github/kdroidfilter/seforimapp/framework/search/LuceneLookupSearchService.kt‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ class LuceneLookupSearchService(
5353
)
5454
}
5555

56+
private val authorSort = Sort(SortField("book_count", SortField.Type.LONG, true))
57+
5658
private val bookSort =
5759
Sort(
5860
SortField("is_base_book", SortField.Type.INT, true),
@@ -69,6 +71,13 @@ class LuceneLookupSearchService(
6971
val score: Float,
7072
)
7173

74+
/** An author of the lookup index, with the number of its books. */
75+
data class AuthorHit(
76+
val id: Long,
77+
val name: String,
78+
val bookCount: Int,
79+
)
80+
7281
data class BookHit(
7382
val id: Long,
7483
val categoryId: Long,
@@ -141,6 +150,48 @@ class LuceneLookupSearchService(
141150
}
142151
}
143152

153+
/**
154+
* Authors whose name or alias (רעק״א, ראב״ע...) starts with the typed words, those with the
155+
* most books first. Empty below [MIN_BOOK_QUERY_LENGTH] characters, or with an index built
156+
* before authors were indexed.
157+
*/
158+
fun suggestAuthors(
159+
raw: String,
160+
limit: Int,
161+
): List<AuthorHit> {
162+
val q = normalizeHebrew(raw)
163+
if (q.length < MIN_BOOK_QUERY_LENGTH) return emptyList()
164+
// Aliases are indexed whole (רעקא) and split at their gershayim (רעק + א): the split reading
165+
// only when the whole one finds nobody, as its one-letter pieces match many names
166+
val whole = authorsMatching(q, limit)
167+
if (whole.isNotEmpty()) return whole
168+
val split = normalizeHebrew(raw.replace(Regex("[\"'״׳]"), " "))
169+
return if (split == q) whole else authorsMatching(split, limit)
170+
}
171+
172+
private fun authorsMatching(
173+
normalized: String,
174+
limit: Int,
175+
): List<AuthorHit> {
176+
val tokens = normalized.split("\\s+".toRegex()).filter { it.isNotEmpty() }
177+
if (tokens.isEmpty()) return emptyList()
178+
return withSearcher { searcher ->
179+
val query = BooleanQuery.Builder()
180+
query.add(TermQuery(Term("type", "author")), BooleanClause.Occur.FILTER)
181+
tokens.forEach { t -> query.add(PrefixQuery(Term("q", t)), BooleanClause.Occur.MUST) }
182+
val top = searcher.search(query.build(), limit, authorSort)
183+
val stored = searcher.storedFields()
184+
top.scoreDocs.map { sd ->
185+
val doc = stored.document(sd.doc)
186+
AuthorHit(
187+
id = doc.getField("author_id").numericValue().toLong(),
188+
name = doc.getField("author_name").stringValue(),
189+
bookCount = doc.getField("book_count").numericValue().toInt(),
190+
)
191+
}
192+
}
193+
}
194+
144195
fun searchTocPrefix(
145196
raw: String,
146197
limit: Int = 20,

0 commit comments

Comments
 (0)