Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: duplicates from merge #445

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
3d1c91d
feat: pick the distance between words in search
Sivan22 Jan 16, 2025
2284461
fix: added fuzzy search and better layout for search
Sivan22 Jan 18, 2025
2c63845
Breaking: added reference to index
Sivan22 Jan 20, 2025
c34039f
fix: changed full text search layout
Sivan22 Jan 20, 2025
7a11721
fix: merged book list and book tree to one place, removing topics.
Sivan22 Jan 20, 2025
743f0be
fix: if index does not match, reset the index.
Sivan22 Jan 21, 2025
37ebf99
fix: roll back to query parser for fuzzy search.
Sivan22 Jan 21, 2025
f473348
feat: added results counts for every facet(topic)
Sivan22 Jan 22, 2025
8133dc3
fix: fixed the bug - endless loop
Sivan22 Jan 22, 2025
5f077da
feat: added search results counts to books.
Sivan22 Jan 22, 2025
4e16a8d
minor fixes
Sivan22 Jan 22, 2025
5c47294
feat: order search results by relevance/catalogue order
Sivan22 Jan 23, 2025
e95642f
fix: returned selectionArea
Sivan22 Jan 23, 2025
335199f
feat: added option to reset index
Sivan22 Jan 23, 2025
cf6791a
feat: faceted search
Sivan22 Jan 25, 2025
c48a327
refactor: reorganizes the code
Sivan22 Jan 26, 2025
725e0c8
Update flutter.yml
Sivan22 Jan 26, 2025
040ef36
chore: fixed use of search_engine from git
Sivan22 Jan 26, 2025
154f410
Merge branch 'more_work_on_search' of https://github.com/Sivan22/otza…
Sivan22 Jan 26, 2025
c377d3e
Update flutter.yml
Sivan22 Jan 26, 2025
0315fa7
chore: fixed typo
Sivan22 Jan 26, 2025
15914ca
Merge branch 'more_work_on_search' of https://github.com/Sivan22/otza…
Sivan22 Jan 26, 2025
87d995a
feat: adding selection using ctrl
Sivan22 Jan 28, 2025
fda889c
Merge branch 'main' into more_work_on_search
Sivan22 Jan 28, 2025
0963d37
fix conflicts
Sivan22 Jan 28, 2025
43c3fe1
fix: duplicates from merge
Sivan22 Jan 28, 2025
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
1 change: 1 addition & 0 deletions .github/workflows/flutter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
with:
channel: stable
cache: true
- run: sudo apt install ninja-build
- run: flutter pub get
- run: flutter build apk
- name: Upload apk
Expand Down
23 changes: 22 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,22 @@ migrate_working_dir/
.flutter-plugins-dependencies
.pub-cache/
.pub/
/build/
build/
/windows/
android/
linux/
macos/
ios/
.dart_tool
web/
images/
fonts/
assets/
.idea/




# Symbolication related
app.*.symbols

Expand All @@ -43,3 +57,10 @@ app.*.map.json
/android/app/debug
/android/app/profile
/android/app/release

.git/
.vscode/
android/
build/
fonts/

