query for name not like other name in neo4j cypher

六眼飞鱼酱① 提交于 2019-12-13 05:47:40

问题


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;

回答1:


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.

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.*'.

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 ).

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.

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

(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.)




回答2:


We can use this operator "<>"

eg : match(node:Application) where node.name <> "None" return node

This query is to return all the application node whose name is not "None".



来源:https://stackoverflow.com/questions/21792040/query-for-name-not-like-other-name-in-neo4j-cypher

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!