作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想创建一个显示连接和连接强度的边缘列表。此示例图包含 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 步,显示在同一天参加研讨会的每一对在每个研讨会上的最短时间:
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/
我有一个应用程序,用户在 edittext 中输入数据并按下保存按钮。 通过按“保存”,我将用户数据(在一列中)和当前日期(在另一列中)保存在一个文件中。 然后,我按下另一个按钮并制作绘图(使用图表引
我是一名优秀的程序员,十分优秀!