gremlinamazon-neptunetinkerpop3

SimplePath to omit certails paths based on vertex property


I have a simple graph where I am trying to generate path() from given vertex

g.addV('ORG').as('1').
  property(single, 'orgId', 'f5c').addV('COMP').
    as('2').
  property(single, 'compId', 2112896).
  property(single, 'owner', 'def').addV('COMP').
    as('3').
  property(single, 'compId', 2100198).
  property(single, 'owner', 'def').addV('COMP').
    as('4').
  property(single, 'compId', 4007384).
  property(single, 'owner', 'def').addV('COMP').
    as('5').
  property(single, 'compId', 2106827).
  property(single, 'owner', 'abc').addV('COMP').
    as('6').
  property(single, 'compId', 2106829).
  property(single, 'owner', 'abc').addV('COMP').
    as('7').
  property(single, 'compId', 2104080).
  property(single, 'owner', 'abc').addV('COMP').
    as('8').
  property(single, 'compId', 2110851).
  property(single, 'owner', 'abc').addV('ORG').
    as('9').
  property(single, 'orgId', 28932).addE('edge').
  from('1').to('5').property('sub', '951a').
  addE('edge').from('1').to('2').
  property('sub', 5779).addE('edge').from('2').
  to('3').property('sub', 5779).addE('edge').
  from('3').to('4').property('sub', 5779).
  addE('edge').from('5').to('6').
  property('sub', '951a').addE('edge').
  from('6').to('7').property('sub', '951a').
  addE('edge').from('7').to('8').
  property('sub', '951a').addE('edge').
  from('4').to('9').property('sub', 1234).
  addE('edge').from('8').to('9').
  property('sub', 465474)

And when I am executing below query, I get 2 different paths which is fine

g.V().has("orgId", "f5c")
.repeat(bothE().otherV().simplePath())
.until(hasLabel("ORG")).path().by(valueMap())

If we see each of the vertex has a property called owner, and in my case I can have multiple owners Question : While getting the path I want to filter out all the paths where let's say owner != def which means it will only return the paths where owner is not def

I tried hasNot()

g.V().has("orgId", "f5c")
.repeat(bothE().otherV().hasNot("owner","def").simplePath())
.until(hasLabel("ORG")).path().by(valueMap())

But it didn't work.

Also why the below query is not giving any result

g.V().has("orgId", "f5c")
.repeat(bothE().otherV().has("compId", 2100198).simplePath())
.until(hasLabel("ORG")).path().by(valueMap())

I am trying to fetch the paths() which passes through a vertex which has property compId with value 2100198.


Solution

  • A couple of reasons why your hasNot filter is not working here:

    1. hasNot() checks for the existence of a property. It does not evaluate the property value for equality. Instead, you need to use has(<key>,neq(<value>)).

    2. Your path contains vertices other than just the COMP vertices. So when reaching the final ORG vertex in the path, you'll run into a situation where the evaluation of has(<key>,neq(<value>)) fails because there's no owner property on the final ORG vertex. I believe what you're looking for is:

    g.V().
      has("orgId", "f5c").
      repeat(
        bothE().
        otherV().
        where(hasLabel('ORG').or().has("owner", neq("def"))).
        simplePath()).
        until(hasLabel("ORG")).
      path().by(valueMap())
    

    Similar situation in your last query. The has("compId", 2100198) will be evaluated on every element (vertex and edge) traversed after the starting vertex. So if you wanted to check the entire path to see if a vertex was traversed with that specific property value, you would need to do something like the following:

    g.V().has("orgId", "f5c")
    .repeat(bothE().otherV().simplePath())
    .until(hasLabel("ORG"))
    .where(
        path().unfold().has("compId", 2100198))
    .path().by(valueMap())