cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! Site migration is underway. Expect disruption to service on Thursday, Feb. 9!

Display property in place of relationship name

steve
Node

I'm running Neo4j Community v.3.5.7 on a Mac.

I have a schema that looks something like this:

(:Person { Name: Name, FullName: FullName, Level: Level, Strength: Strength})
(:Strength {Name: Strength})

When I do a query,

MATCH (p:Person),(s:Strength)
WHERE p.Strength = s.Name AND p.Strength = "Responsibility" 
CREATE (p)-[r:Responsibility]->(s)
RETURN p,s

I see this:
2X_3_37f3e3188330430f93b9a48c1d2f72991945be4b.png

How do I get the relationship 'lines' (e.g. labeled as "Responsibility") to be labeled with the Person's "Level" (an integer value property from the Person schema) instead of the name of the relationship?

1 ACCEPTED SOLUTION

Use apoc.create.relationship for this.

As the 'Level' property values are integers you will have a problem in selecting the relationship as the relationship label has to be a string. Here are the workarounds for this problem,

1: Use Level values (needs to be converted to string)

MATCH (p:Person),(s:Strength)
WHERE p.Strength = s.Name AND p.Strength = "Responsibility"
WITH p, s
CALL apoc.create.relationship(p, toString(p.Level), {}, s) YIELD rel
RETURN p, s;

2X_8_8a46b4a6e21ba813b8f5ed0abc289e6b38c0d5cc.png

To select relationship "8" workaround:

MATCH (p:Person)-[r]-(s)
WHERE Type (r) = "8"
RETURN p, s;
2X_1_192571eef581c7b3e8023c584f85ffd65da2faf4.png

2: Prefix Level value with letter "S":

MATCH (p:Person),(s:Strength)
WHERE p.Strength = s.Name AND p.Strength = "Responsibility"
WITH p, s
CALL apoc.create.relationship(p, ("S" + toString(p.Level)), {}, s) YIELD rel
RETURN p, s;

2X_a_aa00296f1d228d82d866e7652fc2f5867ae7ab1b.png

You can easily filter by relationship type:

MATCH (p:Person)-[:S8]-(s)
RETURN p, s;

2X_7_715bd492854d0858945c51df35a286641e608976.png

Hope this works for you.

View solution in original post

4 REPLIES 4

Hello Steve and welcome to the community!

The level/strength properties are set for the nodes in your example. If you want the same properties for the relationships, you would simply use the SET clause to set the values. Also, I would recommend you use Merge for creating the relationships to ensure you do not create duplicate relationships.

For example:

MATCH (p:Person),(s:Strength)
WHERE p.Strength = s.Name AND p.Strength = "Responsibility"
MERGE (p)-[r:Responsibility]->(s)
SET r.Strength = "xxx"
RETURN p,s

This will set the property for the relationship. Ideally you do not want to duplicate data (node property/relationship property) in the graph.

Elaine

Thanks, but "Responsibility" still shows up as the label for the relationship - I want it to display the Person's "Level" instead.

Use apoc.create.relationship for this.

As the 'Level' property values are integers you will have a problem in selecting the relationship as the relationship label has to be a string. Here are the workarounds for this problem,

1: Use Level values (needs to be converted to string)

MATCH (p:Person),(s:Strength)
WHERE p.Strength = s.Name AND p.Strength = "Responsibility"
WITH p, s
CALL apoc.create.relationship(p, toString(p.Level), {}, s) YIELD rel
RETURN p, s;

2X_8_8a46b4a6e21ba813b8f5ed0abc289e6b38c0d5cc.png

To select relationship "8" workaround:

MATCH (p:Person)-[r]-(s)
WHERE Type (r) = "8"
RETURN p, s;
2X_1_192571eef581c7b3e8023c584f85ffd65da2faf4.png

2: Prefix Level value with letter "S":

MATCH (p:Person),(s:Strength)
WHERE p.Strength = s.Name AND p.Strength = "Responsibility"
WITH p, s
CALL apoc.create.relationship(p, ("S" + toString(p.Level)), {}, s) YIELD rel
RETURN p, s;

2X_a_aa00296f1d228d82d866e7652fc2f5867ae7ab1b.png

You can easily filter by relationship type:

MATCH (p:Person)-[:S8]-(s)
RETURN p, s;

2X_7_715bd492854d0858945c51df35a286641e608976.png

Hope this works for you.

steve
Node

F-A-N-T-A-S-T-I-C !!

I used 2: Prefix Level value with letter "S" and it was the perfect answer for what I was trying to accomplish.

Thank you so much for your time and trouble