Neo4j 2.3 引入了在范围查找中使用模式标签索引的功能。在 Cypher 中使用此功能很简单,例如:
MATCH (n:SomeLabel) WHERE n.prop > 200 AND n.prop < 300
正如预期的那样,查询将使用 SomeLabel(prop) 的索引。
我的问题是,有没有办法用标准的 Neo4j Java API 复制它?我可以使用 GraphDatabaseService.findNodes 进行单值模式索引查找,但我没有看到任何允许范围查询的方法。
我知道我可以使用 Java API 运行 Cypher 查询来完成此操作,但由于我的项目仅使用低级 Java API,所以我想避免这种情况并尽可能保持一致。
最佳答案
根据 Neo4j's documentation您无法使用属性范围找到节点。
所以,为了做你想做的,你可以匹配每个具有所需标签的节点,并在 Java 端检查值范围:
假设 gdb
是您的 GraphDatabaseService,Labels
您的标签枚举:
ResourceIterator<Node> nodes = gdb.findNodes(Labels.SomeLabel);
Set<Node> result = new HashSet<Node>();
while(nodes.hasNext()){
Node n = nodes.next();
// I cast only to ensure I really get an integer
int prop = Integer.valueOf(n.getProperty("prop").toString());
if(prop > 200 && prop < 300){
result.add(n);
}
}
//And here you can return your Set, or do whatever you want with it
关于java - Neo4j Java API - 架构索引范围查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33711643/