legaldeveloper
legaldeveloper

Reputation: 15

Greminlin Traversal Basics? Filtering traversal results

first off sorry if this is a duplicate. I'm very new to using Gremin and am currently struggling to get my head round new terminology and can't find what i need but maybe because of the new language.

I've set up some very basic vertices and edges and can seem to traverse ok if something exists but cant seem to work out how to then create a link between them. For example, say I have blog vertex which has 'posted' 3 post vertex. A user vertex that 'follows' a blog and has 'read' 2 of the posts. I'm trying to work out how to return the read and unread posts.

I can traverse to the blog the user follows 'g.V('edward').outE('follows').inV()' and then again to the posts from the blog 'g.V('edward').outE('follows').inV().outE('posted').inV()' but how do I then link the user to the posts to see what they have and haven't read. I then looked at getting the read posts '.inE('read').inV()' but the traversal ends up showing all posts that have been read regardless of who by. I've looked at the .has but seem to keep then traversing back and forwards and cant quite get it to work.

So I guess I have 2 questions, how do I apply the filter in the situation above? And then how do I say where posts have no 'read' for the original user.

Thanks, I know its probably a bit of a complicated question.

--Script--

g.addV('Person').property(id, 'edward').property('FirstName', 'Edward').property('Age', '23')
g.addV('Person').property(id, 'toni').property('FirstName', 'Toni').property('Age', '34')
g.addV('Person').property(id, 'thomas').property('FirstName', 'Thomas').property('Age', '49')
g.addV('Person').property(id, 'casper').property('FirstName', 'Casper').property('Age', '19')
g.addV('Post').property(id, 'post1').property('title', 'Hello World')
g.addV('Post').property(id, 'post2').property('title', 'Where did we come from')
g.addV('Post').property(id, 'post3').property('title', 'Where are we going')
g.addV('Blog').property(id, 'blog1').property('title', 'This is my blog')
g.V('thomas').addE('authored').to(g.V('post1'))
g.V('thomas').addE('authored').to(g.V('post2'))
g.V('toni').addE('authored').to(g.V('post3'))
g.V('edward').addE('read').to(g.V('post1'))
g.V('casper').addE('read').to(g.V('post1'))
g.V('casper').addE('read').to(g.V('post2'))
g.V('casper').addE('read').to(g.V('post3'))
g.V('blog1').addE('posted').to(g.V('post1'))
g.V('blog1').addE('posted').to(g.V('post2'))
g.V('blog1').addE('posted').to(g.V('post3'))
g.V('edward').addE('follows').to(g.V('blog1'))
g.V('casper').addE('follows').to(g.V('blog1'))

Upvotes: 0

Views: 82

Answers (1)

stephen mallette
stephen mallette

Reputation: 46216

Here is one approach:

gremlin> g.V('edward').as('e').
......1>   out('follows').
......2>   out('posted').
......3>   not(where(__.in('read').as('e')))
==>v[post2]
==>v[post3]

It simply uses where() to traverse back down "read" edges of any posts and filters them out if "edward" (labelled "e") is the target vertex.

Upvotes: 1

Related Questions