2012-09-18 34 views
2

我需要的是如何將節點添加到索引使用Neo4JClientNeo4JClient - 如何添加節點,以指數

在我創建了一個索引和僱員節點下面的C#代碼的一個非常簡單的例子。

問:
在下面的代碼,如何能已創建的節點添加到索引?解決方案應該允許在EmployeeID或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 

回答

2

注:這個答案適用於Neo4jClient 1.0.0.474。確保你已經更新。

當您創建的節點上,您可以提供索引條目:

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

它看起來有點冗長的幾個原因:

  1. 你幾乎從不會創建一個沒有在一個節點至少一個關係。關係可以很好地堆疊在第二個參數中。

  2. 一個節點可能以多個索引結束,並且鍵和值不必與節點匹配。

我們希望使這個語法更適合默認場景,但還沒有完成。

當您更新一個節點,你還需要提供新的索引項,則:

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

您可以重新編制一個節點,而無需更新本身使用graphClient.ReIndex節點。

如果您想要將現有節點添加到索引中,而不進行更新,只需使用graphClient.ReIndex即可。 (該方法不對已經在索引中的節點做出任何假設。)

相關問題