Pregunta

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.

¿Fue útil?

Solución

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);
}

Otros consejos

Have you tried Cypher for that, something like

start n = node(1) match p=n-[*4]->(x) return x, length(p) 

?

The path has a length(), which is the depth. The length is equal to the number of relationships in the path, i.e. number of nodes - 1.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top