search method
Search for a given pattern
on the list, optionally limit
ing the result length
Implementation
List<Result<T>> search(String pattern, [int limit = -1]) {
if (list.isEmpty) return <Result<T>>[];
// Return original list as [List<Result>] if pattern is empty
if (pattern == '') {
return list
.map((item) => Result<T>(
item: item,
matches: const [],
score: 0,
))
.toList();
}
final searchers = _prepareSearchers(pattern);
final resultsAndWeights =
_search(searchers.tokenSearchers, searchers.fullSearcher);
_computeScore(resultsAndWeights.weights, resultsAndWeights.results);
if (options.shouldSort) {
_sort(resultsAndWeights.results);
}
if (limit > 0 && resultsAndWeights.results.length > limit) {
return resultsAndWeights.results.sublist(0, limit);
}
return resultsAndWeights.results;
}