Skip to content

Commit a1b819f

Browse files
committed
feat(lib): enhance text search with MongoDB $text operator
- Add support for MongoDB text search using the 'q' parameter - Refactor filter processing to handle text search and other conditions - Improve query robustness by using explicit $and operator for multiple conditions
1 parent a87624b commit a1b819f

File tree

1 file changed

+17
-8
lines changed

1 file changed

+17
-8
lines changed

lib/src/ht_data_mongodb.dart

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -143,16 +143,25 @@ class HtDataMongodb<T> implements HtDataClient<T> {
143143
return {};
144144
}
145145

146-
// If there's only one filter condition, we don't need to wrap it in $and.
147-
if (filter.length == 1) {
148-
_logger.finer('Built MongoDB selector: $filter');
149-
return filter;
146+
// Create a mutable copy to work with.
147+
final processedFilter = Map<String, dynamic>.from(filter);
148+
149+
// Check for the special 'q' parameter for text search.
150+
if (processedFilter.containsKey('q')) {
151+
final searchTerm = processedFilter.remove('q');
152+
// Add the MongoDB text search operator.
153+
// This assumes a text index exists on the collection.
154+
processedFilter[r'$text'] = {r'$search': searchTerm};
150155
}
151156

152-
// If there are multiple conditions, wrap them in an explicit $and operator
153-
// to ensure they are all applied correctly. This makes the query more
154-
// robust and avoids potential ambiguity.
155-
final andConditions = filter.entries
157+
// If there's only one filter condition after processing, return it directly.
158+
if (processedFilter.length == 1) {
159+
_logger.finer('Built MongoDB selector: $processedFilter');
160+
return processedFilter;
161+
}
162+
163+
// If there are multiple conditions, wrap them in an explicit $and operator.
164+
final andConditions = processedFilter.entries
156165
.map((entry) => {entry.key: entry.value})
157166
.toList();
158167

0 commit comments

Comments
 (0)