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

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

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

MATCH p=(n:Label1 {name:'main'})-[:calls*..2]->(m)
WITH COLLECT(p) AS ps 
CALL apoc.convert.toTree(ps) yield value
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函数生成的嵌套文档,但我认为它可以满足您的需求。

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

示例结果:

[
  {
    "result": {
      "_id": 31,
      "_type": [
        "Label1"
      ],
      "node": {
        "name": "main"
      },
      "relationships": [
        {
          "calls.y": "prop y",
          "_type": "Main",
          "_id": 32
        },
        {
          "_type": "Main",
          "_id": 32,
          "calls.x": "prop x"
        }
      ]
    }
  }
]

希望这对您有帮助。

英文:

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.

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

Sample result:

[
  {
    "result": {
      "_id": 31,
      "_type": [
        "Label1"
      ], 
      "node": {
        "name": "main"
      },
      "relationships": [
        {
          "calls.y": "prop y",
          "_type": "Main",
          "_id": 32
        },
        {
          "_type": "Main",
          "_id": 32,
          "calls.x": "prop x"
        }
      ]
    }
  }
]

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:

确定