cancel
Showing results for 
Search instead for 
Did you mean: 

Complex query doesnt work all time and skip some nodes

zlatkopr
Node Link

I'm a beginner in Neo4j and I've somewhat mastered the basics, but slightly more complex queries are a problem for me. I want to display all connections and belonging nodes for one node, with the fact that some nodes are one step, and some nodes are two steps from the beginning. I tried with CALL, but it always skips one of the queries and doesn't show all the queries. For some OIB numbers it does not show the result at all. If I click on one of the nodes, a node is displayed that I cannot see through the query

Slika zaslona 2022-07-11 13-30-28.pngSlika zaslona 2022-07-11 13-30-49.png

 

MATCH (o1:Osoba)-[r1]->(o2:Osoba)
MATCH (o1:Osoba)-[r2]->(zdrp1:ZdravstveniProblem)-[r3]->(zdrp2:ZdravstvenaUstanova)
MATCH (o1:Osoba)-[r4]->(skolp1:ProblemŠkola)-[r5]->(skolp2:ŠkolskaUstanova)
MATCH (o2:Osoba)-[r6]->(zakp1:ZakonskiProblem)-[r7]->(zakp2:PolicijskaPostaja)
MATCH (o2:Osoba)-[r8]->(socp1:SocijalniProblem)-[r9]->(socp2:SocijalnaSkrb)
WHERE o1.OIB = '40105581613'
RETURN o1, o2, r1, r2, r3, r4, r5, r6, r7, r8, r9, zdrp1, zdrp2, skolp1, skolp2, zakp1, zakp2, socp1, socp2

 

 

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

You may have an issue that some of the match return null, so that the query does not return anything for that collection of matches. Try using 'optional match' instead. Try this refactored version.

MATCH (o1:Osoba) WHERE o1.OIB = '40105581613'
OPTIONAL MATCH (o1)-[r1]->(o2:Osoba)
OPTIONAL MATCH (o1)-[r2]->(zdrp1:ZdravstveniProblem)-[r3]->(zdrp2:ZdravstvenaUstanova)
OPTIONAL MATCH (o1)-[r4]->(skolp1:ProblemŠkola)-[r5]->(skolp2:ŠkolskaUstanova)
OPTIONAL MATCH (o2)-[r6]->(zakp1:ZakonskiProblem)-[r7]->(zakp2:PolicijskaPostaja)
OPTIONAL MATCH (o2)-[r8]->(socp1:SocijalniProblem)-[r9]->(socp2:SocijalnaSkrb)
RETURN o1, o2, r1, r2, r3, r4, r5, r6, r7, r8, r9, zdrp1, zdrp2, skolp1, skolp2, zakp1, zakp2, socp1, socp2

Does it give you what you want? 

View solution in original post

4 REPLIES 4

Cobra
Ninja
Ninja

Hello @zlatkopr 😊

I'm not sure you query is optimized but some of your queries overwrite the contents of previously defined variables. So you should write your query like this:

 

MATCH (o1:Osoba {OIB: '40105581613'})-[r1]->(o2:Osoba)
MATCH (o1)-[r2]->(zdrp1:ZdravstveniProblem)-[r3]->(zdrp2:ZdravstvenaUstanova)
MATCH (o1)-[r4]->(skolp1:ProblemŠkola)-[r5]->(skolp2:ŠkolskaUstanova)
MATCH (o2)-[r6]->(zakp1:ZakonskiProblem)-[r7]->(zakp2:PolicijskaPostaja)
MATCH (o2)-[r8]->(socp1:SocijalniProblem)-[r9]->(socp2:SocijalnaSkrb)
RETURN o1, o2, r1, r2, r3, r4, r5, r6, r7, r8, r9, zdrp1, zdrp2, skolp1, skolp2, zakp1, zakp2, socp1, socp2

 

Regards,
Cobra

Thank you very much for the quick reply

glilienfield
Ninja
Ninja

You may have an issue that some of the match return null, so that the query does not return anything for that collection of matches. Try using 'optional match' instead. Try this refactored version.

MATCH (o1:Osoba) WHERE o1.OIB = '40105581613'
OPTIONAL MATCH (o1)-[r1]->(o2:Osoba)
OPTIONAL MATCH (o1)-[r2]->(zdrp1:ZdravstveniProblem)-[r3]->(zdrp2:ZdravstvenaUstanova)
OPTIONAL MATCH (o1)-[r4]->(skolp1:ProblemŠkola)-[r5]->(skolp2:ŠkolskaUstanova)
OPTIONAL MATCH (o2)-[r6]->(zakp1:ZakonskiProblem)-[r7]->(zakp2:PolicijskaPostaja)
OPTIONAL MATCH (o2)-[r8]->(socp1:SocijalniProblem)-[r9]->(socp2:SocijalnaSkrb)
RETURN o1, o2, r1, r2, r3, r4, r5, r6, r7, r8, r9, zdrp1, zdrp2, skolp1, skolp2, zakp1, zakp2, socp1, socp2

Does it give you what you want? 

zlatkopr
Node Link

I forgot to say that there are rows without data, so that's why your solution works without problems. You guessed right that there are such lines. Thank you very much for your help and quick response. I was pleasantly surprised by the speed of the response.

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.