Multiple relationship of same type between two nodes

3,819 views
Skip to first unread message

Bikash Jain

unread,
May 15, 2012, 2:05:59 PM5/15/12
to Neo4j
Hi,

I am working on a small POC project to benchmark neo4j graph DB
against our current mysql implementation of the same feature.


We have our database model in such a way, where we will have to create
multiple relationship of same type (but different properties) between
two node.

However during node traversal, we want to retrive all the relationship
between those 2 nodes (currently I see the traversal framework return
only one relationship and not all. I might be wrong. ) But couldn't
find how to do that using either GraphAlgoFactory or Traversal
frameWork.


I am using, neo4j in JAVA and is embedded in our tomcat server.
(adding neo4j jar in tomcat).

We also want to do that upto a depth of 4 and between two consecutive
node, we need all the relationships.

Let me know if I am not clear on this.

Thanks,
Bikash

Peter Neubauer

unread,
May 15, 2012, 3:16:01 PM5/15/12
to ne...@googlegroups.com
Bikash,
you can do a Cypher query which will give you unique relationships, e.g.

http://tinyurl.com/cmk9bby where there are multiple relationships
between node 1 and 2 (see the setup)

start a = node(1) match p=a-[r:related]->()-->() return p, r

for java, see http://docs.neo4j.org/chunked/snapshot/tutorials-cypher-java.html
for executing it from Java.

Does that do what you want?

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j

Bikash Jain

unread,
May 15, 2012, 3:25:33 PM5/15/12
to ne...@googlegroups.com
Thanks Peter for the quick response.

In the mean time I found out that GraphAlgoFactory.allSimplePaths actually returns all the relationship between two nodes.

Will Cypher query be faster compared to the above mentioned approach? I am actually not much familiar with cypher queries. So will try get a hang of it in the mean time.

I am also looking for a way to hook up my filtering logic in allSimplePaths api. Doesn't look like I can filter out anything except based on RelationshipType and Direction. Let me know if I am missing something obvious here.


Thanks a lot for your response.
--
Thanks,
Bikash
9948439165

Michael Hunger

unread,
May 15, 2012, 4:53:37 PM5/15/12
to ne...@googlegroups.com
Right now the GraphAlgoFactory.allSimplePaths is probably still faster as it has direct access to the low level API.

If you want to use cypher please make sure to reuse the execution-engine and use parameters for eventual start and end-nodes.

Michael

But feel free to test it for your case and please report back to us.

Thanks a lot

Michael

Bikash Jain

unread,
May 16, 2012, 1:32:23 AM5/16/12
to ne...@googlegroups.com
Thanks Michael for the information.

allSimplePaths looks impressive in the initial testing I have done. I will do a load testing probably at a later stage.

However I have few small requirements.

1. How can I traverse all path (all relationship between nodes) from a starting node and with a specified depth? 
    allSimplePath is meant for a pair of node. But I want to find all nodes from a given starting node with all relationship between all the nodes.

2. Is there a way to pluggin filtering logic in allSimplePaths api apart from RelationShip Type and direction. 
    Something similar to what we have in our Traverser frameWork.

Thanks a lot for your time.


--
Thanks,
Bikash
9948439165

Mattias Persson

unread,
May 16, 2012, 8:38:52 AM5/16/12
to ne...@googlegroups.com
This has to do with the default (NODE_GLOBAL) uniqueness of a traversal. Try setting it to RELATIONSHIP_GLOBAL or NODE_PATH, like:

  Traversal.description().uniqueness( Uniqueness.RELATIONSHIP_GLOBAL )....

2012/5/15 Bikash Jain <jainb...@gmail.com>



--
Mattias Persson, [mat...@neotechnology.com]
Hacker, Neo Technology
www.neotechnology.com

Bikash Jain

unread,
May 17, 2012, 9:39:04 AM5/17/12
to ne...@googlegroups.com
Thanks Mattias,

That worked :)
--
Thanks,
Bikash
9948439165

Harshal Vora

unread,
Jan 22, 2014, 12:14:55 PM1/22/14
to ne...@googlegroups.com
Hi Peter,

What is the difference between
start a = node(26) match p=a-[r:COORD_DEFINED_BY]-() return r ;

AND

start a = node(26) match p=a-[r:COORD_DEFINED_BY]-()--() return r


Why does the first query return only one row while the second query returns 13 rows? All same.

Also, I have stored birthTime and deathTime with each node and each relation.
When I try to store two different relations between two same nodes with different birthtime/deathtime it does not seem to persist. Is this even possible i.e. to store same relation with different attribute values multiple times between two same nodes?




Below are the results
start a = node(26) match p=a-[r:COORD_DEFINED_BY]-() return r ;   
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| r                                                                                                                                                                                                                                                                                      |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+



start a = node(26) match p=a-[r:COORD_DEFINED_BY]-()--() return r ;
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| r                                                                                                                                                                                                                                                                                      |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
| :COORD_DEFINED_BY[37] {__type__:"GeometryContainer",birthTimeMillis:1356998400000,geometry:"POLYGON ((0 3.35, 8.61 3.35, 8.61 6.7, 0 6.7, 0 3.35))",deathTime:"1420070400000",birthTime:"1356998400000",uniqueId:"ba2e816b-4697-427e-aa2a-f32fe8407ce9",deathTimeMillis:1420070400000} |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
13 rows
3 ms


Regards,

Harshal Vora

unread,
Jan 23, 2014, 1:02:53 AM1/23/14
to ne...@googlegroups.com
Ok, I figured the difference between the two queries and why the second one returns so many results.

But, I still need to know if it is possible to store the same relation multiple times between two same nodes with different attributes for the relation?

Regards,

Michael Hunger

unread,
Jan 23, 2014, 2:49:00 AM1/23/14
to ne...@googlegroups.com
Yep thats perfectly fine

Sent from mobile device
--
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.

Harshal Vora

unread,
Jan 24, 2014, 12:24:14 AM1/24/14
to ne...@googlegroups.com
Ok. I figured that we need to specifically allow duplicate relations and a few alternate ways to fetch all the relations.
I am using spring SDN. The API's are not very intuitive.

Regards,
Reply all
Reply to author
Forward
0 new messages