如何检查密码中节点的类型?

时间:2017-06-29 21:53:24

标签: neo4j cypher

我有一个包含三种节点类型的图:NodeX,NodeY和NodeZ

我有这个密码查询:

MATCH (x:NodeX)-[*]->(d)  
WHERE x.Name = 'pqr'  
RETURN x,d;

这里(d)可以是NodeY或NodeZ。 我想分别处理不同的nodetypes。类似的东西:

MATCH (x:NodeX)-[*]->(d)  
WHERE x.Name = 'pqr'  
WITH d
CASE WHEN typeof(d)=NodeY THEN {MATCH (y:NodeY)-[*]-(z:NodeZ)}
     WHEN typeof(d)=NodeZ THEN {MATCH (z:NodeZ)-[*]-(y:NodeY)}
RETURN y,z

yz对应d。这可能吗?

1 个答案:

答案 0 :(得分:8)

节点有"标签"而不是"类型" (后一个术语仅适用于关系)。

要获取节点的标签,您可以使用LABELS()功能。因此,要测试节点n是否具有标签Foo,您可以在Cypher中执行类似的操作:

CASE WHEN 'Foo' IN LABELS(n) THEN ... 

但是,CASE子句不能包含MATCH子句。

[EDITED]

在您的具体情况下,此查询(假设您d NodeY可能只有NodeZMATCH (x:NodeX)-[*]->(d) WHERE x.Name = 'pqr' WITH d, CASE WHEN 'NodeY' IN LABELS(d) THEN 'NodeZ' ELSE 'NodeY' END AS otherLabel MATCH (d)-[*]-(other) WHERE otherLabel IN LABELS(other) RETURN d, other; 的唯一标签可能有效)

 info it worked if it ends with ok
1 verbose cli [ '/usr/bin/node', '/usr/bin/npm', 'install', 'web3' ]
2 info using npm@5.0.4
3 info using node@v8.1.3
4 verbose npm-session 02c74f27df35fda1
5 silly install loadCurrentTree
6 silly install readLocalPackageData
7 http fetch GET 304 https://registry.npmjs.org/web3 236ms (from cache)
8 silly pacote tag manifest for web3@latest fetched in 269ms
9 verbose stack TypeError: Path must be a string. Received undefined
9 verbose stack     at assertPath (path.js:28:11)
9 verbose stack     at Object.relative (path.js:1248:5)
9 verbose stack     at computeVersionSpec (/usr/lib/node_modules/npm/lib/install/deps.js:282:27)
9 verbose stack     at Installer.normalizeCurrentTree (/usr/lib/node_modules/npm/lib/install.js:370:69)
9 verbose stack     at Array.<anonymous> (/usr/lib/node_modules/npm/node_modules/slide/lib/bind-actor.js:15:8)
9 verbose stack     at LOOP (/usr/lib/node_modules/npm/node_modules/slide/lib/chain.js:15:14)
9 verbose stack     at /usr/lib/node_modules/npm/node_modules/slide/lib/chain.js:18:7
9 verbose stack     at iferr (/usr/lib/node_modules/npm/lib/install.js:328:5)
9 verbose stack     at /usr/lib/node_modules/npm/node_modules/iferr/index.js:13:50
9 verbose stack     at cb (/usr/lib/node_modules/npm/node_modules/slide/lib/async-map.js:47:24)
9 verbose stack     at /usr/lib/node_modules/npm/node_modules/call-limit/call-limit.js:22:12
9 verbose stack     at _combinedTickCallback (internal/process/next_tick.js:95:7)
9 verbose stack     at process._tickCallback (internal/process/next_tick.js:161:9)
10 verbose cwd /home/username
11 verbose Linux 4.11.7-1-ARCH
12 verbose argv "/usr/bin/node" "/usr/bin/npm" "install" "web3"
13 verbose node v8.1.3
14 verbose npm  v5.0.4
15 error Path must be a string. Received undefined
16 verbose exit [ 1, true ]