rvaneijk
rvaneijk

Reputation: 663

How to add weight as label to vertex?

I want to add weight to the vertex.

I have the first step going in r-studio. The following code gives a dataframe with the weight:

query = "
MATCH (p)-[:REFERS_TO]->(q)<-[:REFERS_TO]-(r)
WHERE (ID(p) < ID(q))
RETURN q.name, COUNT(q) AS Weight
ORDER BY Weight DESC
"
newvalue = cypher(graph, query)

How can the weight be added to the vertex as a label? The following does not work for me because it adds the weight to the node instead of the vertex:

query = "
MATCH (p)-[:REFERS_TO]->(q)<-[:REFERS_TO]-(r)
WITH q.name, COUNT(q) AS Weight
SET q.weight = Weight
"
cypher(graph, query)

Thanks!

Upvotes: 1

Views: 336

Answers (1)

FrobberOfBits
FrobberOfBits

Reputation: 18022

I think what you're trying to do is to add the weight to the edge (or relationship) rather than to the node (or vertex).

In order to add the weight as a property of the edge, you need to bind it to a variable, and then you can set the property as before:

query = "
MATCH (p)-[r1:REFERS_TO]->(q)<-[r2:REFERS_TO]-(r)
WITH q.name, COUNT(q) AS Weight
SET r1.weight = Weight, r2.weight = Weight
"
cypher(graph, query)

Note I can't tell which of the relationships you want the weight on, so in this example I'm doing both. The only thing here is I'm binding those two relationships to r1 and r2. Relationships can have properties just like nodes, so the rest is straightforward.

Upvotes: 1

Related Questions