user3624334
user3624334

Reputation: 619

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.

Upvotes: 2

Views: 3006

Answers (3)

Long Hill
Long Hill

Reputation: 11

If we know the target vertex against which all other vertices are compared, the following may work:

t=g.V().has('id', 'target_node_id').values('age').next()

g.V().has('age').filter{it.get().value('age')-t<=3 && it.get().value('age')-t>=-3}

I don't know how to do it in one query. I also don't know if there is a function/step to get the absolute value.

This only partially satisfies your need, but it may be a start.

Upvotes: 1

youhans
youhans

Reputation: 6849

Comparing two date properties using math step:

g.V().hasLabel('EnterExitDate').limit(10000).as('enter','exit')
    .where("enter",lt("exit")).by('enter').by('exit')
    .where(math('(exit - enter) / (3600*1000) ')
        .by(values('exit').map({ it.get().time }))
        .by(values('enter').map({ it.get().time }))
        .is(lt(1)))
    .valueMap()

This query will find all pairs of enter-exit record of employees that have happened within 1 hour.

EnterExitDate class:

public class EnterExitDate {
    private Date enter;
    private Date exit;
    // getters and setters...
}

Upvotes: 0

Daniel Kuppitz
Daniel Kuppitz

Reputation: 10904

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")

Upvotes: 7

Related Questions