Check if a relationship between two nodes exists

5,725 views
Skip to first unread message

Martyzz1

unread,
Apr 22, 2012, 5:27:03 PM4/22/12
to ne...@googlegroups.com
I would like to run Cypher queries against neo4j via a REST client.... Can anybody recommend a client?

Or

alternatively, is there a way using the REST api to check if a Relationship between 2 nodes exists?

e.g. do this through the rest interface only?
START n1=node(1), n2=node(2)
MATCH n1-[:KNOWS]->n2
RETURN count(*)

Regards

Martin

Michael Hunger

unread,
Apr 22, 2012, 5:58:08 PM4/22/12
to ne...@googlegroups.com
Martin,

what language are you using?

besides cypher you can also look into a traversal/shortest-path starting at n1 and ending at n2 with a max_depth of 1.

But cypher should work fine.

Please be aware that if there is no relationship between the nodes an empty result will be returned.

Michael

Martin Moss

unread,
Apr 22, 2012, 6:14:42 PM4/22/12
to ne...@googlegroups.com
ah my bad,   I'm using python... I have code in php using neo4jphp which does this, but I now have a need to do the same thing using python - I've been looking at py2neo, which seems to have the fullest feature set.

Javier de la Rosa

unread,
Apr 22, 2012, 6:44:42 PM4/22/12
to ne...@googlegroups.com
On Sun, Apr 22, 2012 at 18:14, Martin Moss <mart...@gmail.com> wrote:
> ah my bad,   I'm using python... I have code in php using neo4jphp which
> does this, but I now have a need to do the same thing using python - I've
> been looking at py2neo, which seems to have the fullest feature set.

For Gremlin and Cypher queries you can also use neo4jrestclient:

>>> from neo4jrestclient.client import GraphDatabase

>>> gdb = GraphDatabase("http://localhost:7474/db/data")

>>> query = """START n1=node(1), n2=node(2)
.. MATCH n1-[:KNOWS]->n2
.. RETURN count(*)"""

>>> cypher = gdb.extensions.CypherPlugin.execute_query

>>> relationships = cypher(query=query)

Or the same using Gremlin:

>>> script = "g.v(1).bothE().filter{it.outVertex == g.v(2) || it.inVertex == g.v(2)}"

>>> gremlin = gdb.extensions.GremlinPlugin.execute_script

>>> relationships = gremlin(script=script)

Or just returning the data as raw:

>>> relationships = gremlin(script=script, returns=client.constants.RAW)

And another less efficient way is just a comprehesion list like this one:

>>> relationships = [r for r in n2.relationships.all(type=["KNOWS"]).all() if n2 == r.end]


Best regards.


>
>
> On 22 April 2012 22:58, Michael Hunger <michael...@neotechnology.com>
> wrote:
>>
>> Martin,
>>
>> what language are you using?
>>
>> besides cypher you can also look into a traversal/shortest-path starting
>> at n1 and ending at n2 with a max_depth of 1.
>>
>> But cypher should work fine.
>>
>> Please be aware that if there is no relationship between the nodes an
>> empty result will be returned.
>>
>> Michael
>>
>> Am 22.04.2012 um 23:27 schrieb Martyzz1:
>>
>> > I would like to run Cypher queries against neo4j via a REST client....
>> > Can anybody recommend a client?
>> >
>> > Or
>> >
>> > alternatively, is there a way using the REST api to check if a
>> > Relationship between 2 nodes exists?
>> >
>> > e.g. do this through the rest interface only?
>> > START n1=node(1), n2=node(2)
>> > MATCH n1-[:KNOWS]->n2
>> > RETURN count(*)
>> >
>> > Regards
>> >
>> > Martin
>>
>

--
Javier de la Rosa
http://versae.es

Nigel Small

unread,
Apr 22, 2012, 6:48:07 PM4/22/12
to ne...@googlegroups.com
Hi Martin

The best way to check for a rel between two nodes would be to use Node.get_related_nodes:

http://py2neo.org/doc/py2neo.html#py2neo.neo4j.Node.get_related_nodes

You could iterate through the results using something like:

def are_related(node_a, node_b, direction, type):
    for node in node_a.get_related_nodes(direction, type):
        if node == node_b:
            return True
    return False


Nige

Michael Hunger

unread,
Apr 22, 2012, 6:57:45 PM4/22/12
to ne...@googlegroups.com
How does it work under the hood? Does it use a traverser?

Cheers

Michael

Martin Moss

