Gremlin-随机选择一项 [英] Gremlin - Choose one item at random

查看:89
本文介绍了Gremlin-随机选择一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将我视为用户" 1.查询的目的是让我关注的人发布"这些帖子,并对每个帖子进行检查:

Consider me as 'user' 1. The aim of the query is to get the posts 'posted' by the people I follow and for each of those posts check:

  1. 我是否喜欢它
  2. 我关注的其他人是否喜欢它,如果是,则随机选择其中一个用户返回

样本数据:

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('post').property('postId','post1').as('p1').
  addV('post').property('postId','post2').as('p2').
  addE('follow').from('1').to('2').
  addE('follow').from('1').to('3').
  addE('follow').from('1').to('4').
  addE('posted').from('2').to('p1').
  addE('posted').from('2').to('p2').
  addE('liked').from('1').to('p2').
  addE('liked').from('3').to('p2').
  addE('liked').from('4').to('p2').iterate()

查询:(如此处回答:图形/格林姆林用于社交媒体用例) g.V().has('id',1).as('me').out('follow').aggregate('followers').out('posted').group().by('postId').by(project('likedBySelf','likedByFollowing').by(__.in('liked').where(eq('me')).count()).by(__.in('liked').where(within('followers')).order().by(shuffle).values('id').fold()))

Query: (As answered here: Graph/Gremlin for social media use case) g.V().has('id',1).as('me').out('follow').aggregate('followers').out('posted').group().by('postId').by(project('likedBySelf','likedByFollowing').by(__.in('liked').where(eq('me')).count()).by(__.in('liked').where(within('followers')).order().by(shuffle).values('id').fold()))

输出:

[post1:[likedBySelf:0,likedByFollowing:[]],post2:[likedBySelf:1,likedByFollowing:[4,3]]]

此查询能够重新排列值,但显示所有"id",现在我只想选择第一个"id".使用.next()而不是.fold()会导致异常java.util.NoSuchElementException 是否有可能在没有先评估所有遍历然后将它们进行改组的情况下进行随机选择?

This query is able to shuffle the values but shows all of the 'id's, now I want to select only the first 'id'. Using .next() instead of .fold() causes exception java.util.NoSuchElementException Is it possible to choose randomly without having to evaluate all the traversals first and then shuffling them?

所需的输出:

[post1:[likedBySelf:0,likedByFollowing:[]],post2:[likedBySelf:1,likedByFollowing:[3]]]

[post1:[likedBySelf:0,likedByFollowing:[]],post2:[likedBySelf:1,likedByFollowing:[4]]]

推荐答案

您非常接近您的答案:

gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('user').property('id',1).as('1').
......1>   addV('user').property('id',2).as('2').
......2>   addV('user').property('id',3).as('3').
......3>   addV('user').property('id',4).as('4').
......4>   addV('post').property('postId','post1').as('p1').
......5>   addV('post').property('postId','post2').as('p2').
......6>   addE('follow').from('1').to('2').
......7>   addE('follow').from('1').to('3').
......8>   addE('follow').from('1').to('4').
......9>   addE('posted').from('2').to('p1').
.....10>   addE('posted').from('2').to('p2').
.....11>   addE('liked').from('1').to('p2').
.....12>   addE('liked').from('3').to('p2').
.....13>   addE('liked').from('4').to('p2').iterate()
gremlin> g.V().has('id',1).as('me').
......1>   out('follow').
......2>   aggregate('followers').
......3>   out('posted').
......4>   group().
......5>     by('postId').
......6>     by(project('likedBySelf','likedByFollowing').
......7>          by(__.in('liked').where(eq('me')).count()).
......8>          by(__.in('liked').where(within('followers')).order().by('id',shuffle).values('id').limit(1).fold()))
==>[post2:[likedBySelf:1,likedByFollowing:[3]],post1:[likedBySelf:0,likedByFollowing:[]]]

我几乎只添加了limit(1),以便仅选择shuffle之后的第一项.它执行了几次,但是我能够看到您正在使用此方法寻找的两个输出.正如我在另一个问题上建议的那样,您也可以使用sample(1):

I pretty much just added limit(1) so that only the first item after shuffle is selected. It took a few executions but I was able to see both of the outputs you were looking for with this method. As I suggested on your other question, you might also use sample(1):

gremlin> g.V().has('id',1).as('me').
......1>   out('follow').
......2>   aggregate('followers').
......3>   out('posted').
......4>   group().
......5>     by('postId').
......6>     by(project('likedBySelf','likedByFollowing').
......7>          by(__.in('liked').where(eq('me')).count()).
......8>          by(__.in('liked').where(within('followers')).sample(1).values('id').fold()))
==>[post2:[likedBySelf:1,likedByFollowing:[3]],post1:[likedBySelf:0,likedByFollowing:[]]]
gremlin> g.V().has('id',1).as('me').
......1>   out('follow').
......2>   aggregate('followers').
......3>   out('posted').
......4>   group().
......5>     by('postId').
......6>     by(project('likedBySelf','likedByFollowing').
......7>          by(__.in('liked').where(eq('me')).count()).
......8>          by(__.in('liked').where(within('followers')).sample(1).values('id').fold()))
==>[post2:[likedBySelf:1,likedByFollowing:[4]],post1:[likedBySelf:0,likedByFollowing:[]]]

这篇关于Gremlin-随机选择一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