neo4j - 在节点属性上迭代 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
我一直在查看文档,但无法弄清楚如何解决上述问题。
解决方案
这应该有效:
// 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/了解有关如何限制寻路的更多想法。
推荐阅读
- sql-server - 如何在 Cloud SQL 中为 MS-SQL 配置复制(只读副本)?
- rabbitmq - 同一任务一次执行多次
- python-3.x - 更新给定 id 的 Elasticsearch 记录会产生错误 []
- python - 搜索不在列表中的内容
- azure - 具有多个条件的基于 Azure 流分析阈值的规则
- r - 使用随机森林模型预测测试数据时出错
- django - 如何以 django 形式过滤多对多字段
- python-3.x - 我正在尝试构建一个接收数据集名称的 Azure 函数,然后继续连接并从 Azure Datalake 下载文件(到本地磁盘)
- c# - 如何在比较指令 C# WPF 中使用 int.TryParse?
- python - 在 python 中使用 recfromtxt 读取百分比