unread,
Apr 22, 2012, 7:02:42 PM4/22/12
to ne...@googlegroups.com

Thanks everyone. I'm expecting hundreds of thousands of relationships... On both nodes.. would this have an impact?

Nigel Small

unread,
Apr 22, 2012, 7:06:50 PM4/22/12
to ne...@googlegroups.com
It uses http://docs.neo4j.org/chunked/stable/rest-api-relationships.html#rest-api-get-all-relationships so yes it could have an impact as it would have to grab all those relationships first, before checking whether the one you are interested in exists.

In that case, it might be better to throw the Cypher query at cypher.execute (http://py2neo.org/doc/py2neo.html#py2neo.cypher.execute).

Nige

Martin Moss

unread,
Apr 22, 2012, 7:10:13 PM4/22/12
to ne...@googlegroups.com

Ok great.  This then puts me back to the situation of retrieving a relationship by the results of the cypher query, so I can delete it, or update its properties.....

That's the bit I'm stuck on in py2neo..

Michael Hunger

unread,
Apr 22, 2012, 7:35:21 PM4/22/12
to ne...@googlegroups.com
If you know which of the nodes has the lower number of rels, you should probably do this:

where n1 is the node with the fewer rels.

start n1=node(1),n2=node(2) 
where n1-[:KNOWS]->n2
return count(*)

so you don't have to employ the pattern matcher just check the relationship-existence.

if you need to return the relationship you have to use the matcher

start n1=node(1),n2=node(2) 
match n1-[r:KNOWS]->n2
return r

Michael

Nigel Small

unread,
Apr 22, 2012, 8:21:06 PM4/22/12
to ne...@googlegroups.com
Hi Martin

In order to run the Cypher query that Michael recommends with py2neo, you will need to use code similar to:

from py2neo import neo4j, cypher
graphdb = neo4j.GraphDatabaseService("http://localhost:7474/db/data/")
rows, columns = cypher.execute("
start n1=node(1),n2=node(2) where n1-[:KNOWS]->n2 return count(*)", graphdb)
if rows:
    # rel exists...
else:
    # rel doesn't exist

I have just pushed an update to GitHub which resolves Cypher return values into proper Node/Relationship objects where appropriate. The "rows" part of the tuple returned from cypher.execute is a list of lists. The outer list represents the rows and the inner list represents the values within that row. Each value can be a Node object, a Relationship object or a simple property.

Hope this helps

Nige

Martin Moss

unread,
Apr 23, 2012, 2:21:19 AM4/23/12
to ne...@googlegroups.com

That's awesome! I'll check it out tonight and let you know how it goes..  thanks everyone.

Nigel Small

unread,
Apr 23, 2012, 4:21:23 AM4/23/12
to ne...@googlegroups.com
As it seemed like a useful function to have, I've added a built-in version to the project itself (using the Cypher query method):

https://github.com/nigelsmall/py2neo/blob/master/src/py2neo/neo4j.py#L562

It's not released through PyPI yet but will be there if you grab the code directly from GitHub

Nige

James Thornton

unread,
Apr 23, 2012, 4:39:14 AM4/23/12
to ne...@googlegroups.com

You can use a Gremlin script in Bulbs (http://bulbflow.com/) to find a count of all the relationships between nodes "a" and "b"...

>>> from bulbs.neo4jserver import Graph
>>> g = Graph()
>>> script = "g.v(a).bothE().filter{it.outVertex == g.v(b) || it.inVertex == g.v(b)}.count()"
>>> params = dict(a=1, b=2)
>>> count = g.gremlin.command(script, params)
>>> if count: print True

Here's an overview of the Gremlin methods in Bulbs:


And here's the source code:


- James

Martin Moss

unread,
May 11, 2012, 5:46:31 AM5/11/12
to ne...@googlegroups.com
Nige,

just wanted to say a big thank you.... I finally had time to play with the new features.... Makes my life easier...

I was wondering,   if you might want to join together the two features in some way and add a  

node.get_relationships_to(node2, Direction.OUTGOIING, 'KNOWS')

This would, to me, fit naturally with the has_relationship_to and the new cypher Relationship instantiation feature you added...

Just a suggestion....

Regards

Martin

Nigel Small

unread,
May 11, 2012, 7:32:28 AM5/11/12
to ne...@googlegroups.com
Hi Martin

Glad to help :-)

That seems like a good idea - would you mind putting a request into the GitHub issues list?

Cheers
Nige

Reply all
Reply to author
Forward
0 new messages