gpt4 book ai didi

java - Neo4j : Retrieving All Nodes and Relationship connected to a Node in Neo4j Rest OR through Cypher

转载 作者:搜寻专家 更新时间:2023-11-01 02:47:32 29 4
gpt4 key购买 nike

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

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

第 1 通过 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com