Search code examples
javaapineo4jevaluator

Neo4j Using a custom evaluator to get the depth a node was retrieved at


I'm using the latest version of Neo4j to build up a graph of nodes and relationships with the Java API.

My problem is that I need to traverse the nodes to a certain depth. There may exist a relationship between two nodes at the same depth in the database, but I don't want to return that relationship in the traversal.

I tried to make a custom Evaluator by implementing the Evaluator interface but the only method it overrides is Evaluation evaluate(Path path) . It doesn't appear to have the notion of depth associated with it.

I would really appreciate some advice on how to either associate a node with its depth (when traversing from a particular node) or prune a relationship where two nodes are in the same level.


Solution

  • You can use Evaluators.atDepth() to get a predefined Evaluator that only includes paths with a certain depth.

    In your custom evaluator you can simply check the length of the passed path parameter to decide if you want to include this path or not e.g. with:

    Evaluation evaluate(Path path) {
        return path.length() == 4 ? Evaluation.INCLUDE_AND_PRUNE : Evaluation.EXCLUDE_AND_CONTINUE);
    }