5

I’m just playing with the Graph API in Cosmos DB which uses the Gremlin syntax for query.

I have a number of users (Vertex) in the graph and each have ‘knows’ properties to other users. Some of these are out edges (outE) and others are in edges (inE) depending on how the relationship was created. I’m now trying to create a query which will return all ‘knows’ relationships for a given user (Vertex). I can easily get the ID of either inE or outE via:

g.V('7112138f-fae6-4272-92d8-4f42e331b5e1').inE('knows') 
g.V('7112138f-fae6-4272-92d8-4f42e331b5e1').outE('knows') 

where '7112138f-fae6-4272-92d8-4f42e331b5e1' is the Id of the user I’m querying, but I don’t know ahead of time whether this is an in or out edge, so want to get both (e.g. if the user has in and out edges with the ‘knows’ label). I’ve tried using a projection and OR operator and various combinations of things e.g.:

g.V('7112138f-fae6-4272-92d8-4f42e331b5e1').where(outE('knows').or().inE('knows'))

but its not getting me back the data I want.

All I want out is a list of the Id’s of all inE and outE that have the label ‘knows’ for a given vertex.

Or is there a simpler/better way to model bi-directional associations such as ‘knows’ or ‘friendOf’?

Thanks

LDJ
  • 6,896
  • 9
  • 52
  • 87

1 Answers1

14

You can use the bothE step in this case. g.V('7112138f-fae6-4272-92d8-4f42e331b5e1').bothE('knows')

pantalohnes
  • 961
  • 5
  • 12
  • 1
    Awesome. That, combined with otherV() allows me to get the Vertexes of everyone that either knows or is known by this vertex, which is exactly what I wanted. Didn't even know bothE existed as it wasn't listed in the TinkerPop ToC: http://tinkerpop.apache.org/docs/current/reference! Thanks – LDJ Aug 29 '17 at 13:25
  • 4
    You could also use `both` to get the vertices that you want. http://tinkerpop.apache.org/javadocs/current/core/org/apache/tinkerpop/gremlin/process/traversal/dsl/graph/GraphTraversal.html#both-java.lang.String...- – pantalohnes Aug 29 '17 at 13:45
  • Even better! Thanks. – LDJ Aug 29 '17 at 13:57