rebuildIndex method

Future<void> rebuildIndex({
  1. bool force = false,
  2. String? collectionId,
})

Rebuild the HNSW index after adding documents.

Call this after adding one or more documents for optimal search performance. The index enables fast approximate nearest neighbor search. force - If true, rebuilds even if no changes were detected (default: false).

Implementation

Future<void> rebuildIndex({bool force = false, String? collectionId}) async {
  _ensureEmbeddingFingerprintUnlocked();
  return _runOperation(() async {
    final normalized = _normalizeCollectionId(collectionId);
    final service = await _serviceForCollection(normalized);

    if (normalized == SourceRagService.defaultCollectionId) {
      _indexDebounceTimer
          ?.cancel(); // Cancel any pending auto-rebuild since we are doing it now
      _indexDebounceTimer = null;
    }

    return service.rebuildIndex(force: force); // Service handles dirty check
  });
}