Graph/Gremlin for social media use case

自作多情 提交于 2019-11-28 14:24:22

When you ask questions about Gremlin, especially one of this complexity, it is always best to include a Gremlin script that provides some sample data, like this:

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()

As for the answer, I would probably do something like this:

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')).values('id').fold()))
==>[post2:[likedBySelf:1,likedByFollowing:[3,4]],post1:[likedBySelf:0,likedByFollowing:[]]]

You find the user and get their followers holding them in a list with aggregate(). Then you find their posts with out('posted'). To get your Map structure for your output you can group() on those "posts". The second by() modulator uses project() to build your inner Map and basically makes two traversals, where the first uses zero or one to represent your boolean value by doing a count() and the second goes back to the "followers" list we aggregated earlier to filter for those. Note the important use of fold() at the end there to reduce the result of that inner traversal to a list.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!