0

I need to get vertices filtered by a specific predicate on the properties, and all the edges (with a particular label, and perhaps some predicate on the properties of an edge) existing between them.

This is for a Cosmos Azure Db graph, and the solution should be a single Gremlin query.

So far I am thinking of something along the lines of:

g.V().has('property1', value1).has('property2', value2).select('vertices')
.outE().as('edges').inV().has('property1', value1).has('property2', value2)
.select('vertices','edges')

Is there a better way to achieve this?

noam621
  • 2,766
  • 1
  • 16
  • 26
  • I think without a real sample its hard to answer you. If this query give you the exact outcome you want? Is the edges you looking always connected to the vertex with property 1 and property 2? Maybe project will give you a better results but you need to give example what you trying to achieve. – noam621 Aug 19 '19 at 22:06
  • They might not be always connected. What I want is to select all the vertices with required properties and then see if there are specific edges between them. If so output the edges as well as the vertices connected by them. – Anton Klimov Aug 20 '19 at 21:32

1 Answers1

0

Given the description and your comment, this traversal should work for you:

g.V().has('property1', value1).has('property2', value2).
  aggregate('v').
  outE().                   /* add edge filters here */
  inV().where(within('v')).
  path().
  union(union(limit(local, 1),
              tail (local, 1)).dedup().aggregate('vertices'),
        range(local, 1, 2).aggregate('edges')).
  cap('vertices','edges').next()
Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34
  • Thank you, that gives me a good solution using within. But path() is not what I need as the output, I need a JSON object with a separate array of vertices and a separate array of edges. Can I get that with one query? For a single array I suppose I could do outE().as('e').... select('e').valueMap() but I need both. – Anton Klimov Aug 21 '19 at 20:03
  • .union(select('e'),select('v')) is almost what I need, but it I cannot do select('v').valueMap() so it still doesn't give me the exact format I need. – Anton Klimov Aug 21 '19 at 20:12
  • Do you want all initial vertices in the result or only those that actually have at least 1 outgoing edge that matches the criteria? – Daniel Kuppitz Aug 22 '19 at 20:15
  • Ideally only vertices connected by edges matching the criteria. – Anton Klimov Aug 23 '19 at 21:05
  • Ok, then the easiest thing to do is actually to grab the path (as it's guaranteed to match the criteria) and then split it up into vertices and edges. I'll update my answer in a few minutes. – Daniel Kuppitz Aug 23 '19 at 21:26