JeffA
JeffA

Reputation: 166

Neo4j Cypher query need help getting expected response

Hello – I am looking for help with a cypher query that is not returning expected results for the question “Which boards is node1 pinned to depending on which user is logged in?”

Wrong result is obtained for user2. I expected that the OPTIONAL MATCH would "catch" user2 being a member of the workgroup that can access the board.

I've tried several different combinations of MATCH statements, UNION, etc.

Thanks - Jeff

I created a graph in http://console.neo4j.org/ with the following query:

CREATE (N1:node { name:"node1" })-[:PINNED_TO]->(B1:board { name:"board1" }),(N1)-[:PINNED_TO]->(B2:board { name:"board2" }),(N1)-[:PINNED_TO]->(B3:board { name:"board3" })
CREATE (U1:user { name:"user1" })-[:CREATES]->(B1),(U1)-[:CREATES]->(B2),(U3:user { name:"user3" })-[:CREATES]->(B3)
CREATE (B1)-[:CAN_VIEW]->(V1:view { name:"creator" }),(B2)-[:CAN_VIEW]->(V2:view { name:"creator AND workgroup" }),(B3)-[:CAN_VIEW]->(V3:view { name:"everyone" })
CREATE (U2:user { name:"user2" })-[:MEMBER_OF]->(WG2:workgroup { name:"B2 workgroup" })-[:CAN_ACCESS]->(B2)

I then queried the graph with the following (substituting user.name for each user):

MATCH (n:node)-[p:PINNED_TO]->(b:board)
MATCH (v:view)<-[:CAN_VIEW]-(b)<-[:CREATES]-(logged_in_user: user)
WHERE v.name = 'everyone' OR logged_in_user.name='user2'
OPTIONAL MATCH (b)<-[can_access:CAN_ACCESS]-(wg:workgroup)<-[:MEMBER_OF]-(logged_in_user)
WHERE logged_in_user.name='user2' OR can_access IS NOT NULL
RETURN DISTINCT logged_in_user.name, n.name, type(p), b.name

If “user1” is logged in, I correctly get the following:

logged_in_user.name n.name  type(p)     b.name
user1               node1   PINNED_TO   board1
user1               node1   PINNED_TO   board2
user1               node1   PINNED_TO   board3

If “user2” is logged in, I incorrectly get the following:

logged_in_user.name n.name  type(p)     b.name
user2               node1   PINNED_TO   board3

It should have returned:

logged_in_user.name n.name  type(p)     b.name
user2               node1   PINNED_TO   board2
user2               node1   PINNED_TO   board3

If “ user3” is logged in, I correctly get the following:

logged_in_user.name n.name  type(p)     b.name
user3               node1   PINNED_TO   board3

Upvotes: 0

Views: 82

Answers (2)

JeffA
JeffA

Reputation: 166

Ended up using UNION MATCH instead of OPTIONAL MATCH. For more nformation see http://neo4j.com/docs/stable/query-union.html

Thanks everyone for your help and suggestions. Cheers.

Upvotes: 0

cybersam
cybersam

Reputation: 67009

U1 and U3 have an outgoing CREATES relationship, but U2 (for user2) does not -- so your second MATCH will never match U2.

Also, as @WilliamLyon said, you need to use workgroup instead of workGroup in your query. But fixing that would not be sufficient to make your query work.

Upvotes: 0

Related Questions