Find nodes and their associated subgraphs with Neo4j + Cypher

I am using Neo4j 2.1.7 and Node.js to create a REST API. The data - about 70,000 nodes and 100,000 relationships - contains a lot of small related subgraphs.

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

Illustration:

Connected components

(Image from Wikipedia )

I can get all the data I need with this request:

 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 simply return all triples (a)-[r]-(b) (not grouped by component / subgraph). Of course, I could restore the graph in Node.js and find the subgraphs myself, but this does not seem like the best solution. Is it possible to group the returned data in an array / collection of subgraphs / components? Which Cypher queries will best suit my use case? Or should I use the Neo4j API instead?

Thanks! Bert

+5
source share
2 answers

You should still have a starting point as a 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 
+3
source
 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 up 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.

* It should be noted that I do not know about the performance of this method.

+1
source

Source: https://habr.com/ru/post/1213992/


All Articles