Search code examples
neo4jclient

Adding relationship to index with Neo4jClient


What's the syntax to add a relationship to an already created relationship index in the Neo4jClient?

I've found the following post (Adding a relationship to an index in neo4jclient) but it only says that the syntax is similar to that of nodes but the CreateRelationship method does not have a signature that supports inserting a relationship to the index.

Any help to a Neo4jClient noob would be much appreciated.


Solution

  • You can do it using the ReIndex command:

    if(!GraphClient.CheckIndexExists("relatedto", IndexFor.Relationship))
        GraphClient.CreateIndex("relatedto", ExactIndex, IndexFor.Relationship);
    
    var simple1 = new Simple {Value = "simple_1"};
    var simple2 = new Simple { Value = "simple_2" };
    
    var s1Ref = GraphClient.Create(simple1);
    var s2Ref = GraphClient.Create(simple2);
    
    var relationship = new RelatedTo(s2Ref){RelationshipValue = "indexed_" + s1Ref.Id};
    
    var relRef = GraphClient.CreateRelationship(s1Ref, relationship);
    
    //Adding to the index
    GraphClient.ReIndex(relRef, new []{new IndexEntry("relatedto"){{"value", relationship.RelationshipValue}}});
    
    Console.WriteLine("Use this in N4J Data Browser: rel:index:relatedto:value:{0}", relationship.RelationshipValue);