我正在尝试根据2种条件来过滤图形中的路径:
- 如果我指定的所有节点都属于路径节点
- 如果我指定的所有关系都属于路径关系。
这是我正在运行的Cypher查询:
MATCH path=(`GROUP1`: TRAVEL_PLAN { name: 'GROUP1' })-[*]->(e1)
WITH ['USE', 'HAS', 'ALLOW'] as foundRels, ['LODGING', 'R1'] as foundNodes, path
WHERE ANY (rel in relationships(path) WHERE ALL (foundRel in foundRels WHERE type(rel) CONTAINS foundRel))
AND ANY (pathNode in nodes(path) WHERE ALL (foundNode in foundNodes WHERE pathNode.name CONTAINS foundNode))
RETURN apoc.path.elements(path) as pathElements
但是问题是我得到了一个空的结果集,其中有0条记录。
如上所述,我想使用给定的关系和给定的节点专注于上述路径,但是我不能简单地这样做:
MATCH path=(`GROUP1`: TRAVEL_PLAN { name: 'GROUP1' })-[*]->(`LODGING`: TRAVEL_ENTITLEMENT { name: 'LODGING' })-[*]->(`R1`: RULE { name: 'R1' })-[*]->(`RS 3000` : MONEY { name: `RS 3000` })
RETURN path
因为我正在动态接收关系和节点,所以我要进行可变长度的扩展并查找所有路径,例如:
MATCH path=(`GROUP1`: TRAVEL_PLAN { name: 'GROUP1' })-[*]->(e1)
RETURN path
我不知道查询是否需要修改或我的方法有问题,因为问题是我无法事先明确地匹配路径,因为我不知道{{1 }}子句,因此,我尝试的最佳选择是从一个节点尝试扩展所有路径并根据上述2个条件对其进行过滤。
请向我建议一些基于此的替代方法,或者采用一种优雅的方式来做到这一点,以便获得屏幕快照中概述的路径。
这是在需要时可以重新创建图形的示例数据。
MATCH
我还在Neo4j中制作了一个short sandbox
,以复制我的问题。
这些是凭据:
MERGE (`BANGALORE`: LOCATION { name: 'BANGALORE' })
ON CREATE SET
`BANGALORE`.id = randomUUID(),
`BANGALORE`.type = 'LOCATION',
`BANGALORE`.created = timestamp()
ON MATCH SET
`BANGALORE`.lastUpdated = timestamp()
MERGE (`GROUP1`: TRAVEL_PLAN { name: 'GROUP1' })
ON CREATE SET
`GROUP1`.id = randomUUID(),
`GROUP1`.type = 'TRAVEL_PLAN',
`GROUP1`.created = timestamp()
ON MATCH SET
`GROUP1`.lastUpdated = timestamp()
MERGE (`LODGING`: TRAVEL_ENTITLEMENT { name: 'LODGING' })
ON CREATE SET
`LODGING`.id = randomUUID(),
`LODGING`.type = 'TRAVEL_ENTITLEMENT',
`LODGING`.created = timestamp()
ON MATCH SET
`LODGING`.lastUpdated = timestamp()
MERGE (`BOARDING`: TRAVEL_ENTITLEMENT { name: 'BOARDING' })
ON CREATE SET
`BOARDING`.id = randomUUID(),
`BOARDING`.type = 'TRAVEL_ENTITLEMENT',
`BOARDING`.created = timestamp()
ON MATCH SET
`BOARDING`.lastUpdated = timestamp()
MERGE (`R1`: RULE { name: 'R1' })
ON CREATE SET
`R1`.id = randomUUID(),
`R1`.type = 'RULE',
`R1`.created = timestamp()
ON MATCH SET
`R1`.lastUpdated = timestamp()
MERGE (`R2`: RULE { name: 'R2' })
ON CREATE SET
`R2`.id = randomUUID(),
`R2`.type = 'RULE',
`R2`.created = timestamp()
ON MATCH SET
`R2`.lastUpdated = timestamp()
MERGE (`RS 3000`: MONEY { name: 'RS 3000' })
ON CREATE SET
`RS 3000`.id = randomUUID(),
`RS 3000`.type = 'MONEY',
`RS 3000`.created = timestamp()
ON MATCH SET
`RS 3000`.lastUpdated = timestamp()
MERGE (`RS 4000`: MONEY { name: 'RS 4000' })
ON CREATE SET
`RS 4000`.id = randomUUID(),
`RS 4000`.type = 'MONEY',
`RS 4000`.created = timestamp()
ON MATCH SET
`RS 4000`.lastUpdated = timestamp()
----------------------------------------------------------------------------------------------------
CREATE INDEX ON :LOCATION(name)
CREATE INDEX ON :TRAVEL_PLAN(name)
CREATE INDEX ON :TRAVEL_ENTITLEMENT(name)
CREATE INDEX ON :RULE(name)
CREATE INDEX ON :MONEY(name)
----------------------------------------------------------------------------------------------------
MATCH (`BANGALORE`: LOCATION { name: 'BANGALORE' })
MATCH (`GROUP1`: TRAVEL_PLAN { name: 'GROUP1' })
MATCH (`LODGING`: TRAVEL_ENTITLEMENT { name: 'LODGING' })
MATCH (`BOARDING`: TRAVEL_ENTITLEMENT { name: 'BOARDING' })
MATCH (`R1`: RULE { name: 'R1' })
MATCH (`R2`: RULE { name: 'R2' })
MATCH (`RS 3000`: MONEY { name: 'RS 3000' })
MATCH (`RS 4000`: MONEY { name: 'RS 4000' })
CREATE (`GROUP1`)-[:_USE_]->(`LODGING`)
CREATE (`GROUP1`)-[:_USE_]->(`BOARDING`)
CREATE (`LODGING`)-[:_PROVIDE_IN]->(`BANGALORE`)
CREATE (`BOARDING`)-[:_PROVIDE_IN]->(`BANGALORE`)
CREATE (`LODGING`)-[:_HAS_RULE]->(`R1`)
CREATE (`BOARDING`)-[:_HAS_RULE]->(`R2`)
CREATE (`R1`)-[:_ALLOW_FOR]->(`RS 3000`)
CREATE (`R2`)-[:_ALLOW_FOR]->(`RS 4000`)
----------------------------------------------------------------------------------------------------