Neo4j apoc.convert.toTree 不显示具有唯一关系的重复子项

huangapple go评论77阅读模式
英文:

Neo4j apoc.convert.toTree is not showing duplicate children with unique relationship

问题

I have two nodes A and B (A->B) connected via two relationships. Those 2 relationships are unique. For example, you can think of the two rels as two paths from A to B at two different times. Converting them to one relationship is not viable at this time.

For displaying in UI, query result should be a tree structure. Used following query to get the tree

the resulting tree is

Preferred result is

How can I get a tree with duplicate children is more than one relationship exists between same two nodes?

Thanks.

英文:

I have two nodes A and B (A->B) connected via two relationships. Those 2 relationships are unique. For example, you can think of the two rels as two paths from A to B at two different times. Converting them to one relationship is not viable at this time.
Neo4j apoc.convert.toTree 不显示具有唯一关系的重复子项

For displaying in UI, query result should be a tree structure. Used following query to get the tree

  1. MATCH p=(n:Label1 {name:'main'})-[:calls*..2]->(m)
  2. WITH COLLECT(p) AS ps
  3. CALL apoc.convert.toTree(ps) yield value
  4. RETURN value;

the resulting tree is

Neo4j apoc.convert.toTree 不显示具有唯一关系的重复子项

Preferred result is

Neo4j apoc.convert.toTree 不显示具有唯一关系的重复子项

How can I get a tree with duplicate children is more than one relationship exists between same two nodes?

Thanks.

答案1

得分: 0

我在这个SO问题上提供了一个解决方法。虽然它不完全等同于apoc函数生成的嵌套文档,但我认为它可以满足您的需求。

  1. MATCH p=(n:Label1 {name:'main'})-[:calls*..2]->(m)
  2. // 每个路径都将被返回
  3. WITH COLLECT(p) AS ps, p, n
  4. CALL apoc.convert.toTree(ps) YIELD value
  5. // 作为嵌套文档返回节点n及其关系
  6. RETURN {_id: ID(n), _type: labels(n), node:n, relationships: COLLECT(value['calls'][0])} AS result

示例结果:

  1. [
  2. {
  3. "result": {
  4. "_id": 31,
  5. "_type": [
  6. "Label1"
  7. ],
  8. "node": {
  9. "name": "main"
  10. },
  11. "relationships": [
  12. {
  13. "calls.y": "prop y",
  14. "_type": "Main",
  15. "_id": 32
  16. },
  17. {
  18. "_type": "Main",
  19. "_id": 32,
  20. "calls.x": "prop x"
  21. }
  22. ]
  23. }
  24. }
  25. ]

希望这对您有帮助。

英文:

I offer a hack on this SO question. Although it is not exactly the resulting nested document as the apoc function but I think it can server your purpose.

  1. MATCH p=(n:Label1 {name:'main'})-[:calls*..2]->(m)
  2. // each path will be returned
  3. WITH COLLECT(p) AS ps, p, n
  4. CALL apoc.convert.toTree(ps) yield value
  5. // return node n and it's relationships as a nested document
  6. RETURN {_id: ID(n), _type: labels(n), node:n, relationships: collect(value['calls'][0])} as result

Sample result:

  1. [
  2. {
  3. "result": {
  4. "_id": 31,
  5. "_type": [
  6. "Label1"
  7. ],
  8. "node": {
  9. "name": "main"
  10. },
  11. "relationships": [
  12. {
  13. "calls.y": "prop y",
  14. "_type": "Main",
  15. "_id": 32
  16. },
  17. {
  18. "_type": "Main",
  19. "_id": 32,
  20. "calls.x": "prop x"
  21. }
  22. ]
  23. }
  24. }
  25. ]

huangapple
  • 本文由 发表于 2023年2月6日 13:04:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357491.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定