Graph/Gremlin for social media use case

前端 未结 1 1710
挽巷
挽巷 2020-12-12 03:49

Consider instagram feed scenario. I want to get all the posts \'posted\' by the people I follow. For each of these posts I want to know whether I have liked it or not and al

相关标签:
1条回答
  • 2020-12-12 03:54

    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.

    0 讨论(0)
提交回复
热议问题