我有一个 Neptune 数据库并使用 gremlin 来查询它。我有可以与“朋友”、“关注”、“块”、“报告”等边连接的用户顶点。我想进行一个查询,通过向用户展示他所关注的人所关注的用户来向用户提供“建议”。这些用户不应被当前用户关注、阻止或报告,并且他关注的每个用户最多应有 3 条建议。
我构建了以下查询:
g
.V(userId)
.Out("follows")
.As(ConnectingFollowerLabel)
.Local<Vertex>(__
.Out("follows")
.HasLabel("User")
.Not(__.Both("blocks").HasId(userId))
.Not(__.In("reported").HasId(userId))
.Not(__.In("follows").HasId(userId))
.Not(__.HasId(userId))
.Limit<Vertex>(maxProfileSuggestionsPerCommonUser))
.Dedup();
它会按原样返回结果。但对于关注许多用户的用户来说,它会变得有点慢,所以我在获取的用户上添加了 limit() ,并使用 range() 添加了分页,如下所示
g
.V(userId)
.Out("follows")
.As(ConnectingFollowerLabel)
.Local<Vertex>(__
.Out("follows")
.HasLabel("User")
.Not(__.Both("blocks").HasId(userId))
.Not(__.In("reported").HasId(userId))
.Not(__.In("follows").HasId(userId))
.Not(__.HasId(userId))
.Limit<Vertex>(maxProfileSuggestionsPerCommonUser))
.Limit<Vertex>(maxProfileSuggestions)
.Dedup()
.Range<Vertex>(Scope.Global, offset, offsetLimit);
一旦添加限制和范围步骤,结果就会发生变化,并且我会从已经关注的人那里得到建议。我尝试在范围步骤之前对结果进行排序,但这似乎也不起作用。
与查询一起使用的示例图可能是这样的:
g.addV('User').property(id, '1').as('1')
.addV('User').property(id, '2').as('2')
.addV('User').property(id, '3').as('3')
.addV('User').property(id, '4').as('4')
.addV('User').property(id, '5').as('5')
.addV('User').property(id, '6').as('6')
.addV('User').property(id, '7').as('7')
.addV('User').property(id, '8').as('8')
.addV('User').property(id, '9').as('9')
.addV('User').property(id, '10').as('10')
.addE('follows').from('1').to('2')
.addE('follows').from('1').to('3')
.addE('follows').from('1').to('4')
.addE('follows').from('1').to('5')
.addE('follows').from('1').to('6')
.addE('follows').from('1').to('7')
.addE('friend_requests').from('1').to('8')
.addE('report_citizen').from('1').to('8')
.addE('blocks').from('1').to('7')
.addE('follows').from('2').to('3')
.addE('follows').from('2').to('4')
.addE('follows').from('2').to('5')
.addE('follows').from('2').to('6')
.addE('follows').from('2').to('7')
.addE('follows').from('2').to('8')
.addE('follows').from('3').to('7')
.addE('follows').from('3').to('8')
.addE('follows').from('3').to('9')
.addE('follows').from('3').to('10')
.addE('follows').from('4').to('5')
.addE('follows').from('4').to('6')
.addE('follows').from('4').to('7')
.addE('follows').from('4').to('8')
.addE('follows').from('5').to('3')
.addE('follows').from('5').to('6')
.addE('follows').from('5').to('7')
.addE('follows').from('5').to('8')
.addE('follows').from('5').to('9')
.addE('follows').from('5').to('10')
.addE('follows').from('8').to('1')