@@ -233,12 +233,9 @@ export async function startPlayground() {
233
233
234
234
try {
235
235
const client = await rag . getClient ( ) ;
236
- console . log ( "Using database:" , rag . config . database ) ;
237
- console . log ( "Using collection:" , rag . config . collection ) ;
238
-
239
236
const collection = client . db ( rag . config . database ) . collection ( rag . config . collection ) ;
240
237
const documents = await collection . find ( { } ) . toArray ( ) ;
241
- res . json ( documents ) ;
238
+ res . json ( { documents } ) ;
242
239
} catch ( error ) {
243
240
console . error ( 'Error fetching documents:' , error ) ;
244
241
res . status ( 500 ) . json ( { error : error . message } ) ;
@@ -277,6 +274,27 @@ export async function startPlayground() {
277
274
}
278
275
} ) ;
279
276
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
+
280
298
io . on ( 'connection' , ( socket ) => {
281
299
socket . on ( 'disconnect' , ( ) => {
282
300
console . log ( 'User disconnected' ) ;
0 commit comments