query method

  1. @override
Future<DVSearchResultPage<TModel>> query(
  1. String query, {
  2. TFacets? facets,
  3. int page = 1,
  4. int perPage = 20,
})
override

Implementation

@override
Future<DVSearchResultPage<TModel>> query(
  String query, {
  TFacets? facets,
  int page = 1,
  int perPage = 20,
}) async {
  if (page < 1) throw ArgumentError.value(page, 'page', 'must be positive');
  if (perPage < 1) {
    throw ArgumentError.value(perPage, 'perPage', 'must be positive');
  }

  final String needle = query.trim();
  final List<String> terms = needle.isEmpty
      ? const <String>[]
      : tuning.expand(needle);

  _read();
  final List<String> texts = _texts!;
  // The words within a typo of each term, found once in the vocabulary of
  // every record rather than once per record.
  final Map<String, Set<String>> typos = _typos(terms);

  bool documentMatches(int i) {
    if (terms.isEmpty) return true;
    final String text = texts[i];
    for (final String term in terms) {
      if (text.contains(term)) return true;
      // A typo has to be compared word by word: the whole document is never
      // within an edit or two of a single term.
      final Set<String> near = typos[term]!;
      if (near.isNotEmpty && _distinct![i].any(near.contains)) return true;
    }
    return false;
  }

  // Facet counts describe what the text query found, before the facet
  // filter narrows it -- otherwise every count but the selected one is zero
  // and the UI can only ever narrow further.
  final List<int> matchedAt = _ranked(typos,
    <int>[
      for (int i = 0; i < records.length; i++)
        if (documentMatches(i)) i,
    ],
    terms,
  );
  final List<TModel> textMatches = <TModel>[
    for (final int i in matchedAt) records[i],
  ];
  final Map<TModel, int> position = Map<TModel, int>.identity();
  for (final int i in matchedAt) {
    position[records[i]] = i;
  }

  final matches = textMatches
      .where((TModel record) => facetMatcher?.call(record, facets) ?? true)
      .toList(growable: false);

  final start = (page - 1) * perPage;
  final end =
      start + perPage > matches.length ? matches.length : start + perPage;
  final pageItems =
      start >= matches.length ? <TModel>[] : matches.sublist(start, end);

  final Map<String, Map<String, int>> counts =
      <String, Map<String, int>>{};
  for (final MapEntry<String, String Function(TModel)> facet
      in facetValues.entries) {
    final Map<String, int> byValue = <String, int>{};
    for (final TModel record in textMatches) {
      final String value = facet.value(record);
      byValue[value] = (byValue[value] ?? 0) + 1;
    }
    counts[facet.key] = byValue;
  }

  return DVSearchResultPage<TModel>(
    items: List<TModel>.unmodifiable(pageItems),
    total: matches.length,
    page: page,
    perPage: perPage,
    highlights: List<String>.unmodifiable(
      pageItems.map(
        (TModel record) => dvHighlight(
          _documents![position[record]!],
          terms,
          pre: tuning.highlightPre,
          post: tuning.highlightPost,
        ),
      ),
    ),
    facetCounts: Map<String, Map<String, int>>.unmodifiable(counts),
  );
}