Complex query doesnt work all time and skip some nodes

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

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.

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?

Hello @zlatkopr :blush:

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