cypher 触发器序列:具有交替边方向的遍历 (AgensGraph)
cypher flip flop sequence: traversal with alternate edge directions (AgensGraph)
假设,我们使用 2 个顶点标签(女性和男性)和单个边标签(日期)对夫妇进行建模。边缘的方向总是从女性到男性。
预期的查询结果列表是一对,其中从起始顶点到每对顶点的顶点有一条无向路径,不同。
换句话说,结果应包含图的连通分量的边列表,其中存在给定的顶点。
请注意,如果将原始图转换为无向图,可能会有循环。
过滤条件:{ name: 'Adam' }
预期结果集:
Alice-[:dates]->Adam
Alice-[:dates]->Bob
Chloe-[:dates]->Bob
...
Eve-[:dates]->Edgar
Uhura-[:dates]->Spock
不是结果集的一部分,因为 Adam 和(Uhura 或 Spock)之间没有联系。
以下解决方案有效,但性能较差,因此不能用于生产:
match path = ()-[:dates*]-()
where any(node in to_jsonb(nodes(path)) where node.properties.name = 'Adam')
return distinct path;
(或 return distinct edges(path)
,但 AgensBrowser 不喜欢 return 路径的边缘)。
你能帮我提供一些建议以获得更好的解决方案吗?谢谢。
测试数据:
create
(alice: female { name: 'Alice'}),
(barbara: female { name: 'Barbara'}),
(chloe: female { name: 'Chloe'}),
(diane: female { name: 'Diane'}),
(eve: female { name: 'Eve'}),
(uhura: female { name: 'Uhura'}),
(adam: male { name: 'Adam'}),
(bob: male { name: 'Bob'}),
(charles: male { name: 'Charles'}),
(daniel: male { name: 'Daniel'}),
(edgar: male { name: 'Edgar'}),
(spock: male { name: 'Spock'})
create (alice)-[:dates]->(adam),
(alice)-[:dates]->(bob),
(barbara)-[:dates]->(bob),
(barbara)-[:dates]->(charles),
(barbara)-[:dates]->(edgar),
(chloe)-[:dates]->(bob),
(chloe)-[:dates]->(daniel),
(chloe)-[:dates]->(edgar),
(diane)-[:dates]->(edgar),
(eve)-[:dates]->(edgar),
(uhura)-[:dates]->(spock);
我试图在 Agensgraph 中重新执行您的查询,但最后一个匹配查询对我不起作用,所以我无法查看其解释。
这是我为检索您想要的相同结果而进行的查询。
match (f:female)<-[r:dates*]->(m:male{name:'Adam'}) with distinct f
match p = ((f)-[:dates]->(m:male)) return p;
--------------------------------------------------------------------------
[female[73.1]{"name": "Alice"},dates[71.23][73.1,74.1]{},male[74.1]{"name": "Adam"}]
[female[73.1]{"name": "Alice"},dates[71.24][73.1,74.2]{},male[74.2]{"name": "Bob"}]
[female[73.2]{"name": "Barbara"},dates[71.25][73.2,74.2]{},male[74.2]{"name": "Bob"}]
[female[73.2]{"name": "Barbara"},dates[71.26][73.2,74.3]{},male[74.3]{"name": "Charles"}]
[female[73.2]{"name": "Barbara"},dates[71.27][73.2,74.5]{},male[74.5]{"name": "Edgar"}]
[female[73.3]{"name": "Chloe"},dates[71.28][73.3,74.2]{},male[74.2]{"name": "Bob"}]
[female[73.3]{"name": "Chloe"},dates[71.29][73.3,74.4]{},male[74.4]{"name": "Daniel"}]
[female[73.3]{"name": "Chloe"},dates[71.30][73.3,74.5]{},male[74.5]{"name": "Edgar"}]
[female[73.4]{"name": "Diane"},dates[71.31][73.4,74.5]{},male[74.5]{"name": "Edgar"}]
[female[73.5]{"name": "Eve"},dates[71.32][73.5,74.5]{},male[74.5]{"name": "Edgar"}]
(10 rows)
坦率地说,当数据量很大时,我不太确定上述查询的性能。
请在运行查询后给我反馈。
3 月 25 日编辑
这可以解决您的问题吗?
match p = allshortestpaths( (f:female)<-[r:dates*]->(m:male) )
where any(node in to_jsonb(nodes(p)) where node.properties.name starts with 'Adam' )
return p;
假设,我们使用 2 个顶点标签(女性和男性)和单个边标签(日期)对夫妇进行建模。边缘的方向总是从女性到男性。
预期的查询结果列表是一对,其中从起始顶点到每对顶点的顶点有一条无向路径,不同。
换句话说,结果应包含图的连通分量的边列表,其中存在给定的顶点。
请注意,如果将原始图转换为无向图,可能会有循环。
过滤条件:{ name: 'Adam' }
预期结果集:
Alice-[:dates]->Adam
Alice-[:dates]->Bob
Chloe-[:dates]->Bob
...
Eve-[:dates]->Edgar
Uhura-[:dates]->Spock
不是结果集的一部分,因为 Adam 和(Uhura 或 Spock)之间没有联系。
以下解决方案有效,但性能较差,因此不能用于生产:
match path = ()-[:dates*]-()
where any(node in to_jsonb(nodes(path)) where node.properties.name = 'Adam')
return distinct path;
(或 return distinct edges(path)
,但 AgensBrowser 不喜欢 return 路径的边缘)。
你能帮我提供一些建议以获得更好的解决方案吗?谢谢。
测试数据:
create
(alice: female { name: 'Alice'}),
(barbara: female { name: 'Barbara'}),
(chloe: female { name: 'Chloe'}),
(diane: female { name: 'Diane'}),
(eve: female { name: 'Eve'}),
(uhura: female { name: 'Uhura'}),
(adam: male { name: 'Adam'}),
(bob: male { name: 'Bob'}),
(charles: male { name: 'Charles'}),
(daniel: male { name: 'Daniel'}),
(edgar: male { name: 'Edgar'}),
(spock: male { name: 'Spock'})
create (alice)-[:dates]->(adam),
(alice)-[:dates]->(bob),
(barbara)-[:dates]->(bob),
(barbara)-[:dates]->(charles),
(barbara)-[:dates]->(edgar),
(chloe)-[:dates]->(bob),
(chloe)-[:dates]->(daniel),
(chloe)-[:dates]->(edgar),
(diane)-[:dates]->(edgar),
(eve)-[:dates]->(edgar),
(uhura)-[:dates]->(spock);
我试图在 Agensgraph 中重新执行您的查询,但最后一个匹配查询对我不起作用,所以我无法查看其解释。
这是我为检索您想要的相同结果而进行的查询。
match (f:female)<-[r:dates*]->(m:male{name:'Adam'}) with distinct f
match p = ((f)-[:dates]->(m:male)) return p;
--------------------------------------------------------------------------
[female[73.1]{"name": "Alice"},dates[71.23][73.1,74.1]{},male[74.1]{"name": "Adam"}]
[female[73.1]{"name": "Alice"},dates[71.24][73.1,74.2]{},male[74.2]{"name": "Bob"}]
[female[73.2]{"name": "Barbara"},dates[71.25][73.2,74.2]{},male[74.2]{"name": "Bob"}]
[female[73.2]{"name": "Barbara"},dates[71.26][73.2,74.3]{},male[74.3]{"name": "Charles"}]
[female[73.2]{"name": "Barbara"},dates[71.27][73.2,74.5]{},male[74.5]{"name": "Edgar"}]
[female[73.3]{"name": "Chloe"},dates[71.28][73.3,74.2]{},male[74.2]{"name": "Bob"}]
[female[73.3]{"name": "Chloe"},dates[71.29][73.3,74.4]{},male[74.4]{"name": "Daniel"}]
[female[73.3]{"name": "Chloe"},dates[71.30][73.3,74.5]{},male[74.5]{"name": "Edgar"}]
[female[73.4]{"name": "Diane"},dates[71.31][73.4,74.5]{},male[74.5]{"name": "Edgar"}]
[female[73.5]{"name": "Eve"},dates[71.32][73.5,74.5]{},male[74.5]{"name": "Edgar"}]
(10 rows)
坦率地说,当数据量很大时,我不太确定上述查询的性能。
请在运行查询后给我反馈。
3 月 25 日编辑
这可以解决您的问题吗?
match p = allshortestpaths( (f:female)<-[r:dates*]->(m:male) )
where any(node in to_jsonb(nodes(p)) where node.properties.name starts with 'Adam' )
return p;