简体   繁体   English

在java中的neo4j嵌入式数据库中,我应该如何检查两个节点是否相互关联?

[英]How should I check if two nodes have relationship with each other,in neo4j embedded database in java?

How should I check if two nodes have relationship with each other,in neo4j embedded database in java? 在java中的neo4j嵌入式数据库中,我应该如何检查两个节点是否相互关联?

I want the syntax please or a tutorial link,I have seen neo4j website but didn't find it. 我想要语法或教程链接,我已经看过neo4j网站,但没有找到它。

Thanks. 谢谢。

Given two nodes "nodeA" and "nodeB", 给定两个节点“nodeA”和“nodeB”,

  1. gets all relationships attached to "nodeA", 将所有关系附加到“nodeA”,

     rels = nodeA.getRelationships(); 
  2. iterate through the collection of relationships "rels", for each relationship "rel", test whether the other end node is nodeB 迭代关系集“rels”,对于每个关系“rel”,测试另一个端节点是否为nodeB

     rel.getOtherNode(nodeA).equals(nodeB) 
  3. if the above expression holds true for one of the relationships, then nodeA and nodeB are connected. 如果上述表达式适用于其中一个关系,则nodeA和nodeB连接。

Here is the java API for "Node" and "Relationshiip", 这是“Node”和“Relationshiip”的java API,

http://api.neo4j.org/current/ http://api.neo4j.org/current/

private boolean sharedRelationshipExists( Node nodeA, long nodeBId)
{
    Iterator<Relationship> iterator = nodeA.getRelationships().iterator();
    while ( iterator.hasNext() )
    {
        if (iterator.next().getOtherNode( nodeA ).getId() == nodeBId) return true;
    }
    return false;
}

// in another part
boolean sharedRelationshipBetweenAB;
if ( nodeA.getDegree() < nodeB.getDegree() )
{
    sharedRelationshipBetweenAB = sharedRelationshipExists( nodeA, nodeB.getId() );
}
else
{
    sharedRelationshipBetweenAB = sharedRelationshipExists( nodeB, nodeA.getId() );
}

the boolean sharedRelationshipBetweenAB will hold your answer 布尔值sharedRelationshipBetweenAB将保留你的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM