Support for multiple relationship Cypher query.

1,442 views
Skip to first unread message

Neo

unread,
Jun 1, 2012, 7:40:01 AM6/1/12
to ne...@googlegroups.com
In Cypher, we have following to allow multiple relationships

START n=node(3)
MATCH (n)-[:BLOCKS|KNOWS]->(x)
RETURN x

But, this works only in Version 1.8 above. My questions are
- Is there any alternative to execute same query in version 1.7 or below? If not,--
- If, I am executing Cypher in Java on Neo4j embedded graph service, can I include different JAR library files to get the above query support in Neo4j 1.7 itself, if so what jar files?

Thank You
Neo

Michael Hunger

unread,
Jun 1, 2012, 7:51:23 AM6/1/12
to ne...@googlegroups.com
Both could work,

neo4j-cypher-1.8*.jar should work with Neo4j 1.7 too, but no guarantees and graph-matching, scala version should be the same.

An alternative (but less performant) would be to check the types in where.

START n=node(3)
MATCH (n)-[r]->(x)
WHERE type(r)="BLOCKS" OR type(r)="KNOWS"
RETURN x

Cheers

Michael

Neo

unread,
Jun 1, 2012, 8:36:10 AM6/1/12
to ne...@googlegroups.com
Thank you for the quick reply. I tried using JAR file of one version(1.8) in version(1.6), but it didn't work.
I am really sorry, I didn't ask my question correctly.
Along with above feature, Cypher also has support for variable length relationship like following.

[:TYPE*minHops..maxHops]->.

Query

START a=node(3), x=node(2, 4)
MATCH a-[:KNOWS*1..3]->x
RETURN a,x
I was trying to combine these two. (Multiple relationship names, Variable length) in one query.
Query is 

START a=node(3), x=node(2, 4)
MATCH a-[:KNOWS|BLOCKS*1..3]->x
RETURN a,x

Above method worked in 1.8, but not in 1.6 (sorry, not 1.7). How can I reproduce same effect. In this case, assigning relationship
as a variable r results in IterableType error. Please suggest any alternatives. Or anyway to accomplish this in Java in single query

Andres Taylor

unread,
Jun 1, 2012, 9:14:13 AM6/1/12
to ne...@googlegroups.com
Hi there,

This query should work:

start n=node(1) 
match n-[rels*1..3]->m 
where all(r in rels WHERE type(r) = "KNOWS" or type(r) = "LOVES") 
return n,m



HTH,

Andrés

Neo

unread,
Jun 1, 2012, 10:04:22 AM6/1/12
to ne...@googlegroups.com
Thank You so much. That's an incredible demo. I didn't think I could exploit functions so much. And by the way I got features of 1.8.2 of Cypher to work for a database project on 1.6. I have pointed GraphDatabaseService to the graph.db in 1.6 folder, but added 1.8 versions of all JAR libraries, and all of the usability of 1.8 came into 1.6. Please tell me if there are any hidden dangers in doing so. Thanks again for pointing out the alternatives!

George Fletcher

unread,
May 7, 2015, 4:16:04 PM5/7/15
to ne...@googlegroups.com
Hi all,

I have a follow up question.  Is it possible to extend this to allow variable length matching over multiple *paths*?  By a path I mean something like two KNOWS traversals or three BLOCKS traversals.  For example, I'd like to be able to write something like:

MATCH (s)-[r*]->(t) 
WHERE ALL (x in type(r) WHERE x=KNOWS/KNOWS OR  x= BLOCKS/BLOCKS/BLOCKS)
RETURN s, t

where by "KNOWS/KNOWS" I mean something like (a)-[:KNOWS]->(b)-[:KNOWS]->(c).

I hope this makes sense.

Thanks,
George
...
Reply all
Reply to author
Forward
0 new messages