3

I'm want to make a query like this one

g.V().match(
  as('foo').hasLabel('bar'),
  as('foo').out('baz').hasId('123'),
  as('foo').out('baz').hasId('456')
)
.select('foo').by('id')

which is meant to select the ids of all nodes of type bar, which has baz-typed edges to all the specified nodes.

However, CosmosDB only supports a subset of TinkerPop Gremlin, and match() is among the traversal steps that are not supported.

What is a way to formulate the above query using only supported constructs?

Tomas Aschan
  • 58,548
  • 56
  • 243
  • 402

2 Answers2

4

You can do something like this

 g.V().hasLabel('bar').as('a').out('baz').hasId(within('123','456')).select('a').id()

In a large number of cases you can avoid using the match step.

Cheers Kelvin

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
4

I think you want an "and" condition for the ids in the adjacent vertices so I would go for one of these approaches:

g.V().hasLabel('bar')
  and(out('baz').hasId('123'),out('baz').hasId('456'))
  .id()

or perhaps (note that the "2" in is(2) should be equivalent to the number of ids you are trying to validate):

g.V().hasLabel('bar')
  where(out('baz').hasId('123','456').count().is(2))
  .id()

I prefer this second approach I think as you only iterate out('baz') once to confirm the filter. You might make it a little more performant by including limit(2) as follows:

g.V().hasLabel('bar')
  where(out('baz').hasId('123','456').limit(2).count().is(2))
  .id()

In that way the where() filter will exit as early as possible depending on how quickly you find the "123" and "456" vertices. I suppose imposing the limit() would also protect you from situations where you have more than one edge to those vertices and you end up counting more than what you actually need for the where() to succeed.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Hi, is there anyway for gremlin support keyword LIKE ? I saw gremlin 2.x has filter, but 3.3 it tell us Unsupported groovy language rule: 'closure' text: '{it.name.matches(".*ark.*") – Incarnation P. Lee Jun 08 '18 at 10:18
  • `filter()` still exists with two overloads: http://tinkerpop.apache.org/javadocs/current/core/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.html#filter-java.util.function.Predicate- one takes a closure and the other a `Traversal`. i'd double check your syntax a bit. – stephen mallette Jun 08 '18 at 11:55
  • is there anyway write filter like query literal like `g.V().filter {it.get().label() == 'person'}`, I looks cosmosdb graph API do not honor filter at all. – Incarnation P. Lee Jun 11 '18 at 07:45
  • lambdas are not supported apparently - you can read more about their plans for that here: https://github.com/Azure/azure-documentdb-dotnet/issues/413 – stephen mallette Jun 11 '18 at 10:38
  • Copy that, Thanks a lot. – Incarnation P. Lee Jun 14 '18 at 05:31