Search code examples
neo4jneo4j-java-api

Neo4J Java findNodes with other than a single string match


I'm successfully using the Neo4J Java API (currently version 2.2.1) to do simple queries like this:

Label label = DynamicLabel.label("User");
ResourceIterator<Node> providers = graphDb.findNodes(
        label, "username", "player1"));

But is there a way to allow something other than a simple match (String in my case) for the values? Can I do REGEX, or provide a list of possible values that the key could match on for string comparisons? If so, are there any examples out there?

I've dug around the docs and my favorite search engines and can't seem to find anything other than a straight string match (such as this: http://neo4j.com/docs/2.2.1/tutorials-java-embedded-new-index.html).


Solution

  • You'll rapidly find you might want to execute cypher queries from inside of java to do this kind of querying.

    String query = "MATCH (n:Label) where n.username =~ ".*foo regex.*" RETURN n;";
    try ( Transaction ignored = db.beginTx();
          Result result = db.execute(query) )
    {
        while ( result.hasNext() )
        {
             // Do nifty stuff with results.
        }
    }
    

    The regular embedded API methods you're using aren't going to support regex or a great deal of other things, I'd suggest using cypher except for very simple cases (like "get node by property and value")