Robert Huang
Robert Huang

Reputation: 109

How can I filter after select in gremlin?

I have saved a list of vertices in a variable.

tv = g.V().hasLabel('user').has('name', 'xxyy').out().out().dedup().toList()

The list contains vertices that have label 'topic'.

I am trying to do a traversal to find content that has the same topics in the variable above.

g.V().hasLabel('user').has('name', 'xxyy').out().in().hasLabel('influencer')
.dedup().out().hasLabel('content').as('content').out().as('topic')
.select('content', 'topic')
==>{content=v[22663384], topic=v[23412864]}
==>{content=v[22663384], topic=v[23412864]}
==>{content=v[22663384], topic=v[23445624]}
==>{content=v[22663384], topic=v[23445624]}
==>{content=v[22663384], topic=v[23548032]}
==>{content=v[22663384], topic=v[23548032]}
==>{content=v[22663384], topic=v[23597056]}
==>{content=v[22663384], topic=v[23597056]}
==>{content=v[22663384], topic=v[24039672]}
==>{content=v[22663384], topic=v[24039672]}
==>{content=v[22663384], topic=v[64716880]}
==>{content=v[22663384], topic=v[64716880]}
==>{content=v[22663384], topic=v[65163456]}
...

This will give all the pairs of content and their topics. I only want the content vertices that the topic is within tv.

I cannot figure out how I can use filter or where to do the filtering. Any help is appreciated.

Thank you!

Upvotes: 1

Views: 503

Answers (1)

Daniel Kuppitz
Daniel Kuppitz

Reputation: 10904

If you need the variable tv for something else, then your second query would be:

g.V().hasLabel('user').has('name', 'xxyy').
   out().in().hasLabel('influencer').
   dedup().
   out().hasLabel('content').as('content').
   out().is(within(tv)).as('topic').
   select('content', 'topic')

However, if the sole purpose of tv is to be a filter variable, you don't need to do that. You can do everything in a single query:

g.V().hasLabel('user').has('name', 'xxyy').
   out().sideEffect(out().aggregate('tv')).barrier().
   in().hasLabel('influencer').
   dedup().
   out().hasLabel('content').as('content').
   out().where(within('tv')).as('topic').
   select('content', 'topic')

As an aside: You should always specify the edge label in your traversals.

Upvotes: 3

Related Questions