本文介绍了Neo4J TraversalDescription定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个 TraversalDescription ,它将执行以下搜索;

I am trying to create a TraversalDescription that will perform the following search;


  • 仅返回指向的节点有一定的属性(type==PERSON)

  • 返回一定数量的结果(整个图表很大,我们只对本地图表感兴趣)

  • 可以使用任何关系类型

我没有设法走得很远,我可以似乎没有弄清楚如何为节点属性创建一个Evaluator;

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(?...);


推荐答案

我通过简单地实现Evaluator接口修复了这个问题,覆盖 Evaluator.evaluate(Path p)方法;

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

然后我的 TraversalDescription 定义最终看起来像:

And then my TraversalDescription definition ends up looking like:

TraversalDescription td = Traversal.description().breadthFirst().evaluator(new MyEvaluator(peopleRequired));

这篇关于Neo4J TraversalDescription定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-15 17:33