5

I'm sure this is straightforward, but I'm not sure how to do it. I have vertices, with a certain label, which have two integer properties. Let's call them integer1 and integer2. I simply want to query for all vertices where integer2 is greater than integer1.

I have tried the following:

g.V().hasLabel("myLabel").has("integer2", P.gt(values("integer1"))).toList(); 

but this results in an exception - understandably, as the the "values" method call results in a traversal step where as the predicate expects a number.

Exception in thread "main" java.lang.ClassCastException: org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal cannot be cast to java.lang.Integer
    at java.lang.Integer.compareTo(Integer.java:52)
    at org.apache.tinkerpop.gremlin.process.traversal.Compare$3.test(Compare.java:92)
    at org.apache.tinkerpop.gremlin.process.traversal.P.test(P.java:72)
    at org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer.testValue(HasContainer.java:118)
    at org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer.test(HasContainer.java:94)
    at org.apache.tinkerpop.gremlin.process.traversal.step.util.HasContainer.testAll(HasContainer.java:180)
    at org.apache.tinkerpop.gremlin.tinkergraph.process.traversal.step.sideEffect.TinkerGraphStep.iteratorList(TinkerGraphStep.java:116)
    at org.apache.tinkerpop.gremlin.tinkergraph.process.traversal.step.sideEffect.TinkerGraphStep.vertices(TinkerGraphStep.java:88)
    at org.apache.tinkerpop.gremlin.tinkergraph.process.traversal.step.sideEffect.TinkerGraphStep.lambda$new$0(TinkerGraphStep.java:59)
    at org.apache.tinkerpop.gremlin.tinkergraph.process.traversal.step.sideEffect.TinkerGraphStep$$Lambda$23/1123629720.get(Unknown Source)
...

Any help would be greatly appreciated. Thanks.

Amit A.
  • 71
  • 1
  • 3

1 Answers1

7

One way to do it would be with a where() clause. First, to demonstrate I modified the "modern" graph to include a "k" property with an integer value:

g = TinkerFactory.createModern().traversal()
g.V().hasLabel('person').property('k',30) 

and then:

gremlin> g.V().hasLabel('person').as('a').
......1>   where('a', gt('a')).by('age').by('k').
......2>   valueMap('age','k')
==>[k:[30],age:[32]]
==>[k:[30],age:[35]]
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • As usual, Stephen, I'm impressed with your knowledge and responsiveness. This worked. I need to better understand the underlying traversal mechanisms to gain a more intuitive understanding of the syntax. Aside from the standard Tinkerpop documentation, do you recommend anything else to read? – Amit A. Mar 08 '19 at 15:53
  • well, i'd always recommend: http://kelvinlawrence.net/book/Gremlin-Graph-Guide.html and folks often miss the recipes: http://tinkerpop.apache.org/docs/current/recipes/ but also note that a new book on graphs/Gremlin is in early release right now with manning: https://www.manning.com/books/graph-databases-in-action – stephen mallette Mar 08 '19 at 16:09