2012-04-26 140 views
4

我想知道当需要根据某些节点类型或字段设置多个indecies时,什么是更好的方法。 例如,假设我想要一张学生图,并希望通过他们的学校和身份证编制索引。在Neo4j中建立索引

按我的理解,我可以有这样每所学校的指标:

// add student 
Index<Node> index = this.graphDb.index().forNodes(schoolName); 
Node node = this.graphDb.createNode(); 
node.setProperty("id", studentId); 
index.add(node, "id", studentId); 

// get student 
Index<Node> index = this.graphDb.index().forNodes(schoolName); 
Node node = index.get("id", studentId).getSingle(); 

我能,另一方面采用一个指标,这样做:

// add student 
Index<Node> index = this.graphDb.index().forNodes("schools"); 
Node node = this.graphDb.createNode(); 
node.setProperty("id", studentId); 
index.add(node, schoolName + ":id", studentId); 

// get student 
Index<Node> index = this.graphDb.index().forNodes("schools"); 
Node node = index.get(schoolName + ":id", studentId).getSingle(); 

什么是更好的方法? 对彼此有什么好处? 当涉及到很多节点时,尤其是性能明智或存储方面。

谢谢

回答

7

您的方法是完全有效的。 如果要查询一所学校的所有学生都可以使用:

Iterable<Node> pupils = index.query(schoolName + ":*"); 

您也可以只添加两个字段指数:通过合并查询

index.add(node, "schoolName", studentId); 
index.add(node, "id", studentId); 

,然后对它们进行查询

Iterable<Node> pupils = index.query("schoolName:"+schoolName + " AND id:"+id); 

第一个索引尺寸较小,但第二个更强大。 表现明智,它不会造成如此大的差异(但你可以测试它并报告回来)。

你也可以使用一个结构,在一所学校的一个节点,学生由LEARNS_AT关系也可以有一个startend时间属性附加给它的图形,所以很容易到你的域模型。看到这个demo graph

+0

哦,哇,一个控制台!谢谢你,没有意识到它的存在。我对此很新,我会检查一下。 – 2012-04-27 08:57:03