@@ -38,6 +38,7 @@ export type profileQueryType = {
3838 skipId ?: string | undefined ,
3939 orderBy ?: string | undefined ,
4040 lastModificationWithin ?: string | undefined ,
41+ locale ?: string | undefined ,
4142} & {
4243 [ K in OptionTableKey ] ?: string [ ] | undefined
4344}
@@ -82,6 +83,7 @@ export const loadProfiles = async (props: profileQueryType) => {
8283 orderBy : orderByParam = 'created_time' ,
8384 lastModificationWithin,
8485 skipId,
86+ locale = 'en' ,
8587 } = props
8688
8789 const filterLocation = lat && lon && radius
@@ -102,6 +104,10 @@ export const loadProfiles = async (props: profileQueryType) => {
102104
103105 const userActivityJoin = 'user_activity on user_activity.user_id = profiles.user_id'
104106
107+ const joinInterests = ! ! interests ?. length
108+ const joinCauses = ! ! causes ?. length
109+ const joinWork = ! ! work ?. length
110+
105111 // Pre-aggregated interests per profile
106112 function getManyToManyJoin ( label : OptionTableKey ) {
107113 return `(
@@ -122,9 +128,9 @@ export const loadProfiles = async (props: profileQueryType) => {
122128 const joins = [
123129 orderByParam === 'last_online_time' && leftJoin ( userActivityJoin ) ,
124130 orderByParam === 'compatibility_score' && compatibleWithUserId && join ( compatibilityScoreJoin ) ,
125- interests && leftJoin ( interestsJoin ) ,
126- causes && leftJoin ( causesJoin ) ,
127- work && leftJoin ( workJoin ) ,
131+ joinInterests && leftJoin ( interestsJoin ) ,
132+ joinCauses && leftJoin ( causesJoin ) ,
133+ joinWork && leftJoin ( workJoin ) ,
128134 ]
129135
130136 const _orderBy = orderByParam === 'compatibility_score' ? 'cs.score' : `${ tablePrefix } .${ orderByParam } `
@@ -146,10 +152,23 @@ export const loadProfiles = async (props: profileQueryType) => {
146152 SELECT 1 FROM profile_${ label }
147153 JOIN ${ label } ON ${ label } .id = profile_${ label } .option_id
148154 WHERE profile_${ label } .profile_id = profiles.id
149- AND ${ label } .name = ANY (ARRAY[$(values)])
155+ AND ${ label } .id = ANY (ARRAY[$(values)])
150156 )`
151157 }
152158
159+ function getOptionClauseKeyword ( label : OptionTableKey ) {
160+ return `EXISTS (
161+ SELECT 1 FROM profile_${ label }
162+ JOIN ${ label } ON ${ label } .id = profile_${ label } .option_id
163+ LEFT JOIN ${ label } _translations
164+ ON ${ label } _translations.option_id = profile_${ label } .option_id
165+ AND ${ label } _translations.locale = $(locale)
166+ WHERE profile_${ label } .profile_id = profiles.id
167+ AND lower(COALESCE(${ label } _translations.name, ${ label } .name)) ILIKE '%' || lower($(word)) || '%'
168+ )`
169+ }
170+
171+
153172 const filters = [
154173 where ( 'looking_for_matches = true' ) ,
155174 where ( `profiles.disabled != true` ) ,
@@ -160,8 +179,14 @@ export const loadProfiles = async (props: profileQueryType) => {
160179 where ( `data->>'userDeleted' != 'true' or data->>'userDeleted' is null` ) ,
161180
162181 ...keywords . map ( word => where (
163- `lower(users.name) ilike '%' || lower($(word)) || '%' or lower(bio::text) ilike '%' || lower($(word)) || '%' or bio_tsv @@ phraseto_tsquery('english', $(word))` ,
164- { word}
182+ `lower(users.name) ilike '%' || lower($(word)) || '%'
183+ or lower(search_text) ilike '%' || lower($(word)) || '%'
184+ or search_tsv @@ phraseto_tsquery('english', $(word))
185+ OR ${ getOptionClauseKeyword ( 'interests' ) }
186+ OR ${ getOptionClauseKeyword ( 'causes' ) }
187+ OR ${ getOptionClauseKeyword ( 'work' ) }
188+ ` ,
189+ { word, locale}
165190 ) ) ,
166191
167192 genders ?. length && where ( `gender = ANY($(genders))` , { genders} ) ,
@@ -227,7 +252,7 @@ export const loadProfiles = async (props: profileQueryType) => {
227252 { religion}
228253 ) ,
229254
230- interests ?. length && where ( getManyToManyClause ( 'interests' ) , { values : interests } ) ,
255+ interests ?. length && where ( getManyToManyClause ( 'interests' ) , { values : interests . map ( Number ) } ) ,
231256
232257 causes ?. length && where ( getManyToManyClause ( 'causes' ) , { values : causes } ) ,
233258
@@ -278,9 +303,9 @@ export const loadProfiles = async (props: profileQueryType) => {
278303 } else if ( orderByParam === 'last_online_time' ) {
279304 selectCols += ', user_activity.last_online_time'
280305 }
281- if ( interests ) selectCols += `, COALESCE(profile_interests.interests, '{}') AS interests`
282- if ( causes ) selectCols += `, COALESCE(profile_causes.causes, '{}') AS causes`
283- if ( work ) selectCols += `, COALESCE(profile_work.work, '{}') AS work`
306+ if ( joinInterests ) selectCols += `, COALESCE(profile_interests.interests, '{}') AS interests`
307+ if ( joinCauses ) selectCols += `, COALESCE(profile_causes.causes, '{}') AS causes`
308+ if ( joinWork ) selectCols += `, COALESCE(profile_work.work, '{}') AS work`
284309
285310 const query = renderSql (
286311 select ( selectCols ) ,
0 commit comments