1

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 also know which of the other people I follow have liked it (if any). What is the best solution to get this in gremlin (possibly avoiding duplication)?

Image for clarity

The following just gives the posts 'posted' by USER 2. How to get other information in the same query?

g.V().has('ID','USER 2').out('posted')
Kunal
  • 27
  • 7

1 Answers1

1

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.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Thanks for the response. I updated my question to make it clearer and also added an image. I want to get the posts 'posted' by the people I follow, see whether I have liked any of those posts or not and also see if any of the other people I follow have liked those posts. – Kunal Jul 14 '18 at 12:01
  • Thanks a bunch Stephen. I am really new to gremlin so this helps out a lot. Could you suggest some detailed reference material for gremlin other than the tinkerpop documentation? – Kunal Jul 14 '18 at 21:52
  • Hey Stephen, one more thing. With gremlin rest everything is public, so anyone can hit g.V().drop() and delete everything. How would you secure this on an EC2 instance with public gremlin rest endpoint? Also has CosmosDB fixed this? – Kunal Jul 15 '18 at 11:04
  • Some security explanations here: http://tinkerpop.apache.org/docs/current/reference/#security but there are more advanced concepts as well. perhaps form a more specific question on security once you learn more of the basics. – stephen mallette Jul 15 '18 at 13:54
  • 1. Can vertex properties stored as integers be incremented and decremented? If so, how? – Kunal Jul 17 '18 at 00:09
  • 2. For a fixed dataset, does coalesce always return the same item? Is it possible to randomise it or any other way to do it? For instance of all the incoming vertices, choose a random one every time even if the dataset itself is not changed. – Kunal Jul 17 '18 at 00:10
  • Please start new questions for new topics. It will make the answers more useful to others – stephen mallette Jul 17 '18 at 01:42
  • Here is the link to the new question, thanks: https://stackoverflow.com/questions/51375276/gremlin-use-case-int-properties-and-coalesce – Kunal Jul 17 '18 at 07:13