neo4j,密码关系存在,关系属性存在于where,order by和limit
Posted
技术标签:
【中文标题】neo4j,密码关系存在,关系属性存在于where,order by和limit【英文标题】:neo4j, cypher relationship exist, relation property exsit with where, order by and limit 【发布时间】:2018-05-23 06:19:22 【问题描述】:我有一个具有这种模式的 neo4j 数据库 (:User)-[:ANSWER_TO_QUESTION]->(:Question)。
在我的应用程序中有一些问题和一些用户节点。用户将回答问题,而答案将保存用户与问题之间的关系。
用户可以跳过问题,答案将保存 null 与问题的关系。
每个问题都有一个回答周期时间,如果用户回答了问题,则重新提出问题;如果用户没有回答问题,则重新提出问题的无回答周期时间。
现在我想查询特殊用户未提出或未回答且已通过无回答周期或已回答且已通过回答周期的问题。按问题的优先级排序,然后返回一个问题。
我尝试使用联合查询,但我无法返回总结果的限制 1。所以查询将返回 3 个问题,每个联合查询一个。
MATCH (n:Users user_name:$user_name),(q:Questions)
Where NOT (n)-[:ANSWERED_OF_QUESTION]->(q)
return q as question
ORDER BY question.priority DESC
LIMIT 1
UNION MATCH (n:Users user_name:$user_name)-[r:ANSWERED_OF_QUESTION]->(q:Questions)
Where r.answer is null and r.created+q.no_answer_cycle < $now_time
RETURN q as question
ORDER BY question.priority DESC
LIMIT 1
UNION MATCH (n:Users user_name:$user_name)-[r:ANSWERED_OF_QUESTION]->(q:Questions
Where r.answer is not null and r.created+q.answer_cycle < $now_time
RETURN q as question
ORDER BY question.priority DESC
LIMIT 1
我尝试了 OPTIONAL MATCH 和 CASE,但没有得到我的结果。有什么帮助吗?
【问题讨论】:
【参考方案1】:这样简化怎么样?
MATCH (q:Questions)
WHERE NOT EXISTS ((:Users user_name:$user_name)-[:ANSWERED_OF_QUESTION]->(q))
OPTIONAL MATCH (n:Users user_name:$user_name)-[r:ANSWERED_OF_QUESTION]->(q)
WHERE (r.answer is null and r.created+q.no_answer_cycle < $now_time)
OR (r.answer is not null and r.created+q.answer_cycle < $now_time)
RETURN q as question
ORDER BY question.priority DESC
LIMIT 1
【讨论】:
Neo.ClientError.Statement.SyntaxError: 类型不匹配: 预期的关系但为 List我的答案是这样的:
call apoc.cypher.run("MATCH (n:Users user_name:$user_name),(q:Questions)
Where NOT (n)-[:ANSWERED_OF_QUESTION]->(q)
return q as question
ORDER BY question.priority DESC
LIMIT 1 UNION MATCH (n:Users user_name:$user_name)-[r:ANSWERED_OF_QUESTION]->(q:Questions)
Where r.answer is null
RETURN q as question
ORDER BY question.priority DESC
LIMIT 1
UNION MATCH (n:Users user_name:$user_name)-[r:ANSWERED_OF_QUESTION]->(q:Questions)
Where r.answer is not null
RETURN q as question
ORDER BY question.priority DESC
LIMIT 1",user_name:"saeed@saeed.com")
yield value
RETURN value
ORDER BY value.question.priority
DESC LIMIT 1'
编辑: 第二种解决方案:灵感来自lokesh
MATCH (q:Questions), (n:Users user_name:$user_name)
OPTIONAL MATCH (n)-[r:ANSWERED_OF_QUESTION]->(q)
WHERE (r.answer is null )
OR (r.answer is not null )
RETURN q as question
ORDER BY question.priority DESC
LIMIT 1
有什么改进建议吗?
【讨论】:
以上是关于neo4j,密码关系存在,关系属性存在于where,order by和limit的主要内容,如果未能解决你的问题,请参考以下文章