tesgoe
tesgoe

Reputation: 1072

Identify paths between two nodes in neo4j

I have two paths in a graph: A-B-C-D and A-B-E-F. I would like to assign identification numbers to those paths, i.e. A-B-C-D would be 1 and A-B-E-F would be 2.

Is it possible? If yes, how?

Upvotes: 0

Views: 229

Answers (1)

Nicholas
Nicholas

Reputation: 7501

You mean like a persistent path ID? This isn't directly featured, but you can do it in a Query in Cypher.

If you want somthing persistent, you can always use an index, so create a Relationship index that would store the Relationships of Path 1 under the key/value of Path:1.

EDIT: After getting more information, here's a use case using the index:

It would be up to you to define this in the Index. Here is what you do:

    Node a = db.createNode();
    Node b = db.createNode();
    Node c = db.createNode();
    Node d = db.createNode();
    Node e = db.createNode();
    Node f = db.createNode();

    Relationship aTob = a.createRelationshipTo(b, DynamicRelationshipType.withName("RELATIONSHIP"));
    Relationship bToc = b.createRelationshipTo(c, DynamicRelationshipType.withName("RELATIONSHIP"));
    Relationship cTod = c.createRelationshipTo(d, DynamicRelationshipType.withName("RELATIONSHIP"));

    Relationship bToe = b.createRelationshipTo(e, DynamicRelationshipType.withName("RELATIONSHIP"));
    Relationship eTof = e.createRelationshipTo(f, DynamicRelationshipType.withName("RELATIONSHIP"));

    Index<Relationship> relationshipIndex = db.index().forRelationships("PathIndex");
    String pathRId = UUID.randomUUID().toString();
    String pathMId = UUID.randomUUID().toString();

    relationshipIndex.add(aTob, "PathId", pathRId);
    relationshipIndex.add(bToc, "PathId", pathRId);
    relationshipIndex.add(cTod, "PathId", pathRId);

    relationshipIndex.add(aTob, "PathId", pathMId);
    relationshipIndex.add(bToe, "PathId", pathMId);
    relationshipIndex.add(eTof, "PathId", pathMId);

Then when you want to find a path, you would search by the ID. You would be responsible for maintaining the Set Id in the index, here I use UUID, but you can use something more representative of your information. The relationships would not be in any repeatable order when returned from the Index.

Upvotes: 1

Related Questions