Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cypher query to return nested data hierarchically

Tags:

tree

neo4j

cypher

(:Dot)-[:CONTAINS]->(:Dot)

Not every dot has a container and also there are no loops. Atm a dot can only be contained by one other dot. So we are basically talking about trees

Now I would like to use Neo4j to retrieve the data and return it hierarchally (by containers):

[
    {"id": 1, "containees": [
        {"id": 3},
        {"id": 4, "containees": [
            {"id": 6},
        ]},
    ]},
    {"id": 2, "containees": [
        {"id": 5}
    ]}
]

The query shouldn't use a fixed depth.

Atm I only have this, and I'm really not sure what my options are concerning iterating a path.

MATCH (d:Dot)
OPTIONAL MATCH p = d-[:CONTAINS*]->(d2:Dot)
RETURN d.id
like image 927
Gregor Weber Avatar asked Jul 21 '26 17:07

Gregor Weber


1 Answers

I'm pretty sure this isn't possible currently with Cypher (somebody please correct me if I'm wrong). Cypher is good at taking subgraph matches and turning them into tables, but it doesn't do recursive-type querying.

For this sort of thing I think you'd need to work through the Java APIs directly. Either by interfacing with Java with your language of choice, or by creating an unmanaged extension to have Neo4j return the data for you in JSON (or whatever format you'd like)

like image 113
Brian Underwood Avatar answered Jul 24 '26 22:07

Brian Underwood



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!