Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find nodes and their connected subgraphs with Neo4j + Cypher

I'm using Neo4j 2.1.7 and Node.js to build a REST API. The data - around 70.000 nodes and 100.000 relationships - contains very many small connected subgraphs.

One API call, for example localhost:8000/search?name=Bussum, should return all nodes named Bussum and the connected component they belong to.

Illustration:

Connected components

(Image from Wikipedia)

I can get all the data I need with a query like this:

MATCH (a {name: "Bussum" })-[r*]-(b) 
UNWIND rels AS rel 
RETURN distinct startNode(rel) AS a, type(rel), endNode(rel) AS b

But such a query will just return all triples (a)-[r]-(b) (not grouped per component/subgraph). Of course, I could reconstruct the graph in Node.js and find the subgraphs myself, but this does not at all feel like the best solution. Is it possible to group the returned data in an array/collection of subgraphs/components? Which Cypher queries would match my use case better? Or should I consider using the Neo4j Java API instead?

Thanks! Bert

like image 317
Bert Avatar asked Sep 28 '22 16:09

Bert


2 Answers

You should still have your original start point as grouping node.

MATCH (root {name: "Bussum" })-[rels*]-(b) 
UNWIND rels AS rel 
RETURN root, 
       collect({start: startNode(rel), 
                 type:      type(rel), 
                  end:   endNode(rel)}) as component
like image 191
Michael Hunger Avatar answered Oct 19 '22 18:10

Michael Hunger


MATCH (a {name: "Bossum"})-[*0..]-(b)
WITH DISTINCT a, collect(DISTINCT b) AS sets
RETURN DISTINCT sets

This query will return (possibly) many rows, where each row is a collection of nodes that make a complete subgraph such that each subgraph is as large as possible and contains at least one node named "Bossum". Each row(subgraph) is guaranteed to be unique in the result set.

*I should note, I do not know about the performance of this method.

like image 2
MagnusVortex Avatar answered Oct 19 '22 18:10

MagnusVortex