문제

I have a Neo4j instance running. I have relationships which are weighted. One such example is representing all places in the world, and having a distance between each place.

Now it seems easy to create a relationship between two nodes, A and B:

Relationship relationship = A.createRelationshipTo(B, Relations.Knows );

But what about actual strength of the relationship. Theres been a suggestion on here as :

create a-[:`KNOWS_0.34`]->b

This works, but isn't a particularly great solution, particularly if you wanted to calculate a shortest distance dependent on the weights.

Is there anyway of doing this and storing the relationship int or float?

도움이 되었습니까?

해결책

I wasn't aware of this, but seems you can set additional properties when creating a Relationship:

Relationship relAB = createRelationship( nodeA, nodeC, "length", 2d );

다른 팁

It's a good approach to use a weight property on your relationship!

Take a look at Graph Algorithm examples in the Neo4j tutorial pages. It describes various ways to find shortest paths using weighted relationships e.g. using Dijkstra or A* algorithms. A great dijkstra example can be found here.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top