Neo4j subgraph of different nodes with different labels and relationship if any

I recently started using neo4j and its query language "cypher" for working with building/metering data.

My current graph database consists of different nodes (with different labels such as: point,meter,elec,equip ..etc. just to name a few), and each having different properties (not relevant in this context).

What I would like to do, is to get a sub-graph of different nodes which have different labels. For instance I would like to get all the nodes labeled as "point" as well as the ones labeled "equip" and the ones labeled as "meter". To do so I tried the following query:

MATCH (p:point)
MATCH (e:equip)
MATCH (m:meter)
RETURN p, e, m

However that does not work since: This query builds a cartesian product between disconnected patterns.

I am trying to get these so that, if a node labeled "point" is connected to either an "equip" or "meter" node, I would get the relationship. If nothing is connected to the "point" node, it would just be stand alone. Therefore I could have one subgraph with the "point" to "meter"/"equip" connections and visually identify the isolated "point".

I also tried something like:

MATCH (p:point)--(e:equip)

But that only return the "point" nodes which are somewhat connected to an "equip" node. Not giving me the isolated nodes labeled "point" as well.

Looking forward to your input on this (simple case I guess).


Hello @thibaut.richert and welcome to the Neo4j community :slight_smile:

OPTIONAL MATCH (p:point)--(e:equip)


Thank you very much, that solved my issue :)

1 Like

If you are interested only in isolated points then you can also try

MATCH (p:point)
WHERE size((p)-->())=0 AND size((p)<--())=0 

This will return only isolated points.