2 changes: 2 additions & 0 deletions lib/data/data_providers/file_system_data_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ class FileSystemData {
category.books.add(
PdfBook(
title: title,
category: category,
path: entity.path,
author: metadata[title]?['author'],
heShortDesc: metadata[title]?['heShortDesc'],
Expand All @@ -113,6 +114,7 @@ class FileSystemData {
final title = getTitleFromPath(entity.path);
category.books.add(TextBook(
title: title,
category: category,
author: metadata[title]?['author'],
heShortDesc: metadata[title]?['heShortDesc'],
pubDate: metadata[title]?['pubDate'],
Expand Down
4 changes: 1 addition & 3 deletions lib/data/data_providers/isar_data_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class IsarDataProvider {
}
isar.write((isar) => isar.refs.putAll(refs));
} catch (e) {
print(' Failed creating refs for ${book.title} $e');
print('Failed creating refs for ${book.title} $e');
}
}

Expand Down Expand Up @@ -144,7 +144,6 @@ class IsarDataProvider {
pdfBook: true,
pdfPath: pdfBooks[i].path,
);
print('Adding Pdf ref: ${ref.ref}');
isar.write((isar) => isar.refs.put(ref));
}
}
Expand Down Expand Up @@ -242,7 +241,6 @@ class IsarDataProvider {
linesNumOfbooksDone.value = 0;

for (TextBook book in books) {
print('Adding lines for ${book.title}');
await addLinesForBook(book);
linesNumOfbooksDone.value = books.indexOf(book) + 1;
}
Expand Down
49 changes: 31 additions & 18 deletions lib/data/data_providers/tantivy_data_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ class TantivyDataProvider {
/// Uses Hive for persistent storage of indexed book records, storing them in the 'index'
/// subdirectory of the configured library path.
TantivyDataProvider() {
reopenIndex();
}

void reopenIndex() {
String indexPath = (Settings.getValue('key-library-path') ?? 'C:/אוצריא') +
Platform.pathSeparator +
'index';

engine = SearchEngine.newInstance(path: indexPath);

//test the engine
searchTexts('בראשית', ['בראשית'], 1);
searchTexts('בראשית', ['/'], 1);

booksDone = Hive.box(
name: 'books_indexed',
Expand All @@ -67,14 +71,13 @@ class TantivyDataProvider {
.put('key-books-done', booksDone);
}

Future<int> countTexts(String query, List<String> books, String topics,
Future<int> countTexts(String query, List<String> books, List<String> facets,
{bool fuzzy = false, int distance = 2}) async {
final index = await engine;
if (!fuzzy) {
query = distance > 0 ? '"$query"~$distance' : '"$query"';
}
return index.count(
query: query, books: books, topics: topics, fuzzy: fuzzy);
return index.count(query: query, facets: facets, fuzzy: fuzzy);
}

/// Performs a synchronous search operation across indexed texts.
Expand All @@ -86,8 +89,10 @@ class TantivyDataProvider {
///
/// Returns a Future containing a list of search results
Future<List<SearchResult>> searchTexts(
String query, List<String> books, int limit,
{bool fuzzy = false, int distance = 2}) async {
String query, List<String> facets, int limit,
{ResultsOrder order = ResultsOrder.relevance,
bool fuzzy = false,
int distance = 2}) async {
SearchEngine index;
try {
index = await engine;
Expand All @@ -102,7 +107,6 @@ class TantivyDataProvider {
"PanicException(Failed to create index: SchemaError(\"An index exists but the schema does not match.\"))") {
Directory indexDirectory = Directory(indexPath);
Hive.box(name: 'books_indexed', directory: indexPath).close();
print('Deleting index and creating a new one');
indexDirectory.deleteSync(recursive: true);
indexDirectory.createSync(recursive: true);
engine = SearchEngine.newInstance(path: indexPath);
Expand All @@ -115,7 +119,7 @@ class TantivyDataProvider {
query = distance > 0 ? '"$query"~$distance' : '"$query"';
}
return await index.search(
query: query, books: books, limit: limit, fuzzy: fuzzy);
query: query, facets: facets, limit: limit, fuzzy: fuzzy, order: order);
}

/// Performs an asynchronous stream-based search operation across indexed texts.
Expand All @@ -127,10 +131,10 @@ class TantivyDataProvider {
///
/// Returns a Stream of search results that can be listened to for real-time updates
Stream<List<SearchResult>> searchTextsStream(
String query, List<String> books, int limit, bool fuzzy) async* {
String query, List<String> facets, int limit, bool fuzzy) async* {
final index = await engine;
yield* index.searchStream(
query: query, books: books, limit: limit, fuzzy: fuzzy);
query: query, facets: facets, limit: limit, fuzzy: fuzzy);
}

/// Indexes all books in the provided library within the specified range.
Expand All @@ -154,13 +158,13 @@ class TantivyDataProvider {
if (!isIndexing.value) {
return;
}
print('Adding ${book.title} to index');
try {
// Handle different book types appropriately
if (book is TextBook) {
await addTextsToTantivy(book);
} else if (book is PdfBook) {
await addPdfTextsToTantivy(book);
await addPdfTextsToTantivy(book);
}
} catch (e) {
print('Error adding ${book.title} to index: $e');
Expand Down Expand Up @@ -190,7 +194,6 @@ class TantivyDataProvider {
// Check if book was already indexed using content hash
final hash = sha1.convert(utf8.encode(text)).toString();
if (booksDone.contains(hash)) {
print('${book.title} already in index');
numOfbooksDone.value = numOfbooksDone.value! + 1;
return;
}
Expand Down Expand Up @@ -220,10 +223,10 @@ class TantivyDataProvider {
line = stripHtmlIfNeeded(line);
line = removeVolwels(line);
index.addDocument(
id: BigInt.from(hashCode + i),
id: BigInt.from(DateTime.now().microsecondsSinceEpoch),
title: title,
reference: stripHtmlIfNeeded(reference.join(', ')),
topics: topics,
topics: '$topics/$title',
text: line,
segment: BigInt.from(i),
isPdf: false,
Expand All @@ -234,7 +237,6 @@ class TantivyDataProvider {
await index.commit();
booksDone.add(hash);
saveBooksDoneToDisk();
print('Added ${book.title} to index');
numOfbooksDone.value = numOfbooksDone.value! + 1;
}

Expand All @@ -253,7 +255,6 @@ class TantivyDataProvider {
final data = await File(book.path).readAsBytes();
final hash = sha1.convert(data).toString();
if (booksDone.contains(hash)) {
print('${book.title} already in index');
numOfbooksDone.value = numOfbooksDone.value! + 1;
return;
}
Expand All @@ -275,7 +276,7 @@ class TantivyDataProvider {
id: BigInt.from(DateTime.now().microsecondsSinceEpoch),
title: title,
reference: '$title, עמוד ${i + 1}',
topics: topics,
topics: '$topics/$title',
text: texts[j],
segment: BigInt.from(i),
isPdf: true,
Expand All @@ -286,7 +287,19 @@ class TantivyDataProvider {
await index.commit();
booksDone.add(hash);
saveBooksDoneToDisk();
print('Added ${book.title} to index');
numOfbooksDone.value = numOfbooksDone.value! + 1;
}

void clear() async {
final index = await engine;
await index.clear();
booksDone.clear();
saveBooksDoneToDisk();
}

void cancelIndexing() async {
isIndexing.value = false;
numOfbooksDone.value = null;
numOfbooksTotal.value = null;
}
}
3 changes: 0 additions & 3 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,5 @@ void createDirectoryIfNotExists(String path) {
Directory directory = Directory(path);
if (!directory.existsSync()) {
directory.createSync(recursive: true);
print('Directory created: $path');
} else {
print('Directory already exists: $path');
}
}
20 changes: 19 additions & 1 deletion lib/models/app_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ import 'package:flutter_settings_screens/flutter_settings_screens.dart';
import 'package:fuzzywuzzy/fuzzywuzzy.dart';
import 'package:hive/hive.dart';
import 'package:otzaria/data/data_providers/file_system_data_provider.dart';
import 'package:otzaria/data/data_providers/tantivy_data_provider.dart';
import 'package:otzaria/data/repository/data_repository.dart';
import 'package:otzaria/models/bookmark.dart';
import 'package:otzaria/models/books.dart';
import 'package:otzaria/models/library.dart';
import 'package:otzaria/models/tabs.dart';
import 'package:otzaria/models/tabs/pdf_tab.dart';
import 'package:otzaria/models/tabs/searching_tab.dart';
import 'package:otzaria/models/tabs/tabs.dart';
import 'package:otzaria/models/tabs/text_tab.dart';
import 'package:otzaria/models/workspace.dart';
import 'package:otzaria/utils/calendar.dart';
import 'package:otzaria/utils/text_manipulation.dart' as utils;
Expand Down Expand Up @@ -118,6 +122,10 @@ class AppModel with ChangeNotifier {
Settings.getValue<bool>('key-use-fast-search') ?? true,
);

final ValueNotifier<bool> replaceHolyNames = ValueNotifier<bool>(
Settings.getValue<bool>('key-replace-holy-names') ?? true,
);

/// Focus node for the book locator search field
FocusNode bookLocatorFocusNode = FocusNode();

Expand Down Expand Up @@ -206,6 +214,15 @@ class AppModel with ChangeNotifier {
isDarkMode.addListener(() {
notifyListeners();
});
() async {
//Check if index is up to date
final totalBooks = (await library).getAllBooks().length;
final indexedBooks = TantivyDataProvider.instance.booksDone.length;
if (!TantivyDataProvider.instance.isIndexing.value &&
totalBooks - indexedBooks > 100) {
DataRepository.instance.addAllTextsToTantivy(await library);
}
}();
}

/// Opens a book in a new tab.
Expand Down Expand Up @@ -550,6 +567,7 @@ class AppModel with ChangeNotifier {
libraryPath = Settings.getValue<String>('key-library-path') ?? libraryPath;
FileSystemData.instance.libraryPath = libraryPath;
library = data.getLibrary();
TantivyDataProvider.instance.reopenIndex();
notifyListeners();
}
}
Expand Down
21 changes: 16 additions & 5 deletions lib/models/books.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:otzaria/data/data_providers/file_system_data_provider.dart';
import 'package:otzaria/models/library.dart';
import 'package:otzaria/models/links.dart';
//import 'package:pdfrx/pdfrx.dart';

Expand All @@ -11,6 +12,8 @@ abstract class Book {
/// The title of the book.
final String title;

final Category? category;

/// Additional titles of the book, if available.
final List<String>? extraTitles;

Expand Down Expand Up @@ -39,9 +42,13 @@ abstract class Book {
factory Book.fromJson(Map<String, dynamic> json) {
switch (json['type']) {
case 'TextBook':
return TextBook(title: json['title']);
return TextBook(
title: json['title'], category: Category.fromJson(json));
case 'PdfBook':
return PdfBook(title: json['title'], path: json['path']);
return PdfBook(
title: json['title'],
path: json['path'],
category: Category.fromJson(json));
case 'OtzarBook':
return ExternalBook.fromJson(json);
default:
Expand All @@ -54,6 +61,7 @@ abstract class Book {
/// The [title] parameter is required and cannot be null.
Book(
{required this.title,
this.category,
this.author,
this.heShortDesc,
this.pubDate,
Expand All @@ -69,6 +77,7 @@ abstract class Book {
class TextBook extends Book {
TextBook(
{required String title,
super.category,
super.author,
super.heShortDesc,
super.pubDate,
Expand Down Expand Up @@ -98,6 +107,7 @@ class TextBook extends Book {
factory TextBook.fromJson(Map<String, dynamic> json) {
return TextBook(
title: json['title'],
category: Category.fromJson(json['category']),
);
}

Expand Down Expand Up @@ -210,6 +220,7 @@ class PdfBook extends Book {
final String path;
PdfBook(
{required String title,
super.category,
required this.path,
super.topics,
super.author,
Expand All @@ -221,9 +232,9 @@ class PdfBook extends Book {

factory PdfBook.fromJson(Map<String, dynamic> json) {
return PdfBook(
title: json['title'],
path: json['path'],
);
title: json['title'],
path: json['path'],
category: Category.fromJson(json['category']));
}

@override
Expand Down
Loading
Loading