Neo4j:在Neo4j Rest或通过Cypher检索连接到节点的所有节点和关系 [英] Neo4j : Retrieving All Nodes and Relationship connected to a Node in Neo4j Rest OR through Cypher

查看:104
本文介绍了Neo4j:在Neo4j Rest或通过Cypher检索连接到节点的所有节点和关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to Retrieve all nodes and relationship connected to a node.

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

I Tried to do this in two ways:

第一通过 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"

But the problem is if i want to see the relationship of this node again i will have to hit the link "http://localhost:7474/db/data/node/82/relationships/all"

无法获得其中直接显示Node及其关系而不是链接至关系而不再次单击链接的数据吗??

Cant we get Data in which Node and its relationship are shown directly instead of link to relationship without hitting the link again????

第二个事情是从密码查询中获取此信息:

2nd thing I have tried to do is to get this from cypher query :

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

但这也没有用,因为在(b)(c)处将有多个值,因此我必须迭代并编写另一个查询

But this also didn't work because at (b) and (c) there will be multiple values as a result for which i will have to iterate and write another query

我们无法在单个查询中完成此操作,因为我有许多联系关系,因此很难一次又一次地进行迭代.任何帮助都会得到体现.

Cant we get this done in single query because i have so many connected relationship that it is getting hard to iterate again and again. Any Help would be Appreaciated.

推荐答案

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

It's easy to get all nodes connected to a given node with Cypher

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

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

But if you have large number of connected nodes and deep connections, you might not get a good performance.

如果您知道连接的范围,请在查询中明确指定它对性能有帮助,

If you know the bounds of the connections, specify it explicitly in the query would be helpful for performance,

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

这篇关于Neo4j:在Neo4j Rest或通过Cypher检索连接到节点的所有节点和关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