@@ -233,12 +233,9 @@ export async function startPlayground() {
233233
234234 try {
235235 const client = await rag . getClient ( ) ;
236- console . log ( "Using database:" , rag . config . database ) ;
237- console . log ( "Using collection:" , rag . config . collection ) ;
238-
239236 const collection = client . db ( rag . config . database ) . collection ( rag . config . collection ) ;
240237 const documents = await collection . find ( { } ) . toArray ( ) ;
241- res . json ( documents ) ;
238+ res . json ( { documents } ) ;
242239 } catch ( error ) {
243240 console . error ( 'Error fetching documents:' , error ) ;
244241 res . status ( 500 ) . json ( { error : error . message } ) ;
@@ -277,6 +274,27 @@ export async function startPlayground() {
277274 }
278275 } ) ;
279276
277+ // Add this endpoint to handle search queries
278+ app . post ( '/api/search' , async ( req , res ) => {
279+ if ( ! rag ) {
280+ return res . status ( 503 ) . json ( { error : "MongoDB connection not available" } ) ;
281+ }
282+
283+ const { query } = req . body ;
284+
285+ try {
286+ const client = await rag . getClient ( ) ;
287+ const collection = client . db ( rag . config . database ) . collection ( rag . config . collection ) ;
288+
289+ // Perform a search using the query
290+ const results = await collection . find ( { $text : { $search : query } } ) . toArray ( ) ;
291+ res . json ( { results } ) ;
292+ } catch ( error ) {
293+ console . error ( 'Error performing search:' , error ) ;
294+ res . status ( 500 ) . json ( { error : error . message } ) ;
295+ }
296+ } ) ;
297+
280298 io . on ( 'connection' , ( socket ) => {
281299 socket . on ( 'disconnect' , ( ) => {
282300 console . log ( 'User disconnected' ) ;
0 commit comments