Search code examples
graphazure-cosmosdbgremlintinkerpop3

Can we filter multiple labels simultaneously


I have a scenario where I have to check multiple vertices with different labels and match their properties under a parent vertex. And then return the parent vertex if everything matches fine.

I tried writing queries with 'and' clause and 'where' clause but none is working:

Here are my trials:

g.V().hasLabel('schedule').inE().outV().hasLabel('url').as('a').outE().inV().aggregate('x').hasLabel('schedule').has('name', '3').as('b').select('x').hasLabel('states').has('name', 'federal').as('c').select('a')

g.V().hasLabel('schedule').inE().outV().hasLabel('url').as('a').outE().where(inV().hasLabel('schedule').has('name', '3')).where(inV().hasLabel('states').has('name', 'federal')).select('a')

g.V().hasLabel('schedule').inE().outV().hasLabel('url').as('a').outE().and(inV().hasLabel('schedule').has('name', '3'),inV().hasLabel('states').has('name', 'federal')).select('a')

g.V().hasLabel('schedule').inE().outV().hasLabel('url').as('a').outE().inV().aggregate('x').hasLabel('schedule').has('name', '3').as('b').select('x').unfold().hasLabel('states').has('name', 'federal').as('c').select('a')

Please guide me through the right path


Solution

  • You can definitely simplify your approach. I don't think you need the step labels and select() for what you are doing which is good, because they add cost to your traversal. I tried to re-write the first traversal you supplied and I"m hoping I have the logic right, but regardless, I'm thinking you will get the idea for what you need to do when you see the change:

    g.V().hasLabel('schedule').in().hasLabel('url').
      where(and(out().hasLabel('schedule').has('name', '3'),
                out().hasLabel('states').has('name', 'federal')))
    

    You already have the "parent" that you want to return on the first line, so just do a filter with where() and add your filtering logic there to traverse away from each of those "parents".