Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 29 additions & 10 deletions lib/search/utils/scope_tree.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ScopeTree {
final List<ScopeNode> rootNodes;
final Map<String, ScopeNode> nodesByFacet;

const ScopeTree._(this.rootNodes, this.nodesByFacet);
ScopeTree._(this.rootNodes, this.nodesByFacet);

// מפתח חלש מונע מהמטמון להאריך את חיי הספרייה והעץ לאחר רענון.
static final Expando<ScopeTree> _cache = Expando<ScopeTree>('scopeTree');
Expand Down Expand Up @@ -372,15 +372,40 @@ class ScopeTree {
@visibleForTesting
static int debugSearchScans = 0;

/// הצמתים בסדר העץ: המיון אינו יציב, ולכן סדר הסריקה קובע את סדר השווים.
late final List<ScopeNode> _nodesInTreeOrder = () {
final nodes = <ScopeNode>[];
void visit(ScopeNode node) {
nodes.add(node);
for (final child in node.children) {
visit(child);
}
}

for (final node in rootNodes) {
visit(node);
}
return nodes;
}();

/// ההתאמות של החיפוש האחרון: הקשה שמאריכה את השאילתה בודקת רק אותן — נכון
/// כל עוד [findNormalizedTextMatches] מתאים לתחילת כל שאילתה שהוא מתאים לה.
({String query, List<ScopeNode> matches})? _lastSearch;

List<ScopeSearchResultItem> search(String normalizedQuery) {
if (normalizedQuery.isEmpty) {
return const [];
}

debugSearchScans++;
final last = _lastSearch;
final candidates = last != null && normalizedQuery.startsWith(last.query)
? last.matches
: _nodesInTreeOrder;
final matchedNodes = <ScopeNode>[];
final results = <ScopeSearchResultItem>[];

void visit(ScopeNode node) {
for (final node in candidates) {
final normalizedTitle = node.normalizedTitle;
final normalizedSubtitle = node.normalizedSubtitle;
final matches = findNormalizedTextMatches(
Expand All @@ -390,6 +415,7 @@ class ScopeTree {
);

if (matches) {
matchedNodes.add(node);
results.add(
ScopeSearchResultItem(
facet: node.facet,
Expand All @@ -409,15 +435,8 @@ class ScopeTree {
),
);
}

for (final child in node.children) {
visit(child);
}
}

for (final node in rootNodes) {
visit(node);
}
_lastSearch = (query: normalizedQuery, matches: matchedNodes);

results.sort((a, b) {
final scoreCompare = a.score.compareTo(b.score);
Expand Down
95 changes: 95 additions & 0 deletions test/search/scope_tree_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,38 @@ Library _lib(List<Category> categories) {
return library;
}

String _resultKey(ScopeSearchResultItem r) =>
'${r.facet}|${r.title}|${r.subtitle}|${r.isBook}|${r.score}|${r.lengthDelta}';

/// יותר מ-32 תוצאות שוות בכל מפתחות המיון: List.sort אינו יציב בהן, ולכן גם
/// סדרן תלוי בסדר הסריקה.
Library _tiedLibrary() => _lib([
for (var i = 0; i < 40; i++)
_mkCat(
'תיקייה $i',
books: [
TextBook(
title: 'פירוש',
categoryPath: '/תיקייה $i',
author: i.isEven ? 'רש"י' : 'רבי עקיבא',
),
TextBook(title: 'פֵּירוּשׁ רש"י על התורה'),
],
),
_mkCat(
'תורה',
books: [
TextBook(title: 'בראשית', categoryPath: '/תנ״ך/תורה'),
TextBook(title: 'ספר', author: 'פירושים'),
],
),
]);

/// חיפוש ראשון בעץ חדש — אין בו התאמות קודמות לצמצם מהן.
List<String> _freshSearch(String query) => ScopeTree.fromLibrary(
_tiedLibrary(),
).search(query).map(_resultKey).toList();

void main() {
late ScopeTree tree;

Expand Down Expand Up @@ -196,6 +228,69 @@ void main() {
expect(actual, isNotEmpty, reason: query);
}
});

test('רצף הקלדה, מחיקה וקפיצה: ההתאמות לפי findNormalizedTextMatches, '
'והתוצאות וסדרן כמו בחיפוש ראשון בעץ חדש', () {
final t = ScopeTree.fromLibrary(_tiedLibrary());
const queries = [
'פ',
'פי',
'פיר',
'פירו',
'פירוש',
'פירושי',
'פיר',
'פירוש ר',
'רשי',
'רשי ע',
'רשי עק',
'עקיבא',
'תנך/ת',
'תנך/תו',
'ספ',
'ספרז',
'ספרזז',
'ספר',
'ש',
'שי',
'פירוש',
];
for (final query in queries) {
final results = t.search(query);
expect(
results.map((r) => r.facet).toSet(),
{
for (final node in t.nodesByFacet.values)
if (findNormalizedTextMatches(
normalizedQuery: query,
normalizedPrimaryText: node.normalizedTitle,
normalizedSecondaryText: node.normalizedSubtitle,
))
node.facet,
},
reason: query,
);
expect(
results.map(_resultKey).toList(),
_freshSearch(query),
reason: query,
);
}
});

test('שני תפריטים על אותו עץ מקלידים לסירוגין', () {
final library = _tiedLibrary();
final shared = ScopeTree.fromLibrary(library);
expect(identical(ScopeTree.fromLibrary(library), shared), isTrue);
const queries = ['פי', 'רש', 'פיר', 'רשי', 'פירו', 'רשי ע', 'פירוש'];
for (final query in queries) {
expect(
shared.search(query).map(_resultKey).toList(),
_freshSearch(query),
reason: query,
);
}
});
});

group('ScopeTree — סדר תיקיות ראשיות (כמו מסך הספרייה)', () {
Expand Down
Loading