2012-06-03 120 views
0

我對neo4j很陌生,目前正在使用Neo4jRestNet客戶端。我創建了一個小型網絡學習的目的:Neo4jRestClient:如何創建節點和連接後創建索引?

private static void GenerateNetwork() 
     { 
      var rootNode = Node.GetRootNode(); 

      // Create a User Node with Properties 
      var prop = new Properties(); 
      prop.SetProperty(NodeProperty.Name.ToString(), "Madeline J. Parnell"); 
      prop.SetProperty(NodeProperty.Age.ToString(), "25"); 
      prop.SetProperty(NodeProperty.LivesIn.ToString(), "LA"); 

      var nodeUserWithName1 = Node.CreateNode(NodeType.Person.ToString(), prop); 

      prop = new Properties(); 
      prop.SetProperty(NodeProperty.Name.ToString(), "Jack S. Waldrop"); 
      prop.SetProperty(NodeProperty.Age.ToString(), "28"); 
      prop.SetProperty(NodeProperty.LivesIn.ToString(), "San Francisco"); 

      var nodeUserWithName2 = Node.CreateNode(NodeType.Person.ToString(), prop); 

      prop = new Properties(); 
      prop.SetProperty(NodeProperty.Name.ToString(), "Willis O. Hicks"); 
      prop.SetProperty(NodeProperty.Age.ToString(), "33"); 
      prop.SetProperty(NodeProperty.LivesIn.ToString(), "Ann Arbor"); 

      var nodeUserWithName3 = Node.CreateNode(NodeType.Person.ToString(), prop); 

      prop = new Properties(); 
      prop.SetProperty(NodeProperty.Name.ToString(), "Leroy P. Wagner"); 
      prop.SetProperty(NodeProperty.Age.ToString(), "40"); 
      prop.SetProperty(NodeProperty.LivesIn.ToString(), "Hattiesburg"); 

      var nodeUserWithName4 = Node.CreateNode(NodeType.Person.ToString(), prop); 

      prop = new Properties(); 
      prop.SetProperty(NodeProperty.Name.ToString(), "Nancy J. Rose"); 
      prop.SetProperty(NodeProperty.Age.ToString(), "25"); 
      prop.SetProperty(NodeProperty.LivesIn.ToString(), "Cornish"); 

      var nodeUserWithName5 = Node.CreateNode(NodeType.Person.ToString(), prop); 

      prop = new Properties(); 
      prop.SetProperty(NodeProperty.Name.ToString(), "Sally G. Gee"); 
      prop.SetProperty(NodeProperty.Age.ToString(), "48"); 
      prop.SetProperty(NodeProperty.LivesIn.ToString(), "Charlestown"); 

      var nodeUserWithName6 = Node.CreateNode(NodeType.Person.ToString(), prop); 

      prop = new Properties(); 
      prop.SetProperty(NodeProperty.Name.ToString(), "Blanche T. Perez"); 
      prop.SetProperty(NodeProperty.Age.ToString(), "35"); 
      prop.SetProperty(NodeProperty.LivesIn.ToString(), "Philadelphia"); 

      var nodeUserWithName7 = Node.CreateNode(NodeType.Person.ToString(), prop); 

      prop = new Properties(); 
      prop.SetProperty(NodeProperty.Name.ToString(), "Robert S. Johnston"); 
      prop.SetProperty(NodeProperty.Age.ToString(), "22"); 
      prop.SetProperty(NodeProperty.LivesIn.ToString(), "Lancaster"); 

      var nodeUserWithName8 = Node.CreateNode(NodeType.Person.ToString(), prop); 

      // Create Relationships to Nodes 
      rootNode.CreateRelationshipTo(nodeUserWithName1, RelationshipType.Knows.ToString()); 
      rootNode.CreateRelationshipTo(nodeUserWithName2, RelationshipType.Knows.ToString()); 
      rootNode.CreateRelationshipTo(nodeUserWithName3, RelationshipType.Knows.ToString()); 
      rootNode.CreateRelationshipTo(nodeUserWithName4, RelationshipType.Knows.ToString()); 
      rootNode.CreateRelationshipTo(nodeUserWithName5, RelationshipType.Knows.ToString()); 
      rootNode.CreateRelationshipTo(nodeUserWithName6, RelationshipType.Knows.ToString()); 
      rootNode.CreateRelationshipTo(nodeUserWithName7, RelationshipType.Knows.ToString()); 
      rootNode.CreateRelationshipTo(nodeUserWithName8, RelationshipType.Knows.ToString()); 

      // Create Relationship with Properties 
      //var relProp = new Properties(); 
      //relProp.SetProperty(RelationshipProperty.Name.ToString(), "MyRelationship"); 
      //relProp.SetProperty("CustomRelProp", "CustomPropValue"); 

      nodeUserWithName1.CreateRelationshipTo(nodeUserWithName2, RelationshipType.Knows.ToString()); 
      nodeUserWithName1.CreateRelationshipTo(nodeUserWithName3, RelationshipType.Knows.ToString()); 
      nodeUserWithName2.CreateRelationshipTo(nodeUserWithName3, RelationshipType.Knows.ToString()); 
      nodeUserWithName5.CreateRelationshipTo(nodeUserWithName6, RelationshipType.Knows.ToString()); 
      nodeUserWithName6.CreateRelationshipTo(nodeUserWithName8, RelationshipType.Knows.ToString()); 
      nodeUserWithName7.CreateRelationshipTo(nodeUserWithName1, RelationshipType.Knows.ToString()); 
      nodeUserWithName7.CreateRelationshipTo(nodeUserWithName1, RelationshipType.Knows.ToString()); 
      nodeUserWithName7.CreateRelationshipTo(nodeUserWithName3, RelationshipType.Knows.ToString()); 
      nodeUserWithName7.CreateRelationshipTo(nodeUserWithName4, RelationshipType.Knows.ToString()); 
      nodeUserWithName7.CreateRelationshipTo(nodeUserWithName5, RelationshipType.Knows.ToString()); 
      nodeUserWithName7.CreateRelationshipTo(nodeUserWithName6, RelationshipType.Knows.ToString()); 
      nodeUserWithName7.CreateRelationshipTo(nodeUserWithName8, RelationshipType.Knows.ToString()); 

     } 

