Neo4j:在Neo4j Rest或Cypher中检索与节点连接的所有节点和关系

4

我希望您能翻译以下与IT技术有关的内容:检索与一个节点相连的所有节点和关系。我尝试通过两种方式实现:

第一种方式 通过Neo4j REST API,我尝试了以下方法:

URI traverserUri = new URI( startNode.toString() + "/traverse/node" );
WebResource resource = Client.create()
        .resource( traverserUri );
String jsonTraverserPayload = t.toJson();
ClientResponse response = resource.accept( MediaType.APPLICATION_JSON )
        .type( MediaType.APPLICATION_JSON )
        .entity( jsonTraverserPayload )
        .post( ClientResponse.class );

System.out.println( String.format(
        "POST [%s] to [%s], status code [%d], returned data: "
                + System.getProperty( "line.separator" ) + "%s",
        jsonTraverserPayload, traverserUri, response.getStatus(),
        response.getEntity( String.class ) ) );
response.close();

并获取以下响应:
[ {
  "outgoing_relationships" : "http://localhost:7474/db/data/node/82/relationships/out",
  "data" : {
    "band" : "The Clash",
    "name" : "Joe Strummer"
  },
  "traverse" : "http://localhost:7474/db/data/node/82/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/82/relationships/all/{-list|&|types}",
  "property" : "http://localhost:7474/db/data/node/82/properties/{key}",
  "all_relationships" : "http://localhost:7474/db/data/node/82/relationships/all",
  "self" : "http://localhost:7474/db/data/node/82",
  "properties" : "http://localhost:7474/db/data/node/82/properties",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/82/relationships/out/{-list|&|types}",
  "incoming_relationships" : "http://localhost:7474/db/data/node/82/relationships/in",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/82/relationships/in/{-list|&|types}",
  "create_relationship" : "http://localhost:7474/db/data/node/82/relationships"
}, {
  "outgoing_relationships" : "http://localhost:7474/db/data/node/83/relationships/out",
  "data" : {
  }]

但问题是,如果我想再次查看此节点的关系,我必须点击链接 "http://localhost:7474/db/data/node/82/relationships/all"

我们不能直接获取显示节点及其关系的数据,而不是再次点击链接以获取关系吗?

第二个 我尝试从Cypher查询中获取这个:

START a=node(3)
MATCH (a)-[:KNOWS]->(b)-[:KNOWS]->(c)-[:KNOWS]->(d)
RETURN a,b,c,d

但是这也行不通,因为在 (b)(c) 会有多个值作为结果,需要迭代并编写另一个查询。
难道我们不能用单个查询来完成吗?因为我有很多连接关系,反复迭代变得越来越困难。任何帮助将不胜感激。
3个回答

5

使用Cypher很容易获取与给定节点相连的所有节点

START a=node(3)
MATCH (a)-[:KNOWS*]->(d)
RETURN distinct d

但是,如果您有大量连接的节点和深层次的连接,则可能无法获得良好的性能。

如果您知道连接的边界,则在查询中明确指定它对于性能会很有帮助,

START a=node(3)
MATCH (a)-[:KNOWS*1..3]->(d)
RETURN Distinct d

是的,你说得对,我可以这样做,但如果(a)-[:KNOWS]->(b)-[:KNOWS]->(c)-[:KNOWS]->(d),并且我们得到多个节点(b)和(c)怎么办?我们将不得不为每个节点编写单独的查询,难道不能在一个查询中完成吗? - Shiv
我不确定我理解你的关注点。"Match (a)-[:KNOWs*1..3]-b" 将返回所有与 a 有 1、2 或 3 条连接的节点。例如,如果你有 a->b1、a->b2 和 a->b3,则 "Match a-[:KNOWS]-b" 将匹配这三条路径,并返回 b1、b2 和 b3。对吗? - Lisa Li

0

关于多个节点或重复节点的问题,我明白你的意思。这是我使用这样的查询消除重复内容的方法。更多关于如果a认识b,b认识c,但实际上c就是a。就像这样。我们可以使用类似WHERE NOT的东西。

start player=node({0})
          match player-[:player.active*2..2]-friendsOfFriends,
          where not(player-[:player.active]-friendsOfFriends) and player <>     friendsOfFriends
          return distinct friendsOfFriends
          order by friendsOfFriends.username asc

-1

如果您进行查询

MATCH (a)-[r1:KNOWS]->(b)-[r2:KNOWS]->(c)-[r3:KNOWS]->(d) RETURN a,r1,b,r2,c,r3,d;

r(x)将返回有关关系的相应详细信息。对于与查询匹配的每个路径,都会有一行。

如果您定义反序列化程序以识别r(x)并构建关系而不是实体,则应该能够在一个查询中完成所有操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接