Re: [Neo4j] Cypher -- distance between relationship and node?

612 views
Skip to first unread message

Michael Hunger

unread,
Sep 17, 2012, 6:59:09 PM9/17/12
to ne...@googlegroups.com
could you try to look for the path between the user and the security relationship, and return them together with the length of that path (ordered by distance)

START u=node:Users("email:ha...@hogwarts.com"), f=node:Folders("uid:child") 
MATCH p = allShortestPaths(u-[*]->f) 
WITH head(filter(r in relationships(p) : type(r) = 'SECURITY')) as sec_rel, length(p) as len
    return sec_rel, length(u-[*0..len]-()-[r]-()) as distance
    order by distance asc

Michael

Am 17.09.2012 um 20:14 schrieb Matt Jones:

Hi,

I'm a newbie here, and I'm experimenting with using neo4j for an access control list.  There's lots of great documentation that has helped me get this far.  Thank you.

It turns out that the security relationship in a hierarchical relationship can nearly always be represented by the closest Security relationship. 

START u=node:Users("email:ha...@hogwarts.com"), f=node:Folders("uid:child")
MATCH p = allShortestPaths(u-[*]->f)
RETURN filter(r in relationships(p) : type(r) = 'SECURITY')

But sometimes, there is more than one route between the user and folder that is the same length.  For this, I need a bit more information to resolve the tie.  One example is below, where the user is granted read access on a parent folder, but the read permission has been revoked for a group that the user is a member of in a child folder.  In this case, I need to know the distance that the security relationship is from the folder.  In this case, the security relationship from the group to the child is closer than the security relationship from the user to the parent.  How can I get this distance in the cypher query?


Thanks in advance!

Matt

--
 
 

Matt Jones

unread,
Sep 18, 2012, 12:05:08 PM9/18/12
to ne...@googlegroups.com
Thanks Michael!

I like the with syntax to perform a subquery.  That seems like the better way to get the security relationship.  However, "length(u-[*0..len]-()-[r]-()) as distance" simply produces a SyntaxException: unknown identifier 'u'.  I tried putting in sec_rel and others but had no luck.  Any tips?


On Monday, September 17, 2012 5:59:15 PM UTC-5, Michael Hunger wrote:
could you try to look for the path between the user and the security relationship, and return them together with the length of that path (ordered by distance)

START u=node:Users("email...@hogwarts.com"), f=node:Folders("uid:child") 

MATCH p = allShortestPaths(u-[*]->f) 
WITH head(filter(r in relationships(p) : type(r) = 'SECURITY')) as sec_rel, length(p) as len
    return sec_rel, length(u-[*0..len]-()-[r]-()) as distance
    order by distance asc

Michael
Am 17.09.2012 um 20:14 schrieb Matt Jones:

Hi,

I'm a newbie here, and I'm experimenting with using neo4j for an access control list.  There's lots of great documentation that has helped me get this far.  Thank you.

It turns out that the security relationship in a hierarchical relationship can nearly always be represented by the closest Security relationship. 

START u=node:Users("email...@hogwarts.com"), f=node:Folders("uid:child")

Michael Hunger

unread,
Sep 18, 2012, 1:01:00 PM9/18/12
to ne...@googlegroups.com, ne...@googlegroups.com
Sorry, 

You have to add u to the with list
And remove r from the return expression

Michael

Sent from mobile device
--
 
 

Matt Jones

unread,
Sep 18, 2012, 7:11:51 PM9/18/12
to ne...@googlegroups.com
I made the updates you suggested, and then the lengths I was getting were way off.  With there being multiple paths between nodes, the length function on the second path returned a much longer path.  However, I was able to get what I was looking for (closest security relationship, with it's distance from the folder) with the following query:

START u=node:Users("email:ha...@hogwarts.com"), f=node:Folders("uid:ab7fefd3-c168-4b97-bb4b-4968b612396f") 
MATCH p = allShortestPaths(u-[*]->f) 
WITH head(filter(r in relationships(p) : type(r) = 'SECURITY')) as sec_rel, length(p) as totalLength, f
MATCH rp = ()-[sec_rel]-()-[*0..len]->f
RETURN sec_rel, MIN(LENGTH(rp)) as distance
ORDER BY MIN(LENGTH(rp))

Thanks for your help on this.  I certainly learned a lot about Cypher from this exercise.

Michael Hunger

unread,
Sep 19, 2012, 1:59:53 AM9/19/12
to ne...@googlegroups.com
You're welcome! Perhaps you can write up what you've learned in a blog post?

Thanks

Michael

Sent from mobile device
--
 
 
Reply all
Reply to author
Forward
0 new messages