Last active
December 16, 2021 09:11
-
-
Save blaugold/5d9014a94a06fedbc8d01d66da7b7769 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Future<List<NoteSearchResult>> searchNotes(Query queryString) async { | |
// Creating a query has some overhead and if a query is executed | |
// many times, it should be created once and reused. For simplicity | |
// we don't do that here. | |
final query = await Query.fromN1ql( | |
database, | |
r''' | |
SELECT META().id AS id, title | |
FROM _ | |
WHERE type = 'note' AND match(note-fts, $query) | |
ORDER BY rank(note-fts) | |
LIMIT 10 | |
''', | |
); | |
// Query parameters are defined by prefixing an identifier with `$`. | |
await query.setParameters(Parameters({'query': '$queryString*'})); | |
// Each time a query is executed, its results are returned in a ResultSet. | |
final resultSet = await query.execute(); | |
// To create the NoteSearchResults, we turn the ResultSet into a stream | |
// and collect the results into a List, after transforming them into | |
// NoteSearchResults. | |
return resultSet.asStream().map(NoteSearchResult.fromResult).toList(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment