更复杂的Cypher查询作为TraversalDescription

时间:2013-04-29 12:06:41

标签: neo4j cypher

我不知道,如何在Java中将以下cypher查询构建为TraversalDescription:

START container=node(startContainerId), condition=node(startConditionId) MATCH container-[:CONTAINS*]->items-[*]->condition WHERE items.type! = 'instance' RETURN items

我只能意识到第一部分,在给定的“容器”中获取所有“项目”:

TraversalDescription td = Traversal.description().depthFirst()
                .relationships(Neo4JRelTypes.CONTAINS, Direction.OUTGOING).evaluator(new Evaluator() {

                    @Override
                    public Evaluation evaluate(Path path) {
                        if (path.endNode().getProperty("type").equals("instance")) {
                            return Evaluation.INCLUDE_AND_CONTINUE;
                        } else {
                            return Evaluation.EXCLUDE_AND_CONTINUE;
                        }
                    }
                });

但是如何使用与特定节点匹配的传出关系过滤这些“项目”? 我想测试这个cypher查询和类似的TraversalDescription之间的性能。

感谢您的帮助。 最好的问候,Max

1 个答案:

答案 0 :(得分:2)

我在想:

    private static class MyExpander implements PathExpander
    {
        private final boolean forward;

        MyExpander( boolean forward )
        {
            this.forward = forward;
        }

        @Override
        public Iterable expand( Path path, BranchState state )
        {
            boolean instance = state.getState();
            if ( !instance && (instance = "instance".equals( path.endNode().getProperty( "type", null )) ) )
                state.setState( instance );
            Direction direction = forward ? OUTGOING : INCOMING;
            return (forward ? instance : !instance) ?
                    path.endNode().getRelationships( direction ) :
                    path.endNode().getRelationships( CONTAINS, direction );
        }

        @Override
        public PathExpander reverse()
        {
            return new MyExpander( !forward );
        }
    }

    TraversalDescription side = traversal( NODE_PATH )
                .expand( new MyExpander( true ), new InitialBranchState.State( FALSE, FALSE ) );

        BidirectionalTraversalDescription traverser = bidirectionalTraversal().collisionEvaluator( new PathEvaluator.Adapter()
        {
            @Override
            public Evaluation evaluate( Path path, BranchState state )
            {
                return Evaluation.ofIncludes( state.getState() );
            }
        } ).mirroredSides( side );

虽然我注意到碰撞评估器存在问题,但导致异常。在neo4j代码库中本地修复了它。