简体   繁体   English

在Neo4j中编制索引

[英]Indexing in Neo4j

I'm wonderring what's a better approach when needing to have multiple indecies based on some node type or field. 我想知道当需要基于某个节点类型或字段的多个indecies时,什么是更好的方法。 For example, let's say I want to have a graph of students and want to index them by their school and id. 例如,假设我想要一个学生图表,并希望按照他们的学校和身份对其进行索引。

As I understand I can have an index per school like this: 据我所知,每个学校都可以有这样的索引:

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

I can on the other hand use one index and do something like: 另一方面,我可以使用一个索引并执行以下操作:

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

What is a better approach? 什么是更好的方法? Any advantages to one over the other? 一个优于另一个的任何优势? Especially performance wise or storage wise, when there are a lot of nodes involved. 特别是在性能方面或存储方面,当涉及很多节点时。

Thanks 谢谢

Your approach is perfectly valid. 你的方法是完全有效的。 If you want to query all students of a school you can use: 如果您想查询学校的所有学生,您可以使用:

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

You can also just add both fields to the index: 您也可以将两个字段添加到索引中:

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

and then query them by a combined query 然后通过组合查询查询它们

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

The first one is smaller in index size but the second one is more powerful. 第一个是索引大小较小但第二个更强大。 Performance wise it won't make such a big difference (but you can test it and report back). 性能方面它不会产生如此大的差异(但你可以测试并报告)。

You could also use an structure in the graph where a school is a node and the pupils are attached to it by a LEARNS_AT relationship which can also have a start and end temporal property, so it is easier to model your domain. 您还可以在图形中使用一个结构,其中学校是一个节点,并且学生通过LEARNS_AT关系附加到它上面,该关系也可以具有startend时间属性,因此更容易对您的域建模。 See this demo graph 请参阅此演示图

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

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