【问题标题】:Indexing in Neo4jNeo4j 中的索引
【发布时间】:2012-05-07 17:56:11
【问题描述】:

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

据我了解,我可以像这样为每所学校建立一个索引:

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

什么是更好的方法? 一个比另一个有什么优势? 当涉及到很多节点时,尤其是性能方面或存储方面。

谢谢

【问题讨论】:

    标签: java indexing neo4j


    【解决方案1】:

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

    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

    【讨论】:

    • 哇哦,一个控制台!谢谢你,不知道它的存在。我对此很陌生,我会检查一下。
    猜你喜欢
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-25
    • 2013-09-12
    相关资源
    最近更新 更多