查询名称与neo4j cypher中的其他名称不同 [英] query for name not like other name in neo4j cypher

查看:91
本文介绍了查询名称与neo4j cypher中的其他名称不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试查找节点2的名称中未包含节点1的名称的节点,反之亦然. 我已经尝试过了它和它的变体.出现正则表达式错误,不确定如何包含另一个节点的文字名称,以及如何不包含该文本.

I'm trying to find nodes where node1's name is NOT contained in node2's name and vice versa. I've tried this and variants of it. getting regex errors, not sure how to include the other node's literal name, and also how to do NOT includes.

START node1=node(*) 
MATCH node1-[r]-node2 
WHERE node1.name !~ '.*{node2.name}.*' and node2.name !~ '.*{node1.name}.*'
RETURN node1.name, node2.name limit 10;

推荐答案

我将尝试回答如何使您的查询正常工作,但是对于Neo4j而言,这种查询看起来有点不规则.如果您发现自己编写了许多类似这样的查询,则可能需要重新考虑您的模型或数据库选择.

I'll try to answer how to get your query to work, but this type of query looks a bit irregular for Neo4j. If you find yourself writing many queries like these you may want to rethink your model or choice of database.

您的'.*{node2.name}.*'包含node2.name作为字符串文字,而不是作为属性引用.要解析该属性并在正则表达式中使用它的值,可以使用字符串连接,例如'.*' + node2.name + '.*'.如果为node2.name='Darby',则正则表达式字符串将为'.*Darby.*'.

Your '.*{node2.name}.*' contains node2.name as a string literal, not as a property reference. To resolve the property and use it's value in a regular expression you can use string concatenation, something like '.*' + node2.name + '.*'. If node2.name='Darby' then the regexp string will be '.*Darby.*'.

正则表达式用=~表示,如果要使用!检查属性是否存在,可以执行node.property! =~ regexp.要排除双正则表达式条件的结果,请执行WHERE NOT ( condition1 OR condition2 ).

Regexp is signified with =~, if you want to use ! to check for property existence you can do node.property! =~ regexp. To exclude the results of your double regexp condition, do WHERE NOT ( condition1 OR condition2 ).

由于字符串连接和regexp比较之间的运算符顺序不明显,因此最好也将字符串连接放在括号中,否则您可能最终将第一个字符串部分的regexp结果与其余字符串连接在一起字符串部分,即(node.name =~ '.*') + node2.name + '.*',这将是类型错误.

Since the operator order between string concatenation and regexp comparison is not obvious, it's probably best to put the string concatenation in parenthesis too, or you may end up concatenating the result of a regexp on the first string part with the rest of the string parts, i.e (node.name =~ '.*') + node2.name + '.*', which would be a type error.

假设您使用Neo4j 1.9,整个查询可能看起来像(对于2.0+,您可以删除!)

Assuming you use Neo4j 1.9 the whole query could look like (for 2.0+ you can just drop the !)

START node1=node(*)
MATCH node1-[r]-node2
WHERE NOT (
    node1.name! =~ ('.*' + node2.name + '.*') OR
    node2.name! =~ ('.*' + node1.name + '.*')
) RETURN node1.name, node2.name LIMIT 10

(这是一种昂贵的查询,它可能返回冗余结果,因为符合条件的每个节点对(A,B)都将同时以(A,B)和(B,A)的形式返回.请尝试声明关系的方向,它应该排除多余的结果并提高性能.)

(It's an expensive type of query and it probably returns redundant results, since each node pair (A,B) that fit your conditions will be returned both as (A,B) and as (B,A). Try declaring direction on the relationship, it should exclude redundant results and improve performance.)

这篇关于查询名称与neo4j cypher中的其他名称不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