简体   繁体   中英

neo4j find all friends who posted something I liked

I want to find all the friends that posted something I liked. Why does this query return no rows but each clause by themselves returns something?

 match (start:Person)-[:LIKES]->(post:Post), (start)-[:FRIEND]->(person: Person), (person)-[:POSTED]->(post) return start, post, person

profile01 简介02

Thanks for the query plans.

We can see that the rows drop to zero on the expansion of (start)-[:FRIEND]->(person) , so one of two things is happening: either:FRIEND isn't the actual relationship type (probably an issue of mismatched cases), or more likely, all the:FRIEND relationships present are in the opposite direction, incoming to the start node instead of outgoing.

For social graphs like these, since they're both:Person nodes, there isn't a way to tell which direction the relationship was created in, and when you query, the direction shouldn't matter, all that should matter is that a:FRIEND relationship exists.

Try removing the direction from the pattern here: (start)-[:FRIEND]-(person: Person) . Provided your relationship type is correct, that should get you the right results.

For simplicity, I would also combine the 3 patterns into a single long pattern. Doing this will ensure that it doesn't pull extra patterns that don't fit later patterns (get retrieved, then filtered out later). Here is what that Cypher would look like:

MATCH (start:Person)-[:LIKES]->(post:Post)<-[:POSTED]-(person: Person)
WHERE (start)-[:FRIEND]-(person)
RETURN start, post, person

I would start by

MATCH (me:Person)-[:FRIEND]-(friend:Person)-[:POSTED]->(post:Post)<-[:LIKES]-(me)
RETURN DISTINCT friend

To get the (friend:Person) nodes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM