4

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

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

第一次通过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)会有多个值作为结果,我将不得不迭代并编写另一个查询

我们不能在单个查询中完成这项工作,因为我有如此多的连接关系,以至于一次又一次地迭代变得越来越困难。任何帮助都将不胜感激。

4

3 回答 3

5

使用 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
于 2013-09-11T14:13:57.717 回答
0

关于多个节点或重复节点的问题。我明白你的意思。这是我用这样的查询来清除重复项所做的事情。更多关于 if a KNOWS b which KNOWS c, 但 c 真的是 a。有点像。我们可以使用类似 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
于 2014-02-04T21:57:46.327 回答
-1

如果您提出查询

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

r(x)返回有关该关系的相应详细信息。每个与查询匹配的路径都会有一个“行”。

如果您定义您的反序列化器以便它识别r(x)并构建关系而不是实体,那么您应该能够在一个查询中完成所有操作。

于 2014-03-12T02:59:45.360 回答