Search code examples
c#neo4jgremlinneo4jclient

Traversing nodes but including the starting node in the result in Neo4J with Gremlin


I have a graph representing users and some articles they wrote. I need to create something like an activity stream including the articles wrote by the user and also by its friends.

http://twitpic.com/a342f4

I can have all the user's friends’ posts like this:

g.v(4).out("KNOWS").out("POSTED")

and returns

v[9]
v[11]

But I also need to include the posts from the original user retrieved with query:

g.v(4).out("POSTED")

this returns

v[10]

How can I have all the posts without having to do 2 queries and then merging them later? I need to get something like this in return:

v[9]
v[10]
v[11]   

Thanks

PD: I'm using Gremlin and Neo4jClient but if anyone have a better solution with something else its will be fine too, at this point I still can change some of the tools I'm using


Solution

  • Use Gremlin's aggregate step and Groovy's unique method:

    posts = []
    g.v(4).out('POSTED').aggregate(posts).iterate()
    g.v(4).out('KNOWS').out('POSTED').aggregate(posts).iterate()
    return posts.unique()
    

    See...