22import chalk from 'chalk' ;
33import { isConfigValid } from '../../utils/validation.js' ;
44import MongoRAG from '../../../src/core/MongoRAG.js' ;
5- import { formatSearchResults } from '../../utils/formatting.js' ;
65
7- export async function searchDocuments ( config , query , options ) {
8- const isDevelopment = process . env . NODE_ENV === 'development' || process . env . NODE_ENV === 'test' ;
9-
10- // Add debug logging in development
11- if ( isDevelopment ) {
12- console . log ( 'Debug: Received config:' , config ) ;
13- }
14-
15- // More specific validation
16- if ( ! config || ! config . mongoUrl || ! config . database || ! config . collection ) {
17- throw new Error ( "Invalid configuration. Required fields: mongoUrl, database, collection" ) ;
18- }
19-
20- if ( ! config . embedding ?. dimensions ) {
21- throw new Error ( "Configuration missing embedding dimensions" ) ;
6+ export async function searchDocuments ( config , query , options = { } ) {
7+ if ( ! isConfigValid ( config ) ) {
8+ throw new Error ( "Configuration missing. Run 'npx mongodb-rag init' first." ) ;
229 }
2310
24- if ( isDevelopment ) {
25- console . log ( chalk . cyan . bold ( `📂 Database: ${ options . database || config . database } ` ) ) ;
26- console . log ( chalk . cyan . bold ( `📑 Collection: ${ options . collection || config . collection } ` ) ) ;
27- console . log ( chalk . yellow ( `🔍 Performing vector search using index: ${ config . indexName } ` ) ) ;
28- console . log ( chalk . yellow ( `📐 Expected embedding dimensions: ${ config . embedding . dimensions } ` ) ) ;
29- }
11+ const isDevelopment = process . env . NODE_ENV === 'development' || process . env . NODE_ENV === 'test' ;
3012
3113 try {
14+ if ( isDevelopment ) {
15+ console . log ( 'Creating MongoRAG instance...' ) ;
16+ }
17+
3218 const rag = new MongoRAG ( config ) ;
33- await rag . connect ( ) ;
3419
35- // Get a test embedding to verify dimensions
36- const testEmbedding = await rag . getEmbedding ( query ) ;
37- if ( testEmbedding . length !== config . embedding . dimensions ) {
38- throw new Error (
39- `Embedding dimension mismatch: Model produces ${ testEmbedding . length } -dimensional vectors, ` +
40- `but index expects ${ config . embedding . dimensions } dimensions. ` +
41- `Please update your configuration or recreate the index with correct dimensions.`
42- ) ;
20+ if ( isDevelopment ) {
21+ console . log ( 'Connecting to MongoDB...' ) ;
4322 }
4423
45- const searchParams = {
24+ await rag . connect ( ) ;
25+
26+ const searchOptions = {
4627 database : options . database || config . database ,
4728 collection : options . collection || config . collection ,
48- index : config . indexName ,
4929 maxResults : options . maxResults || config . search ?. maxResults || 5 ,
50- minScore : options . minScore || config . search ?. minScore || 0.7
30+ minScore : options . minScore || config . search ?. minScore || 0.7 ,
31+ includeMetadata : true
5132 } ;
5233
53- const results = await rag . search ( query , searchParams ) ;
54- return formatSearchResults ( results ) ;
55- } catch ( error ) {
5634 if ( isDevelopment ) {
57- console . error ( chalk . red ( "❌ Search failed:" ) , error . message ) ;
35+ console . log ( chalk . blue ( `🔍 Searching for: "${ query } "` ) ) ;
36+ console . log ( chalk . blue ( 'Search options:' ) , searchOptions ) ;
5837 }
38+
39+ const results = await rag . search ( query , searchOptions ) ;
40+
41+ if ( isDevelopment ) {
42+ console . log ( chalk . green ( `\n✨ Found ${ results . length } results:` ) ) ;
43+ }
44+
45+ // Format and display results
46+ results . forEach ( ( result , index ) => {
47+ console . log ( chalk . yellow ( `\n${ index + 1 } . Score: ${ result . score . toFixed ( 3 ) } ` ) ) ;
48+ console . log ( chalk . white ( result . content ) ) ;
49+
50+ if ( result . metadata ) {
51+ console . log ( chalk . gray ( 'Metadata:' ) , result . metadata ) ;
52+ }
53+ } ) ;
54+
55+ await rag . close ( ) ;
56+ return results ;
57+ } catch ( error ) {
58+ console . error ( chalk . red ( '❌ Search failed:' ) , error . message ) ;
5959 throw error ;
6060 }
61- }
61+ }
0 commit comments