douba05167 2015-06-03 20:51
浏览 129

Neo4J,如何查询分层数据/ PHP

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

  • 写回答

3条回答 默认 最新

  • dpqg86714 2015-06-03 21:25
    关注

    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:

    MATCH path(a:`Category`{category_id:0})-[r:HAS_CHILD*]->(b:`Category`)
    WHERE NOT(b-[:HAS_CHILD]->())
    RETURN b,r
    

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统
  • ¥188 需要修改一个工具,懂得汇编的人来。
  • ¥15 livecharts wpf piechart 属性
  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题