0

I have a requirement over putting and condition in edges in gremlin. Do we have a mechanism like we have for or condition. g.V().haslabel('u').outE('label1','label2').inV().has('name','name1')

I have already checked on where clause but it limits the traversal further . Do we have a more flexible mechanism to continue the chain?

Varun Tahin
  • 299
  • 1
  • 2
  • 15
  • are you saying that you want to find "u" vertices that have at least one edge of each type: "label1" and "label2" AND that where both of those edges end in an adjacent vertex with property "name"="name1"? – stephen mallette Sep 19 '18 at 10:44
  • No I want to traverse with and with the labels I have specified. Starting with u give me all out vertices with edges in between having both labels label1 as well as label2 . so there are two edges between vertex u and name1 having labels1 and label2. – Varun Tahin Sep 20 '18 at 08:34
  • ok - i think we're saying the same thing. – stephen mallette Sep 20 '18 at 12:51

2 Answers2

1

It's always best to provide a Gremlin script that generates some sample data as it makes the context of the question much more clear:

g.addV('u').as('u').
  addV('v').property('name','name1').as('v1').
  addV('v').property('name','name2').as('v2').
  addV('v').property('name','name1').as('v3').
  addE('label1').from('u').to('v1').
  addE('label2').from('u').to('v1').
  addE('label1').from('u').to('v2').
  addE('label2').from('u').to('v2').
  addE('label2').from('u').to('v3').iterate()

In this case, I would assume that you only want to get the vertex I labelled above as "v1" as it's the only one with two edges (one labelled "label1" and one labelled "label2") AND that has the property key "name1". I'm further assuming that the schema of your graph will only allow for a singular multiplicity of the edges such that you will either have zero or one edges of "label1" and "label2" between "u" and "v*" vertices.

I took this approach, though there may be others:

gremlin> g.V().hasLabel('u').
......1>   outE('label1','label2').
......2>   where(otherV().has('name','name1')).
......3>   groupCount().
......4>    by(otherV()).
......5>   unfold().
......6>   where(select(values).is(eq(2))).
......7>   select(keys).
......8>   unfold().
......9>   valueMap(true)
==>[id:1,name:[name1],label:v]

The main point here is to consider the groupCount() which is basically giving you the number of edges connecting to otherV(). You want those to equal 2 in order for the filter to succeed. So, after the groupCount(), the resulting Map containing otherV() for the key and the count of edges for the value is unfolded to entries and filtered at line 6. Once we have that we can just grab the keys from the Map because we don't need the counts anymore.

I also tried match() which I don't think is supported by CosmosDB but actually reads pretty nicely in this case:

gremlin> g.V().hasLabel('u').
......1>   match(__.as('u').out('label1').as('v'),
......2>         __.as('u').out('label2').as('v'),
......3>         __.as('v').has('name','name1')).
......4>   select('v').
......5>     by(valueMap(true))
==>[id:1,name:[name1],label:v]
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
0

Same has() conditions work

try: g.E().has('edgeProperty') => This will give all the edges that have 'edgeProperty' set.

Jayanta Mondal
  • 436
  • 2
  • 5