Showing results for 
Search instead for 
Did you mean: 

How to dynamically settings relationships weight

in my neo4j database, there are many kinds of relationships between people and vehicles, and each relationship has a weight value:

  1. Driving relationship (weight: 0.2)
  2. Own relationship (weight: 10)
  3. Ride relationship (weight: 0.1)

Relationship attributes include frequency and weight. For example, the relationship between person A and vehicle B is as follows

  1. Driving relationship (frequency: 10, weight: 0.2)
  2. All relationships (frequency: 1, weight: 10)
  3. Ride relationship (frequency: 32, weight: 0.1)

In my query statement, I need to find the relationship and nodes whose weight * times is greater than 3. this is my Cypher :
match p = (u:user)-[r]->(v:vehicles) where r.frequency * r.weight > 3 return p

but I will dynamically adjust the weight of the relationship according to the query results. When the data volume is very large, this process is very slow.
Is there a way to define a relational weight as a variable or some other way to quickly change the weight value?

Looking forward to your help!



One suggestion I would make is to store the product of weight and frequency as another relationship property, so you don’t have to compute it each time you query. You could index the new property as well.

In my model, the weight of the relationship will be dynamically adjusted according to the calculation results. There are so many relationships that it will be very slow for me to update the weight of the relationship with the following statement:

Match (u:user)-[r:Driving]->(v:vehicles) set r.eight = 0.5

But before the model is stable, I have to modify the weight value repeatedly according to the result of calculation. Is there any good way?


I was just suggesting that if you saved the product of the frequency and weight for each relationships, your query may run faster. Adding an index to the new property may help further. So every time you update the weight during your algorithm, you also set an additional attribute to the product. This will avoid the query from having to compute this same result each time.

match p = (u:user)-[r]->(v:vehicles) where r.frequency * r.weight > 3 return p

new query:
match p = (u:user)-[r]->(v:vehicles) where r.freq_weight_product > 3 return p

I don't know of another way to set a property outside of SET.

If you are running an iterative algorithm that accesses the data via a driver, maybe consider implementing it in your own procedure that runs on the server if performance is an issue.

You could also try implementing it with one of the apoc.periodic methods, if it is not too complex.

Although we haven't found the method of dynamic setting, thank you very much for your suggestion