Skip to content

Commit

Permalink
feat: improve search precision using fuzzy search weighted ratio for …
Browse files Browse the repository at this point in the history
…title
  • Loading branch information
maelchiotti committed Apr 27, 2024
1 parent 496fd5a commit 7d43bb2
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/models/note/note.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:convert';

import 'package:equatable/equatable.dart';
import 'package:fleather/fleather.dart';
import 'package:fuzzywuzzy/fuzzywuzzy.dart';
import 'package:isar/isar.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:localmaterialnotes/utils/constants/constants.dart';
Expand Down Expand Up @@ -127,10 +128,15 @@ class Note extends Equatable {
return title.isEmpty && content == _emptyContent;
}

bool containsText(String search) {
bool matchesSearch(String search) {
final searchCleaned = search.toLowerCase().trim();

return title.toLowerCase().contains(searchCleaned) || plainText.toLowerCase().contains(searchCleaned);
final titleContains = title.toLowerCase().contains(searchCleaned);
final contentContains = title.toLowerCase().contains(searchCleaned);

final titleMatches = weightedRatio(plainText, searchCleaned) >= 50;

return titleContains || contentContains || titleMatches;
}

@override
Expand Down
8 changes: 8 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "4.0.0"
fuzzywuzzy:
dependency: "direct main"
description:
name: fuzzywuzzy
sha256: a84b99ebb21c448e02267070c91b218b4fbbef9c668b344aaeada49865985cae
url: "https://pub.dev"
source: hosted
version: "1.1.6"
glob:
dependency: transitive
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies:
flutter_native_splash: ^2.4.0
flutter_riverpod: ^2.5.1
flutter_settings_ui: ^3.0.1
fuzzywuzzy: ^1.1.6
go_router: ^13.2.2
intl: ^0.18.1
is_first_run: ^1.0.0
Expand Down

0 comments on commit 7d43bb2

Please sign in to comment.