27

If I have the cypher query

MATCH (a)-[r]->(b)

I can get the labels of a and b fine line so

MATCH (a)-[r]->(b)
RETURN labels(a), labels(b)

But when I want the label of r using the same syntax

MATCH (a)-[r]->(b)
RETURN labels(r)

I get

Type mismatch: expected Node but was Relationship

How do I return the label of r, the relationship?

Christopher Hackett
  • 6,042
  • 2
  • 31
  • 41

1 Answers1

39

In Neo4j, relationships don't have labels - they have a single type, so it would be:

MATCH (a)-[r]->(b)
RETURN TYPE(r)
Dan G
  • 1,051
  • 8
  • 14
  • is there any reason to not call the type as label as well? – Ooker Nov 06 '21 at 14:02
  • Typically, the term 'label' implies that the target can have more than one of them, and the term 'type' implies a single assignment - relationships can only be one thing, while nodes can be multiple things. That said, two nodes may have multiple relationships, each of a different type. – Dan G Nov 12 '21 at 13:58
  • 1
    I see. But why shouldn't a relationship have multiple labels? – Ooker Nov 12 '21 at 17:15
  • Can you give an example of a relationship where multiple labels is more appropriate than multiple relationships, keeping in mind that relationships can themselves have properties? – Dan G Dec 03 '21 at 19:34
  • I admit that I can't think of one now, but may someone have one use case? Why not let the users decide for themselves? – Ooker Dec 05 '21 at 14:59
  • 1
    The main reason would be that one of the 'prices you pay' for NoSQL databases is that they are specialized. Yes, you can model a graph in a RDBMS- but you wouldn't get the optimizations and performance that a dedicated graph DB provides. A side effects of this is that you end up being much more prescriptive in design. Relationship paths can be optimized more if each has one type. Is it a drawback? Absolutely. Is it worth it? Almost always. Key word is 'almost'. If it's a deal breaker for your solution - see what else can solve your problem. Maybe you can reframe your needs as well. – Dan G Dec 06 '21 at 15:47
  • This is interesting. Do you know any articles discussing this in depth? What keywords should I look for? – Ooker Dec 06 '21 at 15:54