Neo4j Cypher match()在java中。查找连接的节点

时间:2016-01-08 12:24:40

标签: java neo4j cypher

我有以下结构

        firstNode = graphDb.createNode();
        firstNode.setProperty( "person", "Andy " ); 
        Label myLabel = DynamicLabel.label("A");
        firstNode.addLabel(myLabel);
        secondNode = graphDb.createNode();
        secondNode.setProperty( "person", "Bobby" );
        Label myLabel1 = DynamicLabel.label("B");
        secondNode.addLabel(myLabel1);
        ThirdNode = graphDb.createNode();
        ThirdNode.setProperty( "person", "Chris " );
        Label myLabel2 = DynamicLabel.label("C");
        ThirdNode.addLabel(myLabel2);....

        relationship = firstNode.createRelationshipTo( secondNode, RelTypes.emails );
        relationship.setProperty( "relationship", "email " );
        relationship = firstNode.createRelationshipTo( ThirdNode, RelTypes.emails );
        relationship.setProperty( "relationship", "email " );
        relationship = secondNode.createRelationshipTo( ThirdNode, RelTypes.emails );
        relationship.setProperty( "relationship", "email " );
        relationship = secondNode.createRelationshipTo( FourthNode, RelTypes.emails );
        relationship.setProperty( "relationship", "email " );

firstNode通过关系“emails”链接到第二个和第三个。类似地,第二节点连接到第三,第四,第一节点。

我希望每个节点输出类似于:secondNode = [firstNode,FouthNode,ThirdNode],firstNode = [second,third],third = ...

我试过这样的事情:

try{
        ExecutionEngine engine = new ExecutionEngine(graphDb);
        ExecutionResult result = engine.execute("MATCH (secondNode{person:'Bobby'})<-[:emails]-(node)RETURN node");

        System.out.println(result.dumpToString());
        tx1.success();
    } 

我得到了输出:Node[0]{person:"Andy "}

我对cypher很新。如何为此写匹配语句?这可能吗?

1 个答案:

答案 0 :(得分:1)

  • 您的标签应该是这样的:人不是:A,:B,:C
  • 您希望按第一个节点聚合。
  • 您应该使用大写重新类型

尝试这样的事情:

MATCH (sender:Person)-[:EMAILS]->(receiver) 
RETURN sender,collect(receiver) as receivers