ppareja
ppareja

Reputation: 760

How to retrieve a specific relationship by one of its properties in Cypher? (neo4j)

I am trying to come up with the Cypher syntax to both:

Since labels cannot be used for relationships, how could it be done?

Upvotes: 1

Views: 1223

Answers (1)

FylmTM
FylmTM

Reputation: 1997

Unfortunately, you can't add index to relationships in Neo4j. So, you ca't query database for specific relationship by it's property value.

There are alternatives.

Data model

You can reorganize data in such way that:

  • Specific properties are in nodes
  • Create specific relationship types

Usually you can design that in a way when properties that are needed for queries are in nodes, and only additional properties (i.e. cost) are in relationship.

Anyway you can query relationship by specific property:

MATCH (start)-[:RELATIONSHIP {property: "value"}]->(end)

This will end up in fullscan of :RELATIONSHIP scope.

If you know start/end node id's

If you know id's for start and end nodes, and want to update property for specific relationship, then this one should work:

MATCH (start)-[r:RELTYPE {property: "value"}]->(end)
WHERE id(start) = 1 AND id(end) = 2
WITH r
SET r.property = "new_value"
RETURN r

Legacy index

Warning: this functionality is deprecated. I believe that it will be removed in Neo4j 3.0.0.

But there is - Relationship indexes. You can checkout what it provides and if this functionality can be used in your app.

Upvotes: 1

Related Questions