gpt4 book ai didi

gremlin - 如何在 Gremlin 中查找同一天参加同一研讨会的人员的边缘列表?

转载 作者:行者123 更新时间:2023-12-04 08:51:37 27 4
gpt4 key购买 nike

我想创建一个显示连接和连接强度的边缘列表。此示例图包含 4 个人以及他们参加研讨会 A 和 B 的信息,包括他们参加的天数和他们停留的小时数。我想通过研讨会节点建立联系,如果他们在同一天参加同一个研讨会,我会认为两个人有联系,并且联系强度将是在研讨会上花费的最少小时数。
这是示例图:

g.addV('person').property(id, '1').property('name', 'Alice').next()
g.addV('person').property(id, '2').property('name', 'Bob').next()
g.addV('person').property(id, '3').property('name', 'Carol').next()
g.addV('person').property(id, '4').property('name', 'David').next()
g.addV('workshop').property(id, '5').property('name', 'A').next()
g.addV('workshop').property(id, '6').property('name', 'B')

g.V('1').addE('attended').to(g.V('5')).property('hours', 2).property('day', 'Monday').next()
g.V('1').addE('attended').to(g.V('6')).property('hours', 2).property('day', 'Monday').next()
g.V('2').addE('attended').to(g.V('5')).property('hours', 5).property('day', 'Monday').next()
g.V('3').addE('attended').to(g.V('6')).property('hours', 5).property('day', 'Monday').next()
g.V('4').addE('attended').to(g.V('5')).property('hours', 4).property('day', 'Tuesday').next()
g.V('4').addE('attended').to(g.V('6')).property('hours', 4).property('day', 'Monday').next()
g.V('2').addE('attended').to(g.V('6')).property('hours', 1).property('day', 'Monday')
这将是第 1 步,显示在同一天参加研讨会的每一对在每个研讨会上的最短时间:
enter image description here
请注意,David 通过研讨会 A 没有任何联系,因为他与 Alice 和 Bob 在不同的日子参加了研讨会。
然后,我们可以通过将每对工作坊的时间加在一起来找出关系的总强度(现在 Alice 和 Bob 总共有 3 个小时,分别在工作坊 A 和 B 之间):
enter image description here
我正在努力研究如何使用 Gremlin 在海王星图中编写它。我更熟悉 Cypher,并且可以使用以下内容找到这种类型的边缘列表:
match (p:Person)-[a:ATTENDED]->(w:Workshop)<-[a2:ATTENDED]-(other:Person)
where a.day = a2.day
and p.name <> other.name
unwind [a.hours, a2.hours] as hrs
with p, w, other, a, min(hrs) as hrs
return a.name, other.name, sum(hrs) as total_hours
这是我对 Gremlin 的了解,但我不确定如何完成总结:
g.V().
hasLabel('person').as('p').
outE().as('e').
inV().as('ws').
inE('attended').
where(eq('e')).by('day').as('e2').
otherV().
where(neq('p')).as('other').
select('p','e','other','e2','ws').
by(valueMap('name','hours','day'))
有人可以帮忙吗?

最佳答案

如果有更多的时间,我很确定可以简化查询。但是,考虑到您到目前为止的情况,我们可以提取每个人的详细信息:

g.V().
hasLabel('person').as('p').
outE().as('e').
inV().as('ws').
inE('attended').
where(eq('e')).by('day').as('e2').
otherV().
where(neq('p')).as('other').
select('p','e','other','e2','ws').
by(valueMap('name','hours','day').
by(unfold())).
project('p1','p2','shared').
by(select('p').select('name')).
by(select('other').select('name')).
by(union(select('e').select('hours'),
select('e2').select('hours')).min())
这给了我们每个人在一起度过的时间,但还不是总数
==>[p1:Alice,p2:Bob,shared:2]
==>[p1:Alice,p2:Carol,shared:2]
==>[p1:Alice,p2:David,shared:2]
==>[p1:Alice,p2:Bob,shared:1]
==>[p1:Bob,p2:Alice,shared:2]
==>[p1:Bob,p2:Alice,shared:1]
==>[p1:Bob,p2:Carol,shared:1]
==>[p1:Bob,p2:David,shared:1]
==>[p1:Carol,p2:Alice,shared:2]
==>[p1:Carol,p2:David,shared:4]
==>[p1:Carol,p2:Bob,shared:1]
==>[p1:David,p2:Alice,shared:2]
==>[p1:David,p2:Carol,shared:4]
==>[p1:David,p2:Bob,shared:1]
剩下的就是产生最终结果。一种方法是使用 group步。
gremlin> g.V().
......1> hasLabel('person').as('p').
......2> outE().as('e').
......3> inV().as('ws').
......4> inE('attended').
......5> where(eq('e')).by('day').as('e2').
......6> otherV().
......7> where(neq('p')).as('other').
......8> select('p','e','other','e2','ws').
......9> by(valueMap('name','hours','day').
.....10> by(unfold())).
.....11> project('p1','p2','shared').
.....12> by(select('p').select('name')).
.....13> by(select('other').select('name')).
.....14> by(union(select('e').select('hours'),
.....15> select('e2').select('hours')).min()).
.....16> group().
.....17> by(union(select('p1'),select('p2')).fold()).
.....18> by(select('shared').sum())

==>[[Bob,Carol]:1,[David,Alice]:2,[Carol,Alice]:2,[Carol,Bob]:1,[Alice,Bob]:3,[Carol,David]:4,[Bob,Alice]:3,
[David,Bob]:1,[Bob,David]:1,[David,Carol]:4,[Alice,Carol]:2,[Alice,David]:2]
添加 unfold使结果更容易阅读。对于 Bob-Alice 和 Alice-Bob,我没有尝试排除重复项。如果您需要在查询中执行此操作,请输入 order可以在 group 之后添加步骤创建然后一个 dedup用过的。
gremlin> g.V().
......1> hasLabel('person').as('p').
......2> outE().as('e').
......3> inV().as('ws').
......4> inE('attended').
......5> where(eq('e')).by('day').as('e2').
......6> otherV().
......7> where(neq('p')).as('other').
......8> select('p','e','other','e2','ws').
......9> by(valueMap('name','hours','day').
.....10> by(unfold())).
.....11> project('p1','p2','shared').
.....12> by(select('p').select('name')).
.....13> by(select('other').select('name')).
.....14> by(union(select('e').select('hours'),
.....15> select('e2').select('hours')).min()).
.....16> group().
.....17> by(union(select('p1'),select('p2')).fold()).
.....18> by(select('shared').sum()).
.....19> unfold()

==>[Bob, Carol]=1
==>[David, Alice]=2
==>[Carol, Alice]=2
==>[Carol, Bob]=1
==>[Alice, Bob]=3
==>[Carol, David]=4
==>[Bob, Alice]=3
==>[David, Bob]=1
==>[Bob, David]=1
==>[David, Carol]=4
==>[Alice, Carol]=2
==>[Alice, David]=2

关于gremlin - 如何在 Gremlin 中查找同一天参加同一研讨会的人员的边缘列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64072596/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com