1- // import { client } from "../utils/typesenseClient.js";
21import { User } from "../models/User.js" ;
32import { Challenge } from "../models/Challenge.js" ;
43
5- // export const test = async () => {
6- // try {
7- // const res = await client.health.retrieve();
8- // console.log("Typesense is alive:", res);
9- // } catch (err) {
10- // console.error("Connection failed:", err);
11- // }
12- // };
13-
14- // export const search = async (req, res) => {
15- // const { q } = req.query;
16-
17- // if (!q) return res.status(400).json({ error: "Missing search query" });
18-
19- // try {
20- // // Search users
21- // const userResults = await client.collections("users").documents().search({
22- // q,
23- // query_by: "name,username,profilePicture,collegeName,branch,points,rank", // adjust as per your schema
24- // per_page: 5,
25- // });
26-
27- // // Search challenges
28- // const challengeResults = await client
29- // .collections("challenges")
30- // .documents()
31- // .search({
32- // q,
33- // query_by: "title,description,category,difficulty,problemLink", // adjust as per your schema
34- // per_page: 5,
35- // });
36-
37- // // Add type labels
38- // const users = userResults.hits.map(hit => ({
39- // ...hit.document,
40- // type: "user",
41- // }));
42-
43- // const challenges = challengeResults.hits.map(hit => ({
44- // ...hit.document,
45- // type: "challenge",
46- // }));
47-
48- // // Merge & send
49- // res.json([...users, ...challenges]);
50- // } catch (err) {
51- // console.error(err);
52- // res.status(500).json({ error: "Search failed" });
53- // }
54- // };
55-
56- // export const searchUser = async (req, res) => {
57- // const { q } = req.query;
58-
59- // if (!q) return res.status(400).json({ error: "Missing search query" });
60-
61- // try {
62- // // Search users
63- // const userResults = await client.collections("users").documents().search({
64- // q,
65- // query_by: "name,username,profilePicture,collegeName,branch,points,rank",
66- // });
67-
68- // // Add type labels
69- // const users = userResults.hits.map(hit => ({
70- // ...hit.document,
71- // type: "user",
72- // }));
73- // res.json([...users]);
74- // } catch (err) {
75- // console.error(err);
76- // res.status(500).json({ error: "Search failed" });
77- // }
78- // };
79-
804export const search = async ( req , res ) => {
815 const { q } = req . query ;
82- if ( ! q ) {
6+ if ( ! q || q . trim ( ) === "" ) {
837 return res . status ( 400 ) . json ( { error : "Missing search query" } ) ;
848 }
859
8610 try {
87- const usersList = await User . find ( {
88- $or : [
89- { name : { $regex : q , $options : "i" } } ,
90- { username : { $regex : q , $options : "i" } } ,
91- { collegeName : { $regex : q , $options : "i" } } ,
92- { branch : { $regex : q , $options : "i" } } ,
93- ]
94- } ) . limit ( 5 ) . lean ( ) ;
95-
96- const challengesList = await Challenge . find (
97- {
11+ const searchTerm = q . trim ( ) ;
12+
13+ const [ usersList , challengesList ] = await Promise . all ( [
14+ User . find ( {
15+ $or : [
16+ { name : { $regex : searchTerm , $options : "i" } } ,
17+ { username : { $regex : searchTerm , $options : "i" } } ,
18+ { collegeName : { $regex : searchTerm , $options : "i" } } ,
19+ { branch : { $regex : searchTerm , $options : "i" } }
20+ ]
21+ } )
22+ . select ( "name username profilePicture collegeName branch points rank" )
23+ . limit ( 5 )
24+ . lean ( ) ,
25+
26+ Challenge . find ( {
9827 $or : [
99- { title : new RegExp ( q , "i" ) } ,
100- { description : new RegExp ( q , "i" ) } ,
101- { category : { $in : [ new RegExp ( q , "i" ) ] } } ,
102- { difficulty : new RegExp ( q , "i" ) } ,
103- { platform : new RegExp ( q , "i" ) } ,
104- ] ,
105- }
106- ) . limit ( 5 ) . lean ( ) ;
28+ { title : { $regex : searchTerm , $options : "i" } } ,
29+ { description : { $regex : searchTerm , $options : "i" } } ,
30+ { category : { $regex : searchTerm , $options : "i" } } ,
31+ { difficulty : { $regex : searchTerm , $options : "i" } } ,
32+ { platform : { $regex : searchTerm , $options : "i" } }
33+ ]
34+ } )
35+ . select ( "title description category difficulty platform" )
36+ . limit ( 5 )
37+ . lean ( )
38+ ] ) ;
10739
10840 const users = usersList . map ( ( u ) => ( {
10941 id : u . _id ?. toString ( ) ,
@@ -129,45 +61,48 @@ export const search = async (req, res) => {
12961
13062 res . json ( [ ...users , ...challenges ] ) ;
13163 } catch ( err ) {
132- // console.error("Search error:", err);
64+ console . error ( "Search error:" , err . message ) ;
13365 res . status ( 500 ) . json ( { error : "Search failed" } ) ;
13466 }
13567} ;
13668
13769export const searchUser = async ( req , res ) => {
138- const { q } = req . query ;
139-
140- if ( ! q ) {
141- return res . status ( 400 ) . json ( { error : "Missing search query" } ) ;
142- }
70+ const { q } = req . query ;
14371
144- try {
145- const users = await User . find ( {
146- $or : [
147- { name : { $regex : q , $options : "i" } } ,
148- { username : { $regex : q , $options : "i" } } ,
149- { collegeName : { $regex : q , $options : "i" } } ,
150- { branch : { $regex : q , $options : "i" } }
151- ]
152- } )
153- . limit ( 10 )
154- . lean ( ) ;
72+ if ( ! q || q . trim ( ) === "" ) {
73+ return res . status ( 400 ) . json ( { error : "Missing search query" } ) ;
74+ }
15575
156- const formattedUsers = users . map ( user => ( {
157- id : user . _id ?. toString ( ) ,
158- name : user . name ,
159- username : user . username ,
160- profilePicture : user . profilePicture ,
161- collegeName : user . collegeName ,
162- branch : user . branch ,
163- points : user . points ,
164- rank : user . rank ,
165- type : "user"
166- } ) ) ;
76+ try {
77+ const searchTerm = q . trim ( ) ;
78+
79+ const users = await User . find ( {
80+ $or : [
81+ { name : { $regex : searchTerm , $options : "i" } } ,
82+ { username : { $regex : searchTerm , $options : "i" } } ,
83+ { collegeName : { $regex : searchTerm , $options : "i" } } ,
84+ { branch : { $regex : searchTerm , $options : "i" } }
85+ ]
86+ } )
87+ . select ( "name username profilePicture collegeName branch points rank" )
88+ . limit ( 10 )
89+ . lean ( ) ;
90+
91+ const formattedUsers = users . map ( user => ( {
92+ id : user . _id ?. toString ( ) ,
93+ name : user . name ,
94+ username : user . username ,
95+ profilePicture : user . profilePicture ,
96+ collegeName : user . collegeName ,
97+ branch : user . branch ,
98+ points : user . points ,
99+ rank : user . rank ,
100+ type : "user"
101+ } ) ) ;
167102
168- res . json ( formattedUsers ) ;
169- } catch ( err ) {
170- // console.error(err);
171- res . status ( 500 ) . json ( { error : "Search failed" } ) ;
172- }
103+ res . json ( formattedUsers ) ;
104+ } catch ( err ) {
105+ console . error ( "Search user error:" , err . message ) ;
106+ res . status ( 500 ) . json ( { error : "Search failed" } ) ;
107+ }
173108} ;
0 commit comments