首先让我指出我是Neo4j的新手,如果有更好的方法,请告诉我。
其次,我怎样才能找到:
// QUERY
if(!empty($_SESSION['uid'])) {
$uid = $_SESSION['uid'];
$query = 'MATCH (cu:User)
WHERE cu.id = "'.$uid.'"
WITH cu
MATCH (p:Painting)<-[:PAINTED]-(u:User)-[:LIVES_IN]->(l:Location)
WHERE (p.slug) = "'.$slug.'"
RETURN p,u,l,
EXISTS((cu)-[:LIKES]->(p)) as liked,
EXISTS((cu)-[:FOLLOWS]->(u)) as followed,
SIZE((:User)-[:LIKES]->(p)) as total_likes,
SIZE((:User)-[:FOLLOWS]->(u)) as total_follows
LIMIT 1';
} else {
$query = 'MATCH (p:Painting)<-[:PAINTED]-(u:User)-[:LIVES_IN]->(l:Location) WHERE (p.slug) = "'.$slug.'"
RETURN p,u,l,
SIZE((:User)-[:LIKES]->(p)) as total_likes,
SIZE((:User)-[:FOLLOWS]->(u)) as total_follows
LIMIT 1';
}
所以现在我根据用户的可用性运行两个查询。我必须想象必须有一个更好/更有效的方法来做到这一点,但至少它现在有效。
答案 0 :(得分:0)
MATCH (p:Painting)<-[:PAINTED]-(u:User)-[:LIVES_IN]->(l:Location) WHERE (p.slug) = "blah-blah-blah"
WITH p, u, l
OPTIONAL MATCH (ul:User)-[likes:LIKES]->(p)
OPTIONAL MATCH (uf:User)-[follows:FOLLOWS]->(u)
RETURN p,u,l,ul,uf LIMIT 1
这会返回正确的图表,我无法弄清楚为什么我很难验证类似/跟随连接。
唉,我今天尝试过很多东西,我不知道上下是什么。我认为OPTIONAL MATCHES现在实际上已经处理了总值。我想我遇到的问题是验证现有用户是否喜欢或关注过。答案 1 :(得分:0)
听起来有一个当前用户的概念,您可能会通过ID或名称或类似物获得,并且您希望看到他们是否喜欢这幅画并跟随用户为其“slug”定义的这幅特定画作“属性(这是一个独特的价值,还是与多幅画相匹配?)
我会对这个进行一次尝试,并提供一些改进,以便更好地获得喜欢和跟随的数量。
MATCH (currentUser:User)
WHERE currentUser.ID = 123
WITH currentUser
MATCH (p:Painting)<-[:PAINTED]-(painter:User)-[:LIVES_IN]->(l:Location)
WHERE (p.slug) = "blah-blah-blah"
RETURN p,painter,l,
SIZE( (:User)-[:LIKES]->(p) ) as painting_likes,
SIZE( (:User)-[:FOLLOWS]->(painter) ) as painter_follows,
EXISTS( (currentUser)-[:LIKES]->(p) ) as current_user_liked,
EXISTS( (currentUser)-[:FOLLOWS]->(painter) ) as current_user_followed
LIMIT 1