首页 > 解决方案 > 在节点属性上迭代 Neo4j 图匹配

问题描述

例如,我在 Neo4j 中有以下图表

(startnode)-[:BELONG_TO]-(Interface)-[:IS_CONNECTED]-(Interface)-[:BELONG_TO]-

#the line below can repeat itself 0..n times
(node)-[:BELONG_TO]-(Interface)-[:IS_CONNECTED]-(Interface)-[:BELONG_TO]-
#up to the endnode
(endnode)

我还需要匹配一个接口属性。我不想遵循所有路径,我只是我正在寻找的具有接口节点属性的路径。例如 Interface.VlanList CONTAINS ",23,"

我在 Cypher 中完成了以下操作,但它适用于我已经知道要找到多少次迭代,而实际上并非如此。

match (n:StartNode {name:"device name"}) -[:BELONG_TO]- (i:Interface) -[:IS_CONNECTED]- (ii:Interface)-[:BELONG_TO]-(nn:Node) -[:BELONG_TO]- (iii:Interface) -[:IS_CONNECTED]- (iiii:Interface) -[:BELONG_TO]-(nnn:Node)
where i.VlanList CONTAINS ",841,"
AND ii.VlanList CONTAINS ",841,"
AND iii.VlanList CONTAINS ",841,"
return n, i,ii,nn,iii,iiii,nnn

我一直在查看文档,但无法弄清楚如何解决上述问题。

标签: neo4jcypher

解决方案


这应该有效:

// put the searchstring in a variable
WITH ',841,' AS searchstring

// look up start end endnode
MATCH (startNode: .... {...}), (endNode:  .... {...})

// look for paths of variable length
// that have your search string in all nodes, 
// except the first and the last one

WITH searchstring,startNode,endNode
MATCH path=(startnode)-[:BELONG_TO|IS_CONNECTED*]-(endnode)
WHERE ALL(i IN nodes(path)[1..-1] WHERE i.VlanList CONTAINS searchstring)
RETURN path

您还可以查看https://neo4j.com/labs/apoc/4.1/graph-querying/path-expander/了解有关如何限制寻路的更多想法。


推荐阅读