i created a category-structure in graph-database "neo4j". I have nodes and relationships, everything perfect.
I am using Neoxygen Neoclient for PHP to access the data. How can I query the whole category-graph in an efficient way (including structure) from my root-element?
MATCH (a:`Category`{category_id:0})-[r:HAS_CHILD*]->(b:`Category`)
RETURN b,r
My desired structure in PHP is: - Root --- Category A -------- Subcategory AB --- Category B --- Category C -------- Subcategory CA ----------------- Subsubcategory CAA ...
Any ideas?
Thanks in advance.
mr_g
Cypher returns tabular data so if you want to get a tree hierarchy the most efficient way is to return all of the paths from the root to the leaves. A path is a collection/array of node-(relationship-node)* (that is it's an odd number of objects, always containing a node at each end with alternating nodes and relationships). Here is the cypher for how you would do that:
The
WHERE
clause ensures that you only return all of the paths to the leafs. You could return all categories in the tree which would give you the partial paths too, but all of those partial paths are contained in some longer path so you'd just end up returning more data than you need.Once you have the paths (I'm not sure what form they show up in Neoclient as I'm not a PHP guy) you can build a hierarchical data structure in memory from the results. If I recall correctly the map/dictionary-type structure in PHP is an associative array.