我有这种情况,我希望看到基于userId
和name
的结果(如果userId
是基于userId
提出的搜索,而名字是基于else
提出的搜索的名字),但我收到的是总是返回userId
而没有考虑function findUsers(userId, name, skip, limit) {
let queryString = (userId) ?
'MATCH (user:User {userId:{userId}}) ' :
'MATCH (user:User) ' +
'WHERE ((user.username =~ {name}) OR (user.name =~ {name})) ' +
'WITH user ' +
'ORDER BY user.name ' +
'RETURN DISTINCT user.userId, user.username, user.name ' +
'SKIP {skip} LIMIT {limit}';
let cypher = {
query: queryString,
params: {
userId: userId,
name: util.format('(?i).*%s.*', name),
skip: skip,
limit: limit
}
};
class Token(val original: TokenPattern)
答案 0 :(得分:1)
三元组的优先级比+
低,因此所有串联都放在else
部分中。使用括号,以便您根据三元选择合适的MATCH
选项。
let queryString = (
(userId) ?
'MATCH (user:User {userId:{userId}}) ' :
'MATCH (user:User) ') +
'WHERE ((user.username =~ {name}) OR (user.name =~ {name})) ' +
'WITH user ' +
'ORDER BY user.name ' +
'RETURN DISTINCT user.userId, user.username, user.name ' +
'SKIP {skip} LIMIT {limit}';
您甚至可以进一步缩小范围:
let queryString = 'MATCH (user:User ' + (userId ? '{userId:{userId}}' : '') + ') ' +
'WHERE ((user.username =~ {name}) OR (user.name =~ {name})) ' +
'WITH user ' +
'ORDER BY user.name ' +
'RETURN DISTINCT user.userId, user.username, user.name ' +
'SKIP {skip} LIMIT {limit}';