Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Neo4j Java API - schema index range lookup?

Tags:

java

neo4j

Neo4j 2.3 introduced the ability to use schema label indexes in range lookups. In Cypher using this feature is simple, ex:

MATCH (n:SomeLabel) WHERE n.prop > 200 AND n.prop < 300

As expected that query would use an index for SomeLabel(prop).

My question is, is there a way to replicate this with the standard Neo4j Java API? I can use GraphDatabaseService.findNodes to do a single value schema index lookup, but I don't see any method that allows for a range query.

I realize I can run a Cypher query with the Java API to accomplish this, but since my project only uses the low-level Java API I'd like to avoid that and keep it consistent, if possible.

like image 695
David Fox Avatar asked Feb 23 '26 01:02

David Fox


1 Answers

According to Neo4j's documentation you can't find a node using a property range.

So, to do what you want to do, you can match every nodes having the desired label, and check the value range on Java side:

Assuming gdb is your GraphDatabaseService, and Labelsyour labels enum:

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
like image 151
Supamiu Avatar answered Feb 25 '26 14:02

Supamiu