Graph 如何在Neo4j中找到定义长度的现金循环圈?

Graph 如何在Neo4j中找到定义长度的现金循环圈?,graph,neo4j,transactions,cypher,Graph,Neo4j,Transactions,Cypher,我想找到像这样的圆圈a1->a2->a3->a1,其中每笔交易与前一笔交易的差异不到20%。下面的查询运行良好 我的问题是:如何通过参数化圆长度使其更通用 match p=(a1:Account { id:'123'})-[t1:Transaction]->(a2:Account)-[t2:Transaction]->(a3:Account)-[t3:Transaction]->(a1) where a1<>a2<>a3<>a1 and

我想找到像这样的圆圈a1->a2->a3->a1,其中每笔交易与前一笔交易的差异不到20%。下面的查询运行良好

我的问题是:如何通过参数化圆长度使其更通用

match p=(a1:Account { id:'123'})-[t1:Transaction]->(a2:Account)-[t2:Transaction]->(a3:Account)-[t3:Transaction]->(a1)
where a1<>a2<>a3<>a1
  and 0.8*t1.amount<t2.amount<1.2*t1.amount
  and 0.8*t2.amount<t3.amount<1.2*t2.amount
  and 0.8*t3.amount<t1.amount<1.2*t3.amount
return p
match p=(a1:Account{id:'123})-[t1:Transaction]->(a2:Account)-[t2:Transaction]->(a3:Account)-[t3:Transaction]->(a1)
其中a1a2a3a1

和0.8*t1.amount首先,您可以使用可变长度路径模式:

match (a1:Account { id:'123'}) with a1
match p = (a1)-[:Transaction*3..3]->(a1)
      where all( n in nodes(p) where 'Account' in labels(n) )
return p
其次,条件也可以移动到语句
all

match (a1:Account { id:'123'}) 
with a1
match p = (a1)-[:Transaction*3..3]->(a1)
      where all( n in nodes(p) where 'Account' in labels(n) ) 
with p, 
     relationships(p) + head(relationships(p)) as txs
     where all( i in range(1,length(p)) 
                 where 0.8*txs[i-1].amount<txs[i].amount<1.2*txs[i-1].amount
           )
return p

首先,您可以使用可变长度路径模式:

match (a1:Account { id:'123'}) with a1
match p = (a1)-[:Transaction*3..3]->(a1)
      where all( n in nodes(p) where 'Account' in labels(n) )
return p
其次,条件也可以移动到语句
all

match (a1:Account { id:'123'}) 
with a1
match p = (a1)-[:Transaction*3..3]->(a1)
      where all( n in nodes(p) where 'Account' in labels(n) ) 
with p, 
     relationships(p) + head(relationships(p)) as txs
     where all( i in range(1,length(p)) 
                 where 0.8*txs[i-1].amount<txs[i].amount<1.2*txs[i-1].amount
           )
return p

谢谢可变长度模式很简单,但您的第二个查询非常有用!特别是你如何比较数量。你能不能加上这个:a1a2a3a1在哪里?@Tom你的意思是检查所有的节点都不同吗?谢谢。可变长度模式很简单,但您的第二个查询非常有用!特别是你如何比较数量。你能不能加入这个:a1a2a3a1在哪里?@Tom你的意思是检查所有的节点都不同吗?