Graph neo4j单传递图但多个匹配

Graph neo4j单传递图但多个匹配,graph,neo4j,cypher,Graph,Neo4j,Cypher,我在neo4j中有一个顶点为: person:ID,name,value:int,:LABEL 1,Alice,1,Person 2,Bob,0,Person 3,Charlie,0,Person 4,David,0,Person 5,Esther,0,Person 6,Fanny,0,Person 7,Gabby,0,Person 8,XXXX,1,Person 和边缘: :START_ID,:END_ID,:TYPE 1,2,call 2,3,text 3,2,text 6,3,text

我在neo4j中有一个顶点为:

person:ID,name,value:int,:LABEL
1,Alice,1,Person
2,Bob,0,Person
3,Charlie,0,Person
4,David,0,Person
5,Esther,0,Person
6,Fanny,0,Person
7,Gabby,0,Person
8,XXXX,1,Person
和边缘:

:START_ID,:END_ID,:TYPE
1,2,call
2,3,text
3,2,text
6,3,text
5,6,text
5,4,call
4,1,call
4,5,text
1,5,call
1,8,call
6,8,call
6,8,text
8,6,text
7,1,text
导入neo4j,如:

DATA_DIR_SAMPLE=/data_network/
$NEO4J_HOME/bin/neo4j-admin import --mode=csv \
  --database=graph.db \
  --nodes:Person ${DATA_DIR_SAMPLE}/vertices.csv \
  --relationships ${DATA_DIR_SAMPLE}/edges.csv
这看起来像:

现在在查询图形时,如:

MATCH (source:Person)-[*1]-(destination:Person)
RETURN source.name, source.value, avg(destination.value), 'undir_1_any' as type
UNION ALL
MATCH (source:Person)-[*2]-(destination:Person)
RETURN source.name, source.value, avg(destination.value), 'undir_2_any' as type
您可以看到该图被多次遍历,另外,我希望获得如下表:

Vertex | value | type_undir_1_any | type_undir_2_any
Alice  | 1     | 0.2              |  0
需要额外的聚合步骤(透视/重塑)

将来,我想添加以下模式

  • 无向的
  • 所有关系|关系类型
  • 如图中所示,最多3个级别 所有这些的排列

有更好的方法组合查询吗?

您需要沿路径长度进行聚合,同时使用计算平均值的自定义函数:

MATCH p = (source:Person)-[*1..2]-(destination:Person)
WITH 
  length(p) as L, source, destination
RETURN 
  source.name as Vertex, 
  source.value as value, 
  1.0 * 
      sum(CASE WHEN L = 1 THEN destination.value ELSE 0 END) / 
      sum(CASE WHEN L = 1 THEN 1 ELSE 0 END) as type_undir_1_any,
  1.0 * 
      sum(CASE WHEN L = 2 THEN destination.value ELSE 0 END) /
      sum(CASE WHEN L = 2 THEN 1 ELSE 0 END) as type_undir_2_any
或者是一个更优雅的版本,该版本具有用于计算集合平均值的函数:

MATCH p = (source:Person)-[*1..2]-(destination:Person)
RETURN 
  source.name as Vertex, 
  source.value as value,
  apoc.coll.avg(COLLECT(
    CASE WHEN length(p) = 1 THEN destination.value ELSE NULL END
  )) as type_undir_1_any,
  apoc.coll.avg(COLLECT(
    CASE WHEN length(p) = 2 THEN destination.value ELSE NULL END
  )) as type_undir_2_any

伟大的这适用于解决多个级别的问题。如何将其扩展以同时处理不同类型的关系(任意、仅调用、仅文本),即生成`#depth*typeOfRelation`输出?@GeorgHeiler您可以为集合组合任何条件,包括其中的关系类型:
当长度(p)=1时的情况和所有(关系(p)中的r,其中类型(r)=“text”)然后destination.value ELSE NULL END
我明白了-但考虑到
列表(itertools.product(*[['text'、'call'、'tware']、['1'、'2'、'3'、'1..2'、'1..3']),这将很快导致无法很好地维护代码,['undirected','src->dst','src我不认为有任何其他方法可以解决它。但是学习它会很有趣。)非常感谢你的伟大回答!-我会接受它,因为它已经接近了。