繁体   English   中英

在Neo4j中编制索引

[英]Indexing in Neo4j

我想知道当需要基于某个节点类型或字段的多个indecies时,什么是更好的方法。 例如,假设我想要一个学生图表,并希望按照他们的学校和身份对其进行索引。

据我所知,每个学校都可以有这样的索引:

// 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();

什么是更好的方法? 一个优于另一个的任何优势? 特别是在性能方面或存储方面,当涉及很多节点时。

谢谢

你的方法是完全有效的。 如果您想查询学校的所有学生,您可以使用:

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时间属性,因此更容易对您的域建模。 请参阅此演示图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM