假设我们有一个Neo4j图表,如(品牌) - [:from] - >(发布)< - [:likes] - (人)。
如何返回具有最少数量的品牌帖子的密码查询,例如3.我希望这是可扩展的,而不依赖于特定的属性属性值。
因此,结果将返回至少3个Brand节点的实例,以及5个来自Post和15个来自Person。
我尝试过几件不同的事情:
1。)为每个品牌声明几个变量名称(不可扩展)
Match (b:Brand)-[]->(p:Post)<-[]-(per:Person)
Match (b1:Brand)-[]->(p1:Post)<-[]-(per2:Person)
Match (b2:Brand)-[]->(p2:Post)<-[]-(per3:Person)
return b,b1,b2,p,p1,p2,per,per2,per3
limit 30
这不起作用,因为它基本上与
返回相同 Match (b:Brand)-[]->(p:Post)<-[]-(per:Person)
return b,p,per
limit 30
2。)使用foreach some
Match (b:Brand) WITH collect (distinct b) as bb
FOREACH (b in bb[0..3] | MATCH (b)-[]->(p:Post)<-[]-(per:Person))
RETURN b, p, per LIMIT 40
这不起作用,因为你不能在Foreach调用中使用Match。
我知道如何执行此操作的唯一方法是声明where子句及其唯一的属性品牌名称值,这些值不可伸缩。它看起来像这样:
Match (b:Brand)-[]->(p:Post)<-[]-(per:Person)
where b.brand = "b1" OR b.brand ="b2" or b.brand = "b3"
Return b,p,per
Limit 30
然而,上面仍然没有回复我想要的东西。
请帮忙。这是一个快速测试图表:
Create (b1:Brand {brand:'b1'})
Create (b2:Brand {brand:'b2'})
Create (b3:Brand {brand:'b3'})
Create (p1:Post {id: "001",message: "foo"})
Create (p2:Post {id: "002",message: "bar"})
Create (p3:Post {id: "003",message: "baz"})
Create (p4:Post {id: "004",message: "raz"})
Create (per1:Person {id: "001",name: "foo"})
Create (per2:Person {id: "002",name: "foo"})
Create (per3:Person {id: "003",name: "foo"})
Create (per4:Person {id: "004",name: "foo"})
Create (per5:Person {id: "005",name: "foo"})
Create (per6:Person {id: "006",name: "foo"})
Create (per7:Person {id: "007",name: "foo"})
Merge (b1)-[:FROM]->(p1)
Merge (b1)-[:FROM]->(p2)
Merge (b2)-[:FROM]->(p3)
Merge (b3)-[:FROM]->(p4)
Merge (per1)-[:LIKES]->(p1)
Merge (per1)-[:LIKES]->(p2)
Merge (per1)-[:LIKES]->(p3)
Merge (per2)-[:LIKES]->(p1)
Merge (per2)-[:LIKES]->(p4)
Merge (per3)-[:LIKES]->(p3)
Merge (per4)-[:LIKES]->(p1)
Merge (per5)-[:LIKES]->(p2)
Merge (per6)-[:LIKES]->(p1)
Merge (per6)-[:LIKES]->(p2)
Merge (per6)-[:LIKES]->(p3)
Merge (per6)-[:LIKES]->(p4)
Merge (per7)-[:LIKES]->(p4)
答案 0 :(得分:0)
您可以使用unwind
代替foreach
:
Match (b:Brand) WITH collect (distinct b) as bb
UNWIND bb[0..3] as b
MATCH (b)-[]->(p:Post)<-[]-(per:Person)
RETURN b, p, per LIMIT 40
或合并with
和limit
:
MATCH (b:Brand) WITH distinct b LIMIT 3
MATCH (b)-[]->(p:Post)<-[]-(per:Person)
RETURN b, p, per LIMIT 40