匹配多个标签的cypher语句
发布于 6 年前 作者 bingo 8340 次浏览 来自 问答

方法:找出标签为PEOPLE1或者标签为CORPID1的节点

MATCH (a:PEOPLE1) ,(b:CORPID1) with [a,b] as m unwind m as mm
RETURN mm;

有没有更高效的写法,以上写法 一旦数据量大了。效率就很低

11 回复

可以在where里面用 label(n) in label 数组

这样会所有标签扫描,这样性能不怎么好。我用的match(n:label1) union match(n:label2)

搜索节点而不通过关系连接,相当于对节点做全组合/Cartesian Join,完全没有必要也很浪费系统资源。

两位老师。我这里主要是做算法用的。

官网标签传播算法是algo.labelPropagation 有三个参数 1:要扫描的节点Id 2.cypher语句 3.方向 4.参数

CALL algo.labelPropagation( ‘MATCH (p:User) RETURN id§ as id, p.weight as weight, id§ as value’, ‘MATCH (p1:User)-[f:FRIEND]->(p2:User)RETURN id(p1) as source, id(p2) as target, f.weight as weight’, “OUT”, {graph:‘cypher’,write:true});

官网给的例子只能用一个标签去匹配节点,实际运用中寻找团伙一般都不止一个标签。这里用union可以返回多个标签的id。避免全库扫描 直接写成

'MATCH (a) RETURN id(a) as id, id(a) as value 也可以。但是会进行全库扫。

CALL algo.labelPropagation( ‘MATCH (a:PEOPLE1) RETURN id(a) as id, id(a) as value union all match (a:CORPID1) RETURN id(a) as id, id(a) as value’, ‘match (a:PEOPLE1)-[fr]->(b:CORPID1)-[:dk]->(c:LOANID1)<-[:db]-(a)-[:dk]->(d:LOANID1)<-[:db]-(b) RETURN id(a) as source, id(b) as target’, “OUT”, {graph:‘cypher’,write:true,partitionProperty:‘partitionx’}); match(n:PEOPLE1),(m:CORPID1) return n,m;

@graphway 是的。我这个语句只是举例。这里会 产生笛卡尔积。很浪费资源

@bingo 我需要将标签作为参数传入进来,然后进行查询,类似于这种 name = "六味地黄胶囊" data = graph.run(‘match (n {name:$name}) return n limit 5’, name=name)

只不过我需要把变量换成label,如果我这么写肯定报错 name = "六味地黄胶囊" data = graph.run(‘match (n:$label) return n limit 5’, label=name)

不知道您有没有什么办法解决这个问题,谢谢

match(n)–(m) where n:label1 or n:label2 return n ; 终于可以结案了

@liutianling 标签是不是能用变量的

@bingo match (n)–(m)一定要匹配那个关系和(m)吗?只用match (n)不行吗?

match(n) where n:label1 or n:label2 return n ; 完全可以啊

@liutianling 你解决这个问题了吗

回到顶部