search method

Future<List<Result<T>>> search(
  1. String pattern, [
  2. int limit = -1
])

Search for a given pattern on the list, optionally limiting the result length

Implementation

Future<List<Result<T>>> search(String pattern, [int limit = -1]) async {
  if (source.isEmpty) {
    return <Result<T>>[];
  }

  // Return original list as [List<Result>] if pattern is empty
  if (pattern.isEmpty) {
    return source
        .map((item) => Result<T>(
              identifier: _identifierFor(item),
              item: item,
              matches: const [],
              score: 0,
            ))
        .toList();
  }

  final searchers = _prepareSearchers(pattern);
  final resultsAndWeights = await _search(searchers.tokenSearchers, searchers.fullSearcher);

  _computeScore(resultsAndWeights.weights, resultsAndWeights.results);

  if (options.shouldSort) {
    _sort(resultsAndWeights.results);
  }

  if (limit > 0) {
    return resultsAndWeights.results.take(limit).toList();
  }

  return resultsAndWeights.results;
}