cancel
Showing results for 
Search instead for 
Did you mean: 

How to filter virtual nodes/relationships after apoc.meta.graph() call?

koen_smets
Node

I'd like to filter some of the nodes (or relationships) from the meta graph.

If I just call apoc.meta.graph(), the resulting nodes (and relationships) seem to have properties:

"nodes": [
{
  "identity": -1826,
  "labels": [
    "A"
  ],
  "properties": {
"name": "A",
"count": 21
  }
}
...
]

However, the following query fails to select the nodes without name "A", but results in no nodes at all.

call apoc.meta.graph()
yield nodes, relationships
with [x in nodes | x.name <> "A"] as nodes, relationships
return nodes, relationships

I also tried x.properties.name or x["properties]["name"], but all result in no nodes at all. So it seems that these virtual nodes don't seem to have any properties.

Any ideas on how to properly filter nodes (or relationships) from the meta graph?

Thx, Koen

1 ACCEPTED SOLUTION

giuseppe_villan
Graph Fellow

@koen.smets
The apoc.meta.graph returns a virtual graph, therefore the "classic" neo4j methods don't work.
You have to use the apoc.any.properties or the apoc.any.property.
See here: apoc.any - APOC Documentation

For example:

call apoc.meta.graph()
yield nodes, relationships
with [x in nodes where apoc.any.properties(x).name <> "A"] as nodes, relationships
return nodes, relationships

or, filtering both nodes and rels:

call apoc.meta.graph()
yield nodes, relationships
with [x in nodes where apoc.any.property(x, 'name') <> "A"] as nodes, [x in relationships where apoc.any.property(x, 'type') <> "E"] as relationships
return nodes, relationships

View solution in original post

2 REPLIES 2

giuseppe_villan
Graph Fellow

@koen.smets
The apoc.meta.graph returns a virtual graph, therefore the "classic" neo4j methods don't work.
You have to use the apoc.any.properties or the apoc.any.property.
See here: apoc.any - APOC Documentation

For example:

call apoc.meta.graph()
yield nodes, relationships
with [x in nodes where apoc.any.properties(x).name <> "A"] as nodes, relationships
return nodes, relationships

or, filtering both nodes and rels:

call apoc.meta.graph()
yield nodes, relationships
with [x in nodes where apoc.any.property(x, 'name') <> "A"] as nodes, [x in relationships where apoc.any.property(x, 'type') <> "E"] as relationships
return nodes, relationships

Thanks @giuseppe.villani for pointing this out.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.