2

I need a very simple example of how to add a node to an index with using Neo4JClient

In the following C# code I have created an index and an employee node.

Question:
In the following code, how can the node that was created be added to the index? The solutions should allow for the ability to search on EmployeeID or Name.

    class Program
    {
        static void Main(string[] args)
        {
            //Connect to Neo4J
            var graphClient = new GraphClient(new Uri(@"http://localhost:7474/db/data"));
            graphClient.Connect();

            //Create Index
            graphClient.CreateIndex("employee", new IndexConfiguration() { Provider = IndexProvider.lucene, Type = IndexType.exact }, IndexFor.Node);

            //Create an Employee node
            var employee = new Employee() { EmployeeID = "12345", Name = "Mike"};
            NodeReference employeeRef = graphClient.Create(employee);

            //Add the node that was just created to the Employee index.  

        }
        private class Employee
        {
            [JsonProperty("EmployeeID")]
            public string EmployeeID { get; set; }

            [JsonProperty("Name")]
            publi

1 Answer 1

2

Note: This answer applies to Neo4jClient 1.0.0.474. Make sure you have updated.

When you create the node, you can supply the index entries:

var employeeRef = graphClient.Create(
    employee,
    new IRelationshipAllowingParticipantNode<Employee>[0],
    new []
    {
        new IndexEntry("employee")
        {
            {"EmployeeID", 1234 },
            { "Name", "Mike" }
        }
    }
);

It looks a little verbose for a few reasons:

  1. You would almost never create a node without at least one relationship. The relationships would stack nicely in that second parameter.

  2. One node can end up in multiple indexes, and the keys and values don't have to match the node.

We would like to make this syntax nicer for the default scenario, but haven't done it yet.

When you update a node, you also need to supply new index entries then:

graphClient.Update(employeeRef,
    e =>
    {
        e.Name = "Bob";
    },
    e => new[]
    {
        new IndexEntry("employee") { { "Name", e.Name } }
    });

You can reindex a node without updating the node itself using graphClient.ReIndex.

If you want to add an existing node to the index, without updating it, just use graphClient.ReIndex as well. (That method doesn't make any assumptions about the node already being in the index.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.