如何找到Gremlin同一天参加同一研讨会的人员的边缘列表?

如何找到Gremlin同一天参加同一研讨会的人员的边缘列表?,gremlin,amazon-neptune,Gremlin,Amazon Neptune,我想创建一个显示连接和连接强度的边缘列表。此示例图包含4个人及其参加研讨会A和B的信息,包括参加的日期和停留的小时数。我想通过车间节点建立连接,如果在同一天参加同一个车间,我会考虑连接两个人,连接强度将是车间花费的最少小时数。 以下是示例图: g.addV('person').property(id, '1').property('name', 'Alice').next() g.addV('person').property(id, '2').property('name', 'Bob').n

我想创建一个显示连接和连接强度的边缘列表。此示例图包含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步,显示在同一天参加研讨会的每对学员在每个研讨会上的最短时间:

请注意,David与研讨会A没有任何联系,因为他参加研讨会的日期与Alice和Bob不同

然后,我们可以通过将每一对的工作坊的小时数相加,找到关系的总强度(现在Alice和Bob总共有3个小时,分别是工作坊A和工作坊B):

我正在努力研究如何用小精灵在海王星图中写出这个。我对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

有人能帮忙吗?

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

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]
剩下的就是产生最终结果。一种方法是使用
步骤

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]    
添加一个
展开
使结果更易于阅读。对于鲍勃·爱丽丝和爱丽丝·鲍勃,我没有试图找出重复的。如果需要在查询中执行此操作,可以在创建
后添加
顺序
步骤,然后使用
重复数据消除

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          

只是为了确保我明白你在寻找什么-你想要的是和,差还是。。。对于同样的两个人出现在
name
other
中的每一种情况,我都会继续创建一个答案。如果这不是你想要的,请告诉我。
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