Neo4jRestClient:如何在创建节点和连接后添加索引?

时间:2012-06-03 16:56:38

标签: .net neo4j

我对neo4j很新,目前正在使用Neo4jRestNet客户端。我创建了一个小型网络用于学习:

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 index = graphDb.index(); Index people = index.forNodes(“people”); RelationshipIndex roles = index.forRelationships(“roles”);

并将其添加到使用GenerateNetwork()方法创建的人员,该方法在java中使用以下方法完成:

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

到目前为止,我还没有找到使用Neo4jRestClient处理索引的方法。有没有人知道如何使用Neo4jRestClient检查现有索引并将节点分配给索引?

如果有人分享一个例子,我将非常感激。

1 个答案:

答案 0 :(得分:0)

使用AddToIndex方法将节点/关系添加到索引。例如:

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

或者您可以使用Node上的静态函数:

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

其中"nodes"是索引的名称,"name" / "jack"是键/值对。

如果为值参数传递Enum,它将使用节点上属性集合中的值。

注意:我刚刚推出了截至2012年6月6日的新代码,您应该尝试开始使用它。它现在支持批处理命令,我将很快更新API以支持cypher变异命令。我也会尽快更新文档,因为它们已经过时了。

第h