How to find the edges of a node that have a weight of x or greater?

I have a graph with nodes that are connected to one another, and I have weights on the edges that connect these nodes. On this query g.V().has("person", "name", "A").out("is friends with").values("name").to_list() returns the names of people that person "A" has the relation "is friends with". But I would like to filter by the weight value. Person A has a weight of 0.9 with Person B, and a weight of 0.7 with Person C. I would like to only get back the people person A has an edge with a weight of greater than 0.8, how can I do that? I am using gremlin-python. I have tried g.V().has("person", "name", "A").out("is friends with").has("weight", gte(0.8)) but I get an error saying NameError: name "gte" is not defined. Did you mean: 'g'?
Solution:
g.V().has("person", "name", "A").out(... returns Vertices to get edges need to use outE() something like a_friends = g.V().has("person", "name", "A").outE("is friends with").has("weight", P.gt(0.75)).inV().to_list()...
Jump to solution
4 Replies
gdotv
gdotv•5mo ago
Try P.gte instead, with the correct import from the gremlin-python library, that should do it
b4lls4ck
b4lls4ckOP•5mo ago
Oh yeah, I also tried that, and for some reason, it didn't work. My query was as follows:
from gremlin_python.process.traversal import P
a_friends = g.V().has("person", "name", "A").out("is friends with").has("weight", P.gt(0.75)).inV().to_list()
from gremlin_python.process.traversal import P
a_friends = g.V().has("person", "name", "A").out("is friends with").has("weight", P.gt(0.75)).inV().to_list()
And it returned an empty list even though I have that person C has a weight of 0.9 with person A Also btw, I am assuming the way I am setting weights for the edges is correct:
person_A_node = g.V().has("person", "name", "A").to_list()[0] # this node represents person A

person_B_node = g.V().has("person", "name", "B").to_list()[0] # this node represents person B

person_C_node = g.V().has("person", "name", "C").to_list()[0] # this node represents person C

g.V(person_A_node).add_e("is friends with").to(person_B_node).property("weight", 0.9).iterate()
g.V(person_A_node).add_e("is friends with").to(person_C_node).property("weight", 0.7).iterate()
person_A_node = g.V().has("person", "name", "A").to_list()[0] # this node represents person A

person_B_node = g.V().has("person", "name", "B").to_list()[0] # this node represents person B

person_C_node = g.V().has("person", "name", "C").to_list()[0] # this node represents person C

g.V(person_A_node).add_e("is friends with").to(person_B_node).property("weight", 0.9).iterate()
g.V(person_A_node).add_e("is friends with").to(person_C_node).property("weight", 0.7).iterate()
Solution
Valentyn Kahamlyk
Valentyn Kahamlyk•5mo ago
g.V().has("person", "name", "A").out(... returns Vertices to get edges need to use outE() something like a_friends = g.V().has("person", "name", "A").outE("is friends with").has("weight", P.gt(0.75)).inV().to_list()
b4lls4ck
b4lls4ckOP•5mo ago
Thank you, this helped me fix my problem 🙂
Want results from more Discord servers?
Add your server