david_p
david_p

Reputation: 6516

How to retrieve a Neo4 relationship by property in Java?

I'm working on a Neo4j plugin and need to retrieve a relationship by property value.

I have access to Neo4j GraphDatabaseService which has a very convenient method GraphDatabaseService.findNode(Label label, String property , String value).

I am looking for the relationship-counterpart of this method, something like GraphDatabaseService.findRelationship(RelationshipType type, String property , String value).

Does this exist? Is it on the roadmap? Is there another way?

Upvotes: 2

Views: 281

Answers (2)

david_p
david_p

Reputation: 6516

Summing up from Michael and Christophe's answers:

  • finding a Relationship by property value with a schema index is not implemented
  • finding relationships by property is possible using manual indexes
  • GraphAware's UUID Neo4j plugin does just that (uses manual indexes) for UUIDs

To get a relationship by UUID, here is the code:

public Relationship getRelByUuid(GraphDatabaseService database, String uuid) {
    UuidReader reader = new DefaultUuidReader(
        getStartedRuntime(database).getModule(UuidModule.class).getConfiguration(),
        database
    );
    return database.getRelationshipById(uuidReader.getRelationshipIdByUuid(uuid));
}

Upvotes: 0

Michael Hunger
Michael Hunger

Reputation: 41676

Right now only nodes are supported with schema indexes.

What would be your use-cases for finding relationships by value without the context of the nodes around them?

You can access manual indexes for relationship via the Java API, it's pretty nice, because you can both find relationships just via property but also in the context of either of their end-nodes. If you create one index-per rel-type it's also really good for nodes with many relationships to filter a few out by also passing in the start or end-node. That's what I added support for in the APOC procedure library.

But you will have to add relationships to that index manually.

Upvotes: 1

Related Questions