我想检索连接到节点的所有节点和关系。

我试图通过两种方式做到这一点:

1st 通过 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"
我们不能在不再次点击链接的情况下直接显示节点及其关系而不是链接到关系的数据吗????

第二个 我尝试做的事情是从密码查询中获取:
START a=node(3)
MATCH (a)-[:KNOWS]->(b)-[:KNOWS]->(c)-[:KNOWS]->(d)
RETURN a,b,c,d

但这也不起作用,因为在 (b)(c) 将有多个值作为结果,我将不得不迭代并编写另一个查询

我们不能在单个查询中完成这项工作,因为我有太多的连接关系,以至于很难一次又一次地迭代。任何帮助都会受到赞赏。

最佳答案

使用 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

关于java - Neo4j : Retrieving All Nodes and Relationship connected to a Node in Neo4j Rest OR through Cypher,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18741918/

10-16 06:24