cancel
Showing results for 
Search instead for 
Did you mean: 

Join the community at Nodes 2022, our free virtual event on November 16 - 17.

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)
RETURN p,e

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).

Best!

1 ACCEPTED SOLUTION

Cobra
Ninja
Ninja

Hello @thibaut.richert and welcome to the Neo4j community

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

Regards,
Cobra

View solution in original post

3 REPLIES 3

Cobra
Ninja
Ninja

Hello @thibaut.richert and welcome to the Neo4j community

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

Regards,
Cobra

Thank you very much, that solved my issue 🙂

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

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

This will return only isolated points.