search method
Search for relevant chunks and assemble context for LLM.
adjacentChunks - Number of adjacent chunks to include before/after each
matched chunk (default: 0). Setting this to 1 will include the chunk
before and after each matched chunk, helping with long articles.
singleSourceMode - If true, only include chunks from the most relevant source.
sourceIds - Only search these sources. similarity of the results is
the cosine similarity with or without sourceIds.
Implementation
Future<RagSearchResult> search(
String query, {
int topK = 10,
int tokenBudget = 2000,
ContextStrategy strategy = ContextStrategy.relevanceFirst,
int adjacentChunks = 0,
bool singleSourceMode = false,
List<int>? sourceIds,
}) async {
// 1. Generate query embedding
final queryEmbedding = await EmbeddingService.embed(query);
// 2. Search chunks
late List<ChunkSearchResult> chunks;
try {
if (sourceIds != null && sourceIds.isNotEmpty) {
// Exact scan of the given sources; `similarity` is cosine, as below.
chunks = await rust_rag.searchChunksInSources(
collectionId: collectionId,
queryEmbedding: queryEmbedding,
topK: topK,
sourceIds: _toInt64List(sourceIds),
);
} else {
chunks = await rust_rag.searchChunksInCollection(
collectionId: collectionId,
queryEmbedding: queryEmbedding,
topK: topK,
);
}
} on RagError catch (e) {
e.when(
databaseError: (msg) =>
debugPrint('[SmartError] Search failed (database): $msg'),
ioError: (msg) => debugPrint('[SmartError] Search IO error: $msg'),
modelLoadError: (_) {},
invalidInput: (msg) =>
debugPrint('[SmartError] Invalid search parameters: $msg'),
staleSearchHandle: (msg) =>
debugPrint('[SmartError] Search stale handle: $msg'),
concurrentMutation: (msg) =>
debugPrint('[SmartError] Search concurrent mutation: $msg'),
internalError: (msg) =>
debugPrint('[SmartError] Search engine failure: $msg'),
unsupportedMigrationVersion: (axis, stored, supported) => debugPrint(
'[SmartError] Search blocked by unsupported migration axis '
'$axis (stored=$stored, supported=$supported)',
),
embeddingFingerprintMismatch: (stored, current, remaining) => debugPrint(
'[SmartError] Search blocked by fingerprint mismatch '
'(stored=$stored, current=$current, remaining=$remaining)',
),
unknown: (msg) => debugPrint('[SmartError] Unknown search error: $msg'),
);
rethrow;
}
// 3. Filter to single source FIRST (before adjacent expansion)
// Pass the original query for text matching
if (singleSourceMode && chunks.isNotEmpty) {
chunks = _filterToMostRelevantSource(chunks, query);
}
// 4. Expand with adjacent chunks (only for the selected source)
if (adjacentChunks > 0 && chunks.isNotEmpty) {
chunks = await _expandWithAdjacentChunks(chunks, adjacentChunks);
}
// 5. Assemble context (pass singleSourceMode to skip headers when single source)
final context = ContextBuilder.build(
searchResults: chunks,
tokenBudget: tokenBudget,
strategy: strategy,
singleSourceMode: singleSourceMode, // Pass through to skip headers
);
return RagSearchResult(chunks: chunks, context: context);
}