我試圖找到一個方法來創建索引,這將是等同於下面的Java方法:

IndexManager指數= graphDb.index(); Index people = index.forNodes(「people」); RelationshipIndex roles = index.forRelationships(「roles」);

,並把它添加到人,用GenerateNetwork()方法,創建了在Java中與下面的方法做:

people.add(nodeInstance, "valueName", reeves.getProperty("valueName")); 

反正到目前爲止我還沒有找到對付使用Neo4jRestClient指標的方式。有誰知道如何檢查現有索引並使用Neo4jRestClient將節點分配給索引?

如果有人分享了一個例子,我會非常感激。

回答

0

您可以使用AddToIndex方法將節點/關係添加到索引。例如:

var node1 = Node.CreateNode(); 
node1.AddToIndex("nodes", "name", "jack"); 

或者你可以使用上Node靜態函數:

Node.AddToIndex(node1, "nodes", "name", "jack"); 

其中"nodes"是索引的名稱和"name"/"jack"是鍵/值對。

如果您通過Enum作爲value參數,它將使用節點上屬性集合中的值。

注意:自6/6/2012起,我剛推出新代碼,您應該嘗試開始使用它。它現在支持批處理命令,我將很快更新API以支持密碼變異命令。我也將盡快更新文檔,因爲它們目前已過期。

hth。

+0

傑克,謝謝你的回答。我無法運行新版本。舊的運行沒有問題,但新的引發異常在線var rootNode = Node.GetRootNode(); –

+0

你能否給我你的電子郵件地址或聯繫方式,以便談論解決方案? –

+0

Niko,如果根節點已從圖中刪除,則GetRootNode方法將引發錯誤。這裏是關於neo4j.org的討論鏈接(https://groups.google.com/forum/#!topic/neo4j/Sb2ANlJtLDY)。我個人對此的看法;我不喜歡RootNode的想法。由於neo4j討論中提出的問題,我更願意將節點作爲根節點創建到我的圖。如果你想在https://github.com/SepiaGroup/Neo4jRestNet上打開問題,謝謝。 –