Rod0n
Rod0n

Reputation: 1079

Grouping nodes by their relationship

I've built a simple graph of one node type and two relationship types: IS and ISNOT. IS relationships means that the node pair belongs to the same group, and obviouslly ISNOT represents the not belonging rel.

When I have to get the groups of related nodes I run the following query:

"MATCH (a:Item)-[r1:IS*1..20]-(b:Item) RETURN a,b"

So this returns a lot of a is b results and I added some code to group them after.

What I'd like is to group them modifying the query above, but given my rookie level I haven't yet figured it out. What I'd like is to get one row per group like:

(node1, node3, node5)
(node2,node4,node6)
(node7,node8)

Upvotes: 1

Views: 583

Answers (1)

Christophe Willemsen
Christophe Willemsen

Reputation: 20175

I assume what you call groups are nodes present in a path where all these nodes are connected with a :IS relationship.

I think this query is what you want :

MATCH p=(a:Item)-[r1:IS*1..20]-(b:Item)
RETURN nodes(p) as nodes

Where p is a path describing your pattern, then you return all the nodes present in the path in a collection.

Note that, a simple graph (http://console.neo4j.org/r/ukblc0) :

(item1)-[:IS]-(item2)-[:IS]-(item3)

will return already 6 paths, because you use undericted relationships in the pattern, so there are two possible paths between item1 and item2 for eg.

Upvotes: 2

Related Questions