Check if a relationship of a given TYPE exists between two nodes.

785 views
Skip to first unread message

Kamesh Marellapudi

unread,
Mar 7, 2012, 5:28:29 PM3/7/12
to ne...@googlegroups.com
Hi Guys, 
     I am new to Neo4j and am trying to get my feet wet here. Wondering if there is an existing API to evaluate if there is a relationship between two nodes of the given TYPE. For example A & B nodes have a relationship of TYPE,  'FRIENDS'. 

I did see some posts where they are trying to achieve this with relationship index but that solution is for finding a relationship between two nodes with the given property (key of the "key/value" pair) but relationship type is not modeled as a property on the relationship, right? Nor I would prefer to duplicate the relationship type as a property on the relationship either. I don't think looping through all the relationships between two nodes is an efficient solution either. 


Thanks in advance,

Kamesh




Peter Neubauer

unread,
Mar 9, 2012, 6:11:10 AM3/9/12
to ne...@googlegroups.com
Kamesh,
how many rels are attached to the nodes you are searching between, and
are these symmetric on both nodes?

Cheers,

/peter neubauer

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

Neo4j 1.6 released                 - dzone.com/6S4K
The Neo4j Heroku Challenge   - http://neo4j-challenge.herokuapp.com/

Kamesh Marellapudi

unread,
Mar 12, 2012, 5:37:33 PM3/12/12
to ne...@googlegroups.com
There could be more than one relationship between these nodes but not many. The relationship is uni-directional.

Thanks
Kamesh

Peter Neubauer

unread,
Mar 27, 2012, 10:13:09 AM3/27/12
to ne...@googlegroups.com
Then,
I would just do a little utility function

isRelated(Node a, Node b, RelationshipType type) {
rels = a.getRelationships();
for(Relationships r : rels) {
if (r.getType().equals(type){
return true;
}
}
return false;

}

This is NOT compiling :)

Cheers,

/peter neubauer

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

Neo4j                                - Graphs rule.
Program or be programmed - Computer Literacy for kids.
http://foocafe.org/#CoderDojo

On Mon, Mar 12, 2012 at 10:37 PM, Kamesh Marellapudi

Michael Hunger

unread,
Mar 27, 2012, 11:23:46 AM3/27/12
to ne...@googlegroups.com
And of course Peter missed the check :)

> public boolean isRelated(Node a, Node b, RelationshipType type) {
> for(Relationship r :a.getRelationships(type)) {
> if (r.getOtherNode(a).equals(b)) return true;
> }
> return false;

Peter Neubauer

unread,
Mar 27, 2012, 1:56:31 PM3/27/12
to ne...@googlegroups.com

We can have a java phone coding contest with the source code branched in mail threads. GIT and CI is for wimps.

Reply all
Reply to author
Forward
0 new messages