密码查询与多个匹配块的行为不符吗?

时间:2019-12-28 02:39:47

标签: neo4j cypher

我有以下查询:

MATCH (u:User) WHERE u.username = "ben"
OPTIONAL MATCH (u)-[:HAS]->(pl)

//MATCH (u)-[r1:IS_AT|PREFERS|DESIRES|VALUES]->()<-[]-(fp:FitnessProgram) WHERE NOT (fp)-[:LIMITED_BY]-(pl)
//WITH u,  pl, fp, coalesce(r1.importance, 0.5) AS importance
//WITH u,  pl, fp, collect({name: fp.name, importance: importance}) AS fpTraits
//WITH u,  pl, reduce(s = 0, t IN fpTraits | s + t.importance) AS fpScore order by fpScore

MATCH (u)-[r2:IS_AT|PREFERS|DESIRES|VALUES]->()<-[]-(ns:NutritionalSupplement) WHERE NOT (ns)-[:LIMITED_BY]-(pl)
WITH u,   ns, coalesce(r2.importance, 0.5) AS importance 
WITH u,   ns, collect({name: ns.name, importance: importance}) AS nsTraits
WITH u,   ns, reduce(s = 0, t IN nsTraits | s + t.importance) AS nsScore order by nsScore desc limit 5

return u, ns.name, nsScore

实际上,注释了4条线后,它可以正常工作,并为我提供了预期的前5种营养补品。

如果我注释掉底部的块并取消注释顶部的块,则该块也可以按预期工作。

如果我都没有像下面这样注释,那么两个块都不起作用,并且我得到了很多欺骗,而且比分都疯了……似乎两场比赛以某种我还不了解的方式合并了(我是是Neo4j的新功能)?

MATCH (u:User) WHERE u.username = "ben"
OPTIONAL MATCH (u)-[:HAS]->(pl)

MATCH (u)-[r1:IS_AT|PREFERS|DESIRES|VALUES]->()<-[]-(fp:FitnessProgram) WHERE NOT (fp)-[:LIMITED_BY]-(pl)
WITH u,  pl, fp, coalesce(r1.importance, 0.5) AS importance
WITH u,  pl, fp, collect({name: fp.name, importance: importance}) AS fpTraits
WITH u,  pl, fp, reduce(s = 0, t IN fpTraits | s + t.importance) AS fpScore order by fpScore desc limit 5

MATCH (u)-[r2:IS_AT|PREFERS|DESIRES|VALUES]->()<-[]-(ns:NutritionalSupplement) WHERE NOT (ns)-[:LIMITED_BY]-(pl)
WITH u, fp, fpScore,  ns, coalesce(r2.importance, 0.5) AS importance 
WITH u, fp, fpScore,  ns, collect({name: ns.name, importance: importance}) AS nsTraits
WITH u, fp, fpScore,  ns, reduce(s = 0, t IN nsTraits | s + t.importance) AS nsScore order by nsScore desc limit 5

return u, fp.name, fpScore, ns.name, nsScore

1 个答案:

答案 0 :(得分:0)

您期望在最后一个块中有fp个什么值?它不是上一个查询的一部分,所以我认为它不能出现在您的WITH语句中

您无需在fp语句中继续声明WITH

MATCH (u)-[r2:IS_AT|PREFERS|DESIRES|VALUES]->()<-[]-(ns:NutritionalSupplement) 
WHERE NOT (ns)-[:LIMITED_BY]-(pl)
WITH u, ns, coalesce(r2.importance, 0.5) AS importance 
WITH u, ns, collect({name: ns.name, importance: importance}) AS nsTraits
WITH u  ns, reduce(s = 0, t IN nsTraits | s + t.importance) AS nsScore order by nsScore desc limit 5

return u, fp.name, fpScore, ns.name, nsScore