Search code examples
groovygremlintinkerpop

Comparing vertex properties in Gremlin Groovy


I'm asking nearly the same question as you see here, but with the constraint that the groovy, not java, syntax must be used. Ideally the answer to would be very concise.

I have a simple graph of people vertices. Each has an "age" property listing that person's age in years. There are also "worksFor" labeled edges connecting pairs of people vertices. I'd like to see all edges where the people at both ends of the edge have the same age property.

I'd then like a similar query where the two ages differ by less than 3 years.

As mentioned, this should be in groovy, not Java, syntax. Gremlin 3 is preferred, but Gremlin 2 answers are acceptable.


Solution

  • all edges where the people at both ends of the edge have the same age property

    g.V().as("a").outE("worksFor").as("e").inV().as("b").select("a","b").by("age").
          where("a", eq("b")).select("e")
    

    where the two ages differ by less than 3 years

    g.V().as("a").outE("worksFor").as("e").inV().as("b").select("a","b").by("age").
          filter {Math.abs(it.get().get("a") - it.get().get("b")) < 3}.select("e")