Skip to content

Instantly share code, notes, and snippets.

@blaugold
Last active December 16, 2021 09:11
Show Gist options
  • Save blaugold/5d9014a94a06fedbc8d01d66da7b7769 to your computer and use it in GitHub Desktop.
Save blaugold/5d9014a94a06fedbc8d01d66da7b7769 to your computer and use it in GitHub Desktop.
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