Neo4j:检索连接到Neo4j Rest中的节点或通过Cypher的所有节点和关系

Shi*_*hiv 4 java neo4j

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

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

第一次通过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();
Run Code Online (Sandbox Code Playgroud)

并得到以下回应:

[ {
  "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" : {
  }]
Run Code Online (Sandbox Code Playgroud)

但问题是,如果我想再次看到这个节点的关系,我将不得不点击链接 "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
Run Code Online (Sandbox Code Playgroud)

但是这也没有用,因为at (b)(c)将会有多个值,因此我将不得不迭代并编写另一个查询

我们不能在单个查询中完成这项工作,因为我有很多连接关系,很难一次又一次地迭代.任何帮助都是Appreaciated.

小智 5

使用Cypher可以轻松地将所有节点连接到给定节点

START a=node(3)
MATCH (a)-[:KNOWS*]->(d)
RETURN distinct d
Run Code Online (Sandbox Code Playgroud)

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

如果您知道连接的边界,在查询中明确指定它将有助于提高性能,

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