使用Cypher重建与相关实体的图表

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

Rebuild Graph by Related Entities in Cypher

问题

抱歉,我只提供中文翻译,以下是您要求的内容的中文翻译:

我是Cypher初学者。我尝试通过Google搜索我的问题,但找不到答案。

假设node-1svc-1之间存在关系,node-2svc-2之间存在关系,以及node-2node-1之间存在关系。

是否可以创建一个查询,以查找node-1node-2之间的路径,以便在svc-1svc-2之间创建关系?

例如,我们有以下数据:

[
    {
        "spanId": "3",
        "name": "checkOut",
        "parentId": "2",
        "service_name": "svc-2"
    },
    {
        "spanId": "4",
        "name": "payment",
        "parentId": "3",
        "service_name": "svc-2"
    },
    {
        "spanId": "2",
        "name": "addCart",
        "parentId": "1",
        "service_name": "svc-1"
    },
    {
        "spanId": "1",
        "name": "frontend",
        "service_name": "svc-1"
    }
]

我们有:

  1. frontend -> addCart 适用于 svc-1
  2. checkOut -> payment 适用于 svc-2
  3. addCart -> checkOut

从跨度视图来看,我们有路径 frontend -> addCart -> checkOut -> payment,但从服务视图来看,我希望有 svc-1 -> svc-2 并仅显示它。是否有Cypher查询可以实现这个目标?

英文:

I am a Cypher beginner. I tried to google my issue but could not find an answer.

Assume node-1 has a relationship to svc-1, node-2 has a relationship to svc-2, and node-2 has a relationship to node-1.

Is it possible to make a query that looks for a path between node-1 and node-2 in order to create a relationship between svc-1 and svc-2?

For example, we have the following data

[
    {
        "spanId":"3",
        "name": "checkOut",
        "parentId": "2",
        "service_name": "svc-2"
    },
    {
        "spanId":"4",
        "name": "payment",
        "parentId": "3",
        "service_name": "svc-2"
    },
    {
        "spanId":"2",
        "name": "addCart",
        "parentId": "1",
        "service_name": "svc-1"
    },
    {
        "spanId":"1",
        "name": "frontend",
        "service_name": "svc-1"
    }
]

we have

  1. frontend -> addCart for svc-1
  2. checkOut -> payment for svc-2
  3. addCart -> checkOut.

使用Cypher重建与相关实体的图表

From the span view, we have the path frontend -> addCart -> checkOut -> payment, but from service, I wish to have svc-1 -> svc-2 and only show it. Is there cypher query to make it?

答案1

得分: 4

如果您只想查看从 node-1node-2 存在 PARENT_OF 路径时的 svc-1svc-2 的名称:

MATCH (s1:Service)<-[:FROM_SERVICE]-(n1:Node)-[:PARENT_OF*..7]->(n2:Node)-[:FROM_SERVICE]->(s2:Service)
WHERE n1.name = 'frontend' AND n2.name = 'payment'
RETURN s1.name AS s1_name, s2.name AS s2_name

我将变量长度路径搜索限制为了 7,以避免查询耗时或内存不足(如果您有大量数据)。

我建议不要实际创建 svc-1svc-2 之间的新关系,因为这将存储冗余信息,而且可能会导致混淆(因为不是所有可能的 PARENT_OF 路径都会具有相同的服务节点对)。

英文:

If you actually just want to see the names of svc-1 and svc-2 if a PARENT_OF path exists from node-1 to node-2:

MATCH (s1:Service)&lt;-[:FROM_SERVICE]-(n1:Node)-[:PARENT_OF*..7]-&gt;(n2:Node)-[:FROM_SERVICE]-&gt;(s2:Service)
WHERE n1.name = &#39;frontend&#39; AND n2.name = &#39;payment&#39;
RETURN s1.name AS s1_name, s2.name AS s2_name

I limited the variable-length path search to a somewhat-arbitray depth of 7, to avoid the query taking too long or running out of memory (if you had a lot of data).

I would advise against actually creating a new relationship between svc-1 and svc-2 because that would be storing redundant information AND it may actually be confusing (since not all possible PARENT_OF paths would have the same pair of service nodes).

huangapple
  • 本文由 发表于 2023年4月7日 00:47:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75951904.html
匿名

发表评论

匿名网友

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

确定