0

I need to return the 'posts' vertices, but those posts have some 'like' edges, how can I return the count of 'likes' edges for that posts as a property of that edge, like this:

{ title: 'lorem ipsum.....', content: 'yadayadayada', likes: 6 <---- }

Augusto Will
  • 562
  • 7
  • 20

1 Answers1

6

Using TinkerPop's modern toy graph as an example, you could do something like this:

gremlin> g.V().as('a').
......1>   map(outE('created').count()).as('count').
......2>   select('a','count').by(valueMap()).by()
==>[a:[name:[marko],age:[29]],count:1]
==>[a:[name:[vadas],age:[27]],count:0]
==>[a:[name:[lop],lang:[java]],count:0]
==>[a:[name:[josh],age:[32]],count:2]
==>[a:[name:[ripple],lang:[java]],count:0]
==>[a:[name:[peter],age:[35]],count:1]

It returns the properties of the vertices in "a" and the count of "created" edges. You might also choose to use project():

gremlin> g.V().
......1>   project('a','knows','created').
......2>     by(valueMap()).
......3>     by(outE('knows').count()).
......4>     by(outE('created').count())
==>[a:[name:[marko],age:[29]],knows:2,created:1]
==>[a:[name:[vadas],age:[27]],knows:0,created:0]
==>[a:[name:[lop],lang:[java]],knows:0,created:0]
==>[a:[name:[josh],age:[32]],knows:0,created:2]
==>[a:[name:[ripple],lang:[java]],knows:0,created:0]
==>[a:[name:[peter],age:[35]],knows:0,created:1]
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • why this last empty by() ? – Augusto Will Sep 26 '17 at 13:44
  • 1
    When you `select()` the `by()` modulators are applied in a round-robin fashion so if you don't add the empty `by()` to pass through the "count" unchanged, Gremlin will try to apply a `valueMap()` to it and that will cause an error as you will be trying to apply `valueMap()` to the `Long` value of "count". – stephen mallette Sep 26 '17 at 14:28
  • 1
    I need some material to get more understanding in depth about Gremlin, the documentation is confused and dont explain about things in a more interconnected way, can someone recommend some material in video, books or things like that? I'm seaching for "gremlin" and "tinkerpop", there are better key words to find more relevant material ? – Augusto Will Sep 26 '17 at 17:29
  • 2
    The main body of TinkerPop documentation is for reference - the index of all things that are TinkerPop related. For more cohesive explanations I would look at the [tutorials](http://tinkerpop.apache.org/docs/current/#tutorials) and this [series of blog posts](http://www.doanduyhai.com/blog/?p=13460). – stephen mallette Sep 26 '17 at 18:04
  • In that code you posted, how can I add another edge count? I try but doesn't works: g.V().as('a').map(outE('like').count()).as('like').map(inE('comment').count()).as('comment').select('a','like', 'comment').by(valueMap()).by() – Augusto Will Sep 26 '17 at 18:56
  • 1
    Try to think of Gremlin as a stream where each step produces some transformation to the object coming into it and thus supplies the next step with that transformed object. so when you do the first `map()` you convert the vertex from `V()` to a long and that long value becomes the input to the second `map()` which you try to do an `inE()` on and it fails (i.e. it expects a vertex). updated my answer with a different way you might approach it. – stephen mallette Sep 26 '17 at 19:50