Cypher: Filtering on properties when doing 'allShortestPaths'

427 views
Skip to first unread message

Mark Needham

unread,
May 7, 2012, 11:55:49 AM5/7/12
to ne...@googlegroups.com
Hey guys,

I've remodelled the data set I'm working on so that it now has the following nodes/edges:

Person X -[worked on]-> Project 1
Person Y -[worked on]-> Project 1

where the 'worked on' relationship also contains properties such as 'start_date' and 'end_date' since people often worked on the same project in non overlapping intervals in which case there shouldn't be a relationship between them. 

This is the query I have for linking directly between two people:

start n1=node(1), n2=node(2) 
match n1-[r:worked_on]->project<-[r2:worked_on]-n2, project<-[r3:has]-client 
where n1 <> n2  and r.start_date <= r2.end_date and r.end_date >= r2.start_date 
return distinct n1.name, project.name, n2.name, r.date, r2.date, client.name

Which works exactly how I want.

I've run into problems when trying to find the shortest path between nodes because I'm not sure how I can do the date checks in that query:

START a=node(1), x=node(2) 
MATCH p = allShortestPaths( a-[:worked_on*]-x ) 
RETURN p, extract(person in nodes(p) : person.name)

I want to do the same thing that I did above where I check the 'start_date' and 'end_date' which people worked on the project and only return that connection if the two people overlapped. Right now it will return a connection between people even if they were on a project at different times.

I thought maybe I should be using the 'relationships' function specified on the cypher documentation page but I don't really know how.

Any ideas?

Cheers, Mark

Andres Taylor

unread,
May 7, 2012, 1:14:22 PM5/7/12
to ne...@googlegroups.com
It's not possible to do this right now. You want the shortest path that fulfills a set of predicates, and Cypher can't do that yet. It's in the back log to expand shortestPath with this. I hope to be able to provide this in 1.8GA, but don't hold your breath.

Andrés

Mark Needham

unread,
May 8, 2012, 4:49:03 PM5/8/12
to ne...@googlegroups.com
Fair enough. How would I do the above query using the REST API? http://docs.neo4j.org/chunked/snapshot/rest-api-graph-algos.html

I can't quite figure out how to write the traversal to do that. I tried this using neography:

@neo.get_paths(node1, node2, { "type" => "worked_on", "direction" => "out" }, depth = 3, algorithm = "shortestPath")

But that doesn't seem to work the same way as the query I had using cypher...

Mark Needham

unread,
May 8, 2012, 4:58:29 PM5/8/12
to ne...@googlegroups.com
I think I might have got it myself - realised a mistake I'd made.

Needed to traverse in both directions and to a greater depth to get it working:

@neo.get_paths(node1, node2, { "type" => "worked_on", "direction" => "all" }, depth = 5, algorithm = "shortestPath")

Guillaume Royer

unread,
Jul 11, 2013, 6:03:50 AM7/11/13
to ne...@googlegroups.com
I just stumbled upon this as I was trying to do the same thing, has it been implemented since 1.8GA?

Wes Freeman

unread,
Jul 11, 2013, 11:13:44 AM7/11/13
to ne...@googlegroups.com
No, but hopefully it will be in for 2.0.GA (it's not in M03).

Wes

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Wes Freeman

unread,
Jul 11, 2013, 11:51:44 AM7/11/13
to ne...@googlegroups.com
As an alternative you can do something with a regular match, where for your limits, and reduce. I didn't realize the original post was so old (since before reduce). Reduce won't be as efficient as the new shortest path syntax, but at least it's possible to express your query (and back in may last year it wasn't).


Wes
Reply all
Reply to author
Forward
0 new messages