0
votes

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
Looks correct to me. Are you sure that there are any persons that meet both of these criteria? That is, are there any persons that appear in the result of query 1 and query 2?ean5533
Yes, there are persons that match both the conditions.Syam Kumar S
I tried to execute the query without the WHERE clause and i Printed the result including all the 'var's that i use. var7,var8,var9,var10,var11 are null for all the results. var1 and var7 both represent the nodes with relation 'contacts', but var1 has some values and var7 is null.Syam Kumar S
That is totally bizarre. I really can't explain that behavior other than guessing that it's a bug (or something else that I'm not seeing). Is it possible for you to set up some example data on console.neo4j.org?ean5533
Is there any way you can recreate this in console.neo4j.org and post the link here?Peter Neubauer

1 Answers

3
votes

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;