找到一条已经与顶点连接到特定顶点 ID 的边,并将其与结果合并
Find an edge that is already connected with vertices to a specific vertex ID, and merge it with the result
考虑人员列表场景的 facebook 搜索结果。我想从数据库中获取所有人 (hasLabel('person'))。对于这些人中的每一个人,我想知道登录的人是否已经连接并关注。在 gremlin 中获得这个的最佳解决方案是什么(可能避免重复)?
g.addV('person').property('id',1).as('1').
addV('person').property('id',2).as('2').
addV('person').property('id',3).as('3').
addV('person').property('id',4).as('4').
addE('connected').from('1').to('2').
addE('connected').from('2').to('3').
addE('connected').from('3').to('1').
addE('connected').from('4').to('2').
addE('follows').from('1').to('2').
addE('follows').from('1').to('3').
addE('follows').from('1').to('4').
addE('follows').from('2').to('1').
addE('follows').from('2').to('3').
addE('follows').from('3').to('1').
addE('follows').from('3').to('4').
addE('follows').from('4').to('2').
addE('follows').from('4').to('3').iterate()
例如,如果登录人 id 是 2,则格式化的 JSON 响应将是
[
{
"id": 1,
"follows": true,
"connected": true
},
{
"id": 3,
"follows": true,
"connected": false
},
{
"id": 4,
"follows": false,
"connected": true
}
]
如果登录的人id是4
[
{
"id": 1,
"follows": false,
"connected": false
},
{
"id": 2,
"follows": true,
"connected": true
},
{
"id": 3,
"follows": true,
"connected": false
}
]
注意:提供 JSON 响应是为了了解结果,但我只是想让 Gremlin 查询获得结果。
以下是您正在寻找的一般模式,但是根据上面列出的脚本和边缘的方向,不清楚何时 return 为真,何时不为真。
g.V().
hasLabel('person').
not(has('id', 2)). //find me person 2
project('id', 'follows', 'connected').
by('id').
by(
__.in('follows').
has('id', 2). //traverse all inbound follows edges to find if they go to person 2
fold(). //create an array (empty if nothing)
coalesce(unfold().constant(true), constant(false))). //return true if edge exists, else false
by(
__.out('connected').
has('id', 2).
fold().
coalesce(unfold().constant(true), constant(false)))
根据您提供的脚本,无法得到您所要求的答案。我们只看 connected
个边。
对于顶点 2:
使用 in() 我们会得到 1 和 4 的 true
和 3 的 false
使用 out() 我们会得到 3 的 true
和 1 和 4 的 false
使用 both() all 将是 true
因此,根据上面的结果,您似乎想要使用 in()
边。然而,当我们将其应用于顶点 4 时,所有结果将是 false
考虑人员列表场景的 facebook 搜索结果。我想从数据库中获取所有人 (hasLabel('person'))。对于这些人中的每一个人,我想知道登录的人是否已经连接并关注。在 gremlin 中获得这个的最佳解决方案是什么(可能避免重复)?
g.addV('person').property('id',1).as('1').
addV('person').property('id',2).as('2').
addV('person').property('id',3).as('3').
addV('person').property('id',4).as('4').
addE('connected').from('1').to('2').
addE('connected').from('2').to('3').
addE('connected').from('3').to('1').
addE('connected').from('4').to('2').
addE('follows').from('1').to('2').
addE('follows').from('1').to('3').
addE('follows').from('1').to('4').
addE('follows').from('2').to('1').
addE('follows').from('2').to('3').
addE('follows').from('3').to('1').
addE('follows').from('3').to('4').
addE('follows').from('4').to('2').
addE('follows').from('4').to('3').iterate()
例如,如果登录人 id 是 2,则格式化的 JSON 响应将是
[
{
"id": 1,
"follows": true,
"connected": true
},
{
"id": 3,
"follows": true,
"connected": false
},
{
"id": 4,
"follows": false,
"connected": true
}
]
如果登录的人id是4
[
{
"id": 1,
"follows": false,
"connected": false
},
{
"id": 2,
"follows": true,
"connected": true
},
{
"id": 3,
"follows": true,
"connected": false
}
]
注意:提供 JSON 响应是为了了解结果,但我只是想让 Gremlin 查询获得结果。
以下是您正在寻找的一般模式,但是根据上面列出的脚本和边缘的方向,不清楚何时 return 为真,何时不为真。
g.V().
hasLabel('person').
not(has('id', 2)). //find me person 2
project('id', 'follows', 'connected').
by('id').
by(
__.in('follows').
has('id', 2). //traverse all inbound follows edges to find if they go to person 2
fold(). //create an array (empty if nothing)
coalesce(unfold().constant(true), constant(false))). //return true if edge exists, else false
by(
__.out('connected').
has('id', 2).
fold().
coalesce(unfold().constant(true), constant(false)))
根据您提供的脚本,无法得到您所要求的答案。我们只看 connected
个边。
对于顶点 2:
使用 in() 我们会得到 1 和 4 的 true
和 3 的 false
使用 out() 我们会得到 3 的 true
和 1 和 4 的 false
使用 both() all 将是 true
因此,根据上面的结果,您似乎想要使用 in()
边。然而,当我们将其应用于顶点 4 时,所有结果将是 false