Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit e270d2d

Browse files
Support keyword search
1 parent c2a2f5b commit e270d2d

File tree

1 file changed

+64
-1
lines changed

1 file changed

+64
-1
lines changed

src/common/es-helper.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ const USER_FILTER_TO_MODEL = {
5656
isAttribute: false,
5757
model: require('../models/Achievement'),
5858
queryField: 'name',
59+
esDocumentValueQuery: 'achievements.name',
5960
esDocumentQuery: 'achievements.id.keyword',
6061
values: []
6162
},
@@ -609,6 +610,64 @@ function setUserAttributesFiltersToEsQuery (filterClause, attributes) {
609610
}
610611
}
611612

613+
/**
614+
* Get skillIds matching the search keyword
615+
*
616+
* @param keyword the search keyword
617+
* @returns array of skillIds
618+
*/
619+
async function searchSkills (keyword) {
620+
const queryDoc = DOCUMENTS.skill
621+
const esQuery = {
622+
index: queryDoc.index,
623+
type: queryDoc.type,
624+
body: {
625+
query: {
626+
query_string: {
627+
default_field: 'name',
628+
query: `*${keyword}*`
629+
}
630+
},
631+
_source: 'id'
632+
}
633+
}
634+
635+
logger.debug(`ES query for searching skills: ${JSON.stringify(esQuery, null, 2)}`)
636+
const results = await esClient.search(esQuery)
637+
return results.hits.hits.map(hit => hit._source.id)
638+
}
639+
640+
async function setUserSearchClausesToEsQuery (boolClause, keyword) {
641+
const skillIds = await searchSkills(keyword)
642+
643+
boolClause.should.push({
644+
nested: {
645+
path: USER_ATTRIBUTE.esDocumentPath,
646+
query: {
647+
query_string: {
648+
query: `*${keyword}*`,
649+
fields: [USER_ATTRIBUTE.esDocumentValueStringQuery]
650+
}
651+
}
652+
}
653+
}, {
654+
query_string: {
655+
query: `*${keyword}*`,
656+
fields: [USER_FILTER_TO_MODEL.achievement.esDocumentValueQuery]
657+
}
658+
})
659+
660+
if (skillIds.length > 0) {
661+
boolClause.should.push({
662+
terms: {
663+
[USER_FILTER_TO_MODEL.skill.esDocumentQuery]: skillIds
664+
}
665+
})
666+
}
667+
668+
boolClause.minimum_should_match = 1
669+
}
670+
612671
/**
613672
* Build ES query from given filter.
614673
* @param filter
@@ -651,7 +710,7 @@ function buildEsQueryToGetAttributeValues (attributeId, attributeValue, page = 1
651710
}
652711
}]
653712

654-
if (attributeValue != null) {
713+
if (attributeValue !== null) {
655714
matchConditions.push({
656715
query_string: {
657716
default_field: USER_ATTRIBUTE.esDocumentValueStringQuery,
@@ -1204,6 +1263,10 @@ async function searchUsers (authUser, filter, params) {
12041263
esQuery.body.query.bool.filter = resolvedUserFilters
12051264
}
12061265

1266+
if (filter.keyword != null) {
1267+
await setUserSearchClausesToEsQuery(esQuery.body.query.bool, filter.keyword)
1268+
}
1269+
12071270
if (filter.attributes != null) {
12081271
setUserAttributesFiltersToEsQuery(esQuery.body.query.bool.filter, filter.attributes)
12091272
}

0 commit comments

Comments
 (0)