美文网首页
NEO4j的多标签、多关系查询

NEO4j的多标签、多关系查询

作者: thirsd | 来源:发表于2022-01-28 16:39 被阅读0次

对于节点的多节点匹配方案:


// 节点的多类型:“和” 场景
CREATE (n:HOST:SERVICE{name:"test"}) return n;
MATCH (s:SERVICE:INSTANCE) return s;
MATCH (n:HOST) where n:HOST and n:SERVICE return n;
MATCH (n:HOST) where "HOST" in labels(n) and "SERVICE" in labels(n) return n;
 MATCH (n:HOST) where ["HOST", "SERVICE"] = labels(n) return n;


// 节点的多类型:“或”场景
MATCH (n:HOST) where n:HOST or n:SERVICE return n;
MATCH (n:HOST) where "HOST" in labels(n) or "SERVICE" in labels(n) return n;
MATCH (n:HOST) where  labels(n) in ["HOST", "SERVICE"] return n;
MATCH (n:HOST) where  any(l in labels(n) where l in ["HOST", "SERVICE"]) return n;
MATCH (n:HOST) with collect(distinct n) as c1   
MATCH (s:SERVICE)  with collect(distinct s) + c1 as c2
return c2;

对于关系的多类型查询:


// 关系的多类型:“或” 场景
match p=shortestpath((s:SERVICE{name:"ecms-proc-ext"})-[:ROUTE_TO|ROUTE_WITH*1..5]-(s1:SERVICE{name:"aut"})) return p

MATCH (a:PERSON)-[:LIKE]->(b:ANIMAL), (a)-[:FEED]->(b) return a,b;
MATCH (d:HOST)-[r]-(e:COMPONENT) where type(r) in ['DEPLOY_ON', 'RUN_ON'] return d,e
同多节点类似,不再赘述

对于cypher 语法,建议参考:Neo4j Cypher Refcard 4.4

相关文章

网友评论

      本文标题:NEO4j的多标签、多关系查询

      本文链接:https://www.haomeiwen.com/subject/gqltkrtx.html