using multiple match clauses doesn't return an

2019-08-27 00:17发布

问题:

I am executing the following two queries and i am getting some result.

First query

START 
person=node:NODE_TYPE(NODE_TYPE='PERSON') 
MATCH 
(person)-[?:contacts]->(var1)-[?:addresses]->(var2)-[?:details]->(var3)-[?:items]->(var4)-[?:items]->(var5)-[?:value]->(var6) 
WHERE 
var2.`#nodeId` ='at0000' and var3.`#nodeId` ='at0001' and var4.`#nodeId` ='at0002' and var5.`#nodeId` ='at0028' and var6.`value` =~'address.*' 
return distinct person;

Second query

START 
person=node:NODE_TYPE(NODE_TYPE='PERSON') 
MATCH 
(person)-[?:contacts]->(var1)-[?:addresses]->(var2)-[?:details]->(var3)-[?:items]->(var4)-[?:value]->(var5) 
WHERE 
var2.`#nodeId` ='at0000' and var3.`#nodeId` ='at0001' and var4.`#nodeId` ='at0009' and var5.`value` =~'india.*' 
return distinct person;

But when I combine the two queries to a single query to get persons that match both these conditions, it isn't working.

the combined query is

START 
person=node:NODE_TYPE(NODE_TYPE='PERSON')  
MATCH 
(person)-[?:contacts]->(var1)-[?:addresses]->(var2)-[?:details]->(var3)-[?:items]->(var4)-[?:items]->(var5)-[?:value]->(var6) , (person)-[?:contacts]->(var7)-[?:addresses]->(var8)-[?:details]->(var9)-[?:items]->(var10)-[?:value]->(var11) 
WHERE 
var2.`#nodeId` ='at0000' and var3.`#nodeId` ='at0001' and var4.`#nodeId` ='at0002' and var5.`#nodeId` ='at0028' and var6.`value` =~'address.*' and 
var8.`#nodeId` ='at0000' and var9.`#nodeId` ='at0001' and var10.`#nodeId` ='at0009' and var11.`value` =~'india.*' 
return distinct person;

This query return an empty iterator.

I used 'comma' to combine the MATCH conditions and 'and' to combine the WHERE conditions. Is there any problem in this?

(I am implementing a query builder to build cypher query. I have to check multiple condition matches. What is the best way to do this?)

Neo4j 1.9M04

回答1:

You may be running into the "identifier uniqueness" condition described here (see first post by Michael Hunger). If you got a match on the same (person) node and have only 1 :contacts relation from there, then the identifier var1 is "using up" the connected node in this match and var7 can't be assigned to the same node.

In other words, a node found by a match statement can only be used by one identifier within the same match statement.

You can try to use a WITH clause instead followed by another MATCH to work around this issue.

START 
person=node:NODE_TYPE(NODE_TYPE='PERSON') 
MATCH 
(person)-[?:contacts]->(var1)-[?:addresses]->(var2)-[?:details]->(var3)-[?:items]->(var4)-[?:items]->(var5)-[?:value]->(var6) 
WHERE 
var2.`#nodeId` ='at0000' and var3.`#nodeId` ='at0001' and var4.`#nodeId` ='at0002' and var5.`#nodeId` ='at0028' and var6.`value` =~'address.*' 
with distinct person
MATCH 
(person)-[?:contacts]->(var1)-[?:addresses]->(var2)-[?:details]->(var3)-[?:items]->(var4)-[?:value]->(var5) 
WHERE 
var2.`#nodeId` ='at0000' and var3.`#nodeId` ='at0001' and var4.`#nodeId` ='at0009' and var5.`value` =~'india.*' 
return distinct person;