英文:
Rebuild Graph by Related Entities in Cypher
问题
抱歉,我只提供中文翻译,以下是您要求的内容的中文翻译:
我是Cypher初学者。我尝试通过Google搜索我的问题,但找不到答案。
假设node-1
与svc-1
之间存在关系,node-2
与svc-2
之间存在关系,以及node-2
与node-1
之间存在关系。
是否可以创建一个查询,以查找node-1
和node-2
之间的路径,以便在svc-1
和svc-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"
}
]
我们有:
frontend -> addCart
适用于svc-1
checkOut -> payment
适用于svc-2
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
frontend -> addCart
forsvc-1
checkOut -> payment
forsvc-2
addCart -> checkOut
.
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-1
到 node-2
存在 PARENT_OF
路径时的 svc-1
和 svc-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-1
和 svc-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)<-[: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
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).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论