Reputation: 5689
I am trying to create a TraversalDescription that will perform the following search;
I haven't managed to get very far, I can't seem to figure out how to create an Evaluator for node properties;
TraversalDescription td = Traversal.description().bredthFirst().evaluator(?...);
Upvotes: 1
Views: 1459
Reputation: 5689
I fixed this by simply implementing the Evaluator interface, and overwriting the Evaluator.evaluate(Path p) method;
public final class MyEvaluator implements Evaluator {
private int peopleCount;
private int maxPeople;
public MyEvaluator(int max) {
maxPeople = max;
peopleCount = 0;
}
public Evaluation evaluate(Path p) {
//prune if we have found the required number already
if(peopleCount >= maxPeople) return Evaluation.EXCLUDE_AND_PRUNE;
//grab the node of interest
Node n = p.endNode();
//include if it is a person
if(n.hasProperty("type") && (n.getProperty("type").equals(NodeTypes.PERSON.name()))) {
peopleCount++;
return Evaluation.INCLUDE_AND_CONTINUE;
}
// otherwise just carry on as normal
return Evaluation.EXCLUDE_AND_CONTINUE;
}
}
And then my TraversalDescription definition ends up looking like:
TraversalDescription td = Traversal.description().breadthFirst().evaluator(new MyEvaluator(peopleRequired));
Upvotes: 4
Reputation: 1144
Even when coding in Java, I'd recommend starting with a Cypher query for traversals, only dropping down into TraversalDescriptions if you really want to tweak the performance or conduct some interesting operations.
From what you've described and assuming you have the id of the start node, a Cypher query could be:
start n=node(1) match (n)-[*1..2]-(m) where m.type="Person" return distinct(m) limit 2
That would find all nodes between 1 and 2 hops away from the starting node, following any relationship type, but where the nodes have a type property set to "Person", finally returning only 2 distinct results. You can try that using an example on console (to which I've added "type" properties).
To execute that from within Java, you'd create an ExecutionEngine, provide the query, then iterate over the results as described in the Neo4j Manual.
Upvotes: 2