zsquare
zsquare

Reputation: 10146

Sort/filter nodes based on relation properties

Given the following graph:

enter image description here

  1. How do I get the nodes adjacent to node[5] with weight > 50?
  2. How do I get the nodes adjacent to node[5] with label "knows", sorted by weight?

Upvotes: 8

Views: 5464

Answers (2)

amirouche
amirouche

Reputation: 7873

  1. How do I get the nodes linked to node[5] with an edge with a label "knows" and a weight > 50 ?

    g.v(5).outE('knows').filter{it.weight > 50}.inV

Depending on the relation/edge direction it might be "inE" instead of outE or "both" step then you will need to adjust the query since you will have edges in the other direction or edges in both directions...

  1. How do I get the nodes related to node[5] with label "knows", sorted by weight?

    g.v(5).outE('knows').filter{it.weight > 50}.sort{a,b -> a.weight <=> b.weight}.toList()._.inV

Upvotes: 10

Josh Adell
Josh Adell

Reputation: 1555

Not sure about Gremlin, but in Cypher it is:

1:

START s=node(5) MATCH s-[r]-f WHERE r.weight > 50 RETURN f

2:

START s=node(5) MATCH s-[r:knows]-f RETURN f ORDER BY r.weight

If you care about the direction of the relationship, put arrows on the relationships, like "s-[]->f" or "s<-[]-f"

Upvotes: 12

Related Questions