rebuildIndex method

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

Rebuild the HNSW and BM25 indexes after adding sources. force - If true, rebuilds even if no changes were detected (default: false).

Implementation

Future<void> rebuildIndex({bool force = false}) {
  if (!force && !_needsRebuild) {
    debugPrint(
      '[SourceRagService] Index is already up to date. Skipping rebuild.',
    );
    return _warmupFuture;
  }
  _isIndexReady = false;
  final startDirtyVersion = _dirtyVersion;

  final rebuildFuture = _enqueueIndexTask(() async {
    try {
      // Rebuild HNSW for vector search
      await rust_rag.rebuildChunkHnswIndexForCollection(
        collectionId: collectionId,
      );
      await saveIndex(); // Persist to disk immediately

      // Rebuild BM25 for keyword search (critical for hybrid search!)
      await rust_rag.rebuildChunkBm25IndexForCollection(
        collectionId: collectionId,
      );

      await _markClean(
        expectedVersion: startDirtyVersion,
      ); // Mark index as clean (Persistent)
      _isIndexReady = !_needsRebuild;
    } on RagError catch (e) {
      _isIndexReady = false;
      e.when(
        databaseError: (msg) =>
            debugPrint('[SmartError] DB error rebuilding index: $msg'),
        ioError: (msg) =>
            debugPrint('[SmartError] IO error rebuilding index: $msg'),
        modelLoadError: (_) {},
        invalidInput: (_) {},
        internalError: (msg) => debugPrint(
          '[SmartError] Internal error rebuilding indexes: $msg',
        ),
        unknown: (_) {},
      );
      rethrow;
    }
  });

  _warmupFuture = rebuildFuture;
  return rebuildFuture;
}