Skip to content

A cross platform Audiobookshelf client for Android and iOS

Dart

Stop search from missing books added after a library passes 10,000 titles

Barnabas9e44572cf89c
diff --git a/lib/screens/library_screen.dart b/lib/screens/library_screen.dart
index bc51d3cb..6e221807 100644
--- a/lib/screens/library_screen.dart
+++ b/lib/screens/library_screen.dart
@@ -2083,6 +2083,21 @@ class LibraryScreenState extends State<LibraryScreen>
)
.toList();
}
+ // A truncated index (item cap) can't see the newest books in a huge
+ // library, but the server's SQL search can - keep any server hit the
+ // index missed instead of letting the override erase it (GH #349).
+ if (BookSearchIndex().isTruncated(libId)) {
+ final seen = books
+ .map((b) => (b['libraryItem'] as Map<String, dynamic>)['id'])
+ .toSet();
+ for (final r in (result?['book'] as List<dynamic>? ?? const [])) {
+ final m = r as Map<String, dynamic>;
+ final item = (m['libraryItem'] as Map<String, dynamic>?) ?? m;
+ if (seen.contains(item['id'])) continue;
+ if (_hideEbookOnly && PlayerSettings.isEbookOnly(item)) continue;
+ books.add({'libraryItem': item, '_titleMatch': true});
+ }
+ }
setState(() => _searchBookResults = books);
}
}
diff --git a/lib/services/android_auto_service.dart b/lib/services/android_auto_service.dart
index 0d18f41e..16fb1fe4 100644
--- a/lib/services/android_auto_service.dart
+++ b/lib/services/android_auto_service.dart
@@ -1674,7 +1674,10 @@ class AndroidAutoService {
if (library?.isPodcast != true) {
final index = BookSearchIndex();
await index.ensureIndex(api, libId);
- if (index.isReady(libId)) {
+ // A truncated index misses the newest items in a huge library, so its
+ // hits alone aren't the full answer - fall through to the server
+ // search and merge instead of returning early (GH #349).
+ if (index.isReady(libId) && !index.isTruncated(libId)) {
final hits = index
.search(libId, trimmedQuery, limit: 20)
.where((hit) => hit.item['mediaType'] != 'podcast')
diff --git a/lib/services/book_search_index.dart b/lib/services/book_search_index.dart
index 6911e17d..9e1f256e 100644
--- a/lib/services/book_search_index.dart
+++ b/lib/services/book_search_index.dart
@@ -38,6 +38,10 @@ class BookSearchIndex {
final Map<String, List<_Indexed>> _cache = {};
final Map<String, Future<void>> _inFlight = {};
+ // Libraries whose index hit the item cap: their search results are missing
+ // the newest items, so callers must merge the server's results in rather
+ // than replacing them (GH #349 - books past 10k vanished from search).
+ final Set<String> _truncated = {};
void _onItemUpdated(Map<String, dynamic> item) => patchItem(item);
@@ -69,11 +73,16 @@ class BookSearchIndex {
bool isReady(String libraryId) => _cache.containsKey(libraryId);
+ /// True when the library has more items than the index cap, meaning search
+ /// results from this index are incomplete for the newest items.
+ bool isTruncated(String libraryId) => _truncated.contains(libraryId);
+
/// Drop everything (call on logout / account switch so a reused libraryId
/// can't serve another account's items).
void clear() {
_cache.clear();
_inFlight.clear();
+ _truncated.clear();
}
/// Build the index for [libraryId] if not already cached. Concurrent callers
@@ -111,8 +120,11 @@ class BookSearchIndex {
}
}
if (pages > maxPages) {
+ _truncated.add(libraryId);
debugPrint('[BookSearchIndex] $libraryId has $total items; indexed first '
- '${maxPages * pageSize}. Search may miss the rest.');
+ '${maxPages * pageSize}. Callers merge server results for the rest.');
+ } else {
+ _truncated.remove(libraryId);
}
_cache[libraryId] = items.map(_indexItem).toList();
}
diff --git a/lib/widgets/add_books_search_sheet.dart b/lib/widgets/add_books_search_sheet.dart
index 701ba816..edf7e7fb 100644
--- a/lib/widgets/add_books_search_sheet.dart
+++ b/lib/widgets/add_books_search_sheet.dart
@@ -122,6 +122,18 @@ class _AddBooksSearchSheetState extends State<AddBooksSearchSheet> {
.search(widget.libraryId, query)
.map((h) => h.item)
.toList();
+ // A truncated index misses the newest items in a huge library - merge
+ // in server hits the index doesn't know about (GH #349).
+ if (BookSearchIndex().isTruncated(widget.libraryId)) {
+ final server = await api.searchLibrary(widget.libraryId, query);
+ if (!mounted || _controller.text.trim() != query) return;
+ final seen = results.map((i) => i['id']).toSet();
+ for (final r in (server?['book'] as List<dynamic>? ?? const [])) {
+ final m = r as Map<String, dynamic>;
+ final item = (m['libraryItem'] as Map<String, dynamic>?) ?? m;
+ if (!seen.contains(item['id'])) results.add(item);
+ }
+ }
} else {
// Index didn't build (e.g. offline) - fall back to the server search.
final result = await api.searchLibrary(widget.libraryId, query);