Trying to get all nodes connected to a given node with the specified relationship (REST API)

3,015 views
Skip to first unread message

MMac

unread,
Dec 26, 2011, 2:03:42 AM12/26/11
to Neo4j
I have was I assume would be fairly simple. I'd like to get all
distinct nodes connected to a given node with a specific relationship.
I need to account for circular paths. Is this possible with the rest
api? Thanks!

Marc

Peter Neubauer

unread,
Dec 26, 2011, 3:30:17 AM12/26/11
to ne...@googlegroups.com
Hi Marc,
The easiest way is probably using Cypher at /db/data/cypher, see the REST documentation .

This would be something like

Start n = node(1) match n-[:myrel]->x return n,x

/peter
--
Sent from Gmail Mobile

James Thornton

unread,
Dec 26, 2011, 3:43:55 AM12/26/11
to ne...@googlegroups.com
Hi Marc -

You could use the Gremlin plugin (http://docs.neo4j.org/chunked/1.4/gremlin-plugin.html) to execute the script:

id = 1;
label = "knows";
g.v(id).bothE(label).inV().uniqueObject();

In Gremlin:

- "nodes" are called "vertices"
- "relationships" are called "edges".
- "start" vertex is outV (out vertex)
- "end" vertex is inV (in vertex)
- relationship "type" is called "label"

So this script returns the vertices that are adjacent to vertex 1, via the outgoing and incoming edges labeled "knows".

If you only want to use the outgoing edges, you could to this:

g.v(id).outE(label).inV().uniqueObject();

Or incoming...

g.v(id).inE(label).inV().uniqueObject();

And you could simplify the script by combing bothE(label).inV() into both(label):

g.v(id).both(label).uniqueObject();

Example request...

BODY '{"script"="g.v(id).out(label).uniqueObject()","params":{"id":1,"label":"knows"}}'

Here are more details on using the Gremlin scripting API:
https://groups.google.com/d/msg/neo4j/rF1WdaMo05Q/k-4mS58vlHoJ

- Jams

MMac

unread,
Dec 26, 2011, 1:03:30 PM12/26/11
to Neo4j
Hi guys, thanks for the responses!

I'm actually using cypher right now, I should have included the
current query:

START n=node(1)
MATCH n-[:pull]->x
RETURN distinct x.id

The issue here is that it only seems to traverse to depth 1. I'd like
to find all nodes connected to the start node via path :pull
(distinct). I tried this:

START n=node(1)
MATCH n-[:pull]->x-[:pull]->x2-[:pull]->x3
RETURN x.id, x2.id, x3.id

I thought maybe the above would go to a depth of 3 (still not what I
ultimately am looking for since I'd like unlimited depth, but it's a
start). I didn't have much success with it though.

Thoughts?

On Dec 26, 3:30 am, Peter Neubauer <peter.neuba...@neotechnology.com>
wrote:
> Hi Marc,
> The easiest way is probably using Cypher at /db/data/cypher, see the REST
> documentation .
>
> This would be something like
>
> Start n = node(1) match n-[:myrel]->x return n,x
>
> /peter
>

Michael Hunger

unread,
Dec 26, 2011, 1:25:43 PM12/26/11
to ne...@googlegroups.com
Try to use arbitrary length paths.

> START n=node(1)
> MATCH n-[:pull*]->x
> RETURN distinct x.id

or for certain limits
> MATCH n-[:pull*start...end]->x
see: http://docs.neo4j.org/chunked/milestone/query-match.html#match-relationship-identifier-in-variable-length-relationships

Cheers

Michael

MMac

unread,
Dec 26, 2011, 1:56:04 PM12/26/11
to Neo4j
Michael,

Wow not sure how I missed that - thanks it works perfectly now! I had
all of this modelled in mongo before, moving to neo4j for much of it
now. Loving neo4j so far.

On Dec 26, 1:25 pm, Michael Hunger <michael.hun...@neotechnology.com>
wrote:
> Try to use arbitrary length paths.
>
> > START n=node(1)
> > MATCH n-[:pull*]->x
> > RETURN distinct x.id
>
> or for certain limits> MATCH n-[:pull*start...end]->x
>
> see:http://docs.neo4j.org/chunked/milestone/query-match.html#match-relati...
Reply all
Reply to author
Forward
0 new messages