简体   繁体   English

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

[英]Neo4j : Retrieving All Nodes and Relationship connected to a Node in Neo4j Rest OR through Cypher

I want to Retrieve all nodes and relationship connected to a node. 我想检索连接到节点的所有节点和关系。

I Tried to do this in two ways: 我试图以两种方式做到这一点:

1st Through Neo4j REST API i Tried this 第一次通过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();

And get Following Response : 并得到以下回应:

[ {
  "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" : {
  }]

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" 但问题是,如果我想再次看到这个节点的关系,我将不得不点击链接"http://localhost:7474/db/data/node/82/relationships/all"

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

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 但这也没有用,因为在(b)(c)会有多个值作为结果,我将不得不迭代并写另一个查询

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. 任何帮助都是Appreaciated。

It's easy to get all nodes connected to a given node with Cypher 使用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

Regarding the question about multiple nodes, or duplicate nodes. 关于多个节点或重复节点的问题。 I understand what you mean. 我明白你的意思。 Here is something I did with such a query to weed out duplicates. 这是我用这样的查询做的事情,以清除重复。 More about if a KNOWS b which KNOWS c, but c is really a. 更多关于如果一个知识b知道c,但c确实是一个。 Kind of like that. 有点像那样。 We can use something like WHERE NOT 我们可以使用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

If you make your query 如果您进行查询

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

The r(x) will return the respective details regarding the relationship. r(x)将返回关于关系的相应细节。 There will be a "row" for each path that matches the query. 每个匹配查询的路径都会有一个“行”。

If you define your deserializer so it recognizes the r(x) and constructs a relationship rather than an entity then you should be able to do it all in one query. 如果您定义了反序列化程序,以便它识别r(x)并构造关系而不是实体,那么您应该能够在一个查询中完成所有操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM