简体   繁体   中英

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:

  • Create an index on a relationship property
  • Query that index in order to retrieve an specific relationship from one of its values.

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

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM