Neo4JClient - How To Add Node to Index

守給你的承諾、 提交于 2019-12-05 05:56:10

问题


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:


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.)



来源:https://stackoverflow.com/questions/12485986/neo4jclient-how-to-add-node-to-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!