Cypher Query:返回可变长度

时间:2015-11-19 11:26:04

标签: neo4j cypher

用户, 我试图接收所有路径(例如,长度< 3),打印节点标题,但也打印关系类型。

使用:

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), p = (p0)-[r*..2]-(p1)
RETURN p AS path

打印路径,但关系表示为{}

因为我正在使用* .. 2,

`RETURN p,type(r)'

不起作用(类型不匹配:预期的关系,但是收集)

我可以使用类似的解决方法:

MATCH
  (p0:Page {title:'New Zealand'}),
  (p1:Page {title:'Kiwi'}),
  p = (p0)-[r]-(p3)-[r1]-(p1)
RETURN p0,type(r),p3,type(r1)

但随着路径长度的增加,我会对每个路径长度执行一次查询。

我试过了:

MATCH
  (p0:Page {title:'New Zealand'}),
  (p1:Page {title:'Kiwi'}),
  p = (p0)-[r*..2]-(p1) FOREACH(r in p | RETURN p,p0,type(r),p1)

- >预期收藏但是道路

有人有提示吗?

来自Neo4j界面的附加信息JSON输出(代码段):

    {
  "results": [
    {
      "columns": [
        "p",
        "rels(p)",
        "nodes(p)"
      ],
      "data": [
        {
          "row": [
            [
              {
                "title": "New Zealand"
              },
              {},
              {
                "title": "Category:Birds of New Zealand"
              },
              {},
              {
                "title": "Kiwi"
              }
            ],
            [
              {},
              {}
            ],
            [
              {
                "title": "New Zealand"
              },
              {
                "title": "Category:Birds of New Zealand"
              },
              {
                "title": "Kiwi"
              }
            ]
          ],
          "graph": {
            "nodes": [
              {
                "id": "11120",
                "labels": [
                  "Page"
                ],
                "properties": {
                  "title": "Kiwi"
                }
              },
              {
                "id": "1942858",
                "labels": [
                  "Page"
                ],
                "properties": {
                  "title": "New Zealand"
                }
              },
              {
                "id": "11994493",
                "labels": [
                  "Category"
                ],
                "properties": {
                  "title": "Category:Birds of New Zealand"
                }
              }
            ],
            "relationships": [
              {
                "id": "1070940",
                "type": "To_Category",
                "startNode": "11120",
                "endNode": "11994493",
                "properties": {}
              },

2 个答案:

答案 0 :(得分:3)

您只需使用extract来提取路径内的关系类型。

基于http://console.neo4j.org上的简单电影图:

MATCH p=(:Crew { name:"Neo" })-[r*3]-()
RETURN p, extract(x IN rels(p)| type(x)) AS types

您的查询将是:

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), 
p=(p0)-[r*..2]-(p1)
RETURN p, extract (rel in rels(p) | type(rel) ) as types

这将返回一系列类型:

[爱,知道,知道]

所以你可以有重复。如果您需要重复删除它们,请使用UNWIND和distinct:

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), 
p=(p0)-[r*..2]-(p1)
WITH p, extract (rel in rels(p) | type(rel) ) as types
UNWIND types as t
RETURN p, collect(distinct t) as types

<强>更新

我对最后一个感到很开心,所以这里有一个更简单的方法:

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), 
p=(p0)-[r*..2]-(p1)
UNWIND rels(p) as rel
RETURN p, collect(distinct type(rel)) as types

答案 1 :(得分:1)

使用rels功能可以帮助您吗?

MATCH (p0:Page {title:'New Zealand'}), (p1:Page {title:'Kiwi'}), p = (p0)-[r*..2]-(p1)
RETURN p, rels(p), nodes(p)
相关问题