From 69c43d6d795f6f58a4bb385c1201447a81c0ba29 Mon Sep 17 00:00:00 2001 From: Vargha Csongor Date: Tue, 29 Apr 2025 22:09:43 +0200 Subject: [PATCH] fix descendant queries --- .../queries/get_blood_relations_by_id.cypher | 24 +++++++++++--- .../get_family_tree_with_spouses.cypher | 33 +++++++++++++++---- 2 files changed, 46 insertions(+), 11 deletions(-) diff --git a/apps/db-adapter/internal/memgraph/queries/get_blood_relations_by_id.cypher b/apps/db-adapter/internal/memgraph/queries/get_blood_relations_by_id.cypher index f74cd4c..556e66d 100644 --- a/apps/db-adapter/internal/memgraph/queries/get_blood_relations_by_id.cypher +++ b/apps/db-adapter/internal/memgraph/queries/get_blood_relations_by_id.cypher @@ -1,11 +1,25 @@ MATCH (n:Person) WHERE id(n) = $id -OPTIONAL MATCH (n)-[p:Parent*..]->(family:Person) -OPTIONAL MATCH (family)-[c:Child*1..4]->(children:Person) -OPTIONAL MATCH (family)-[s:Sibling]->(siblings:Person) +OPTIONAL MATCH (n)-[p:Parent*..]->(ancestors:Person) +optional MATCH (n)-[cc:Child*..]->(descendants:Person) +OPTIONAL MATCH (ancestors)-[c:Child*1..4]->(children:Person) +OPTIONAL MATCH (ancestors)-[s:Sibling]->(siblings:Person) OPTIONAL MATCH (n)-[ds:Sibling]->(direct_siblings:Person) -WITH collections.to_set(collect(n)+collect(family)+collect(children)+collect(direct_siblings)+collect(siblings)) as people, -collections.to_set(collect(c) + collect(p) + collect(s) + collect(ds)) as relationships +WITH collections.to_set( + collect(n)+ + collect(descendants)+ + collect(ancestors)+ + collect(children)+ + collect(direct_siblings)+ + collect(siblings) + ) as people, +collections.to_set( + collect(c) + + collect(cc) + + collect(p) + + collect(s) + + collect(ds) + ) as relationships UNWIND people as ppl RETURN collect({ id: id(ppl), diff --git a/apps/db-adapter/internal/memgraph/queries/get_family_tree_with_spouses.cypher b/apps/db-adapter/internal/memgraph/queries/get_family_tree_with_spouses.cypher index b9e98b6..62586c5 100644 --- a/apps/db-adapter/internal/memgraph/queries/get_family_tree_with_spouses.cypher +++ b/apps/db-adapter/internal/memgraph/queries/get_family_tree_with_spouses.cypher @@ -1,14 +1,35 @@ MATCH (n:Person) WHERE id(n) = $id -OPTIONAL MATCH (n)-[p:Parent*..]->(family:Person) -OPTIONAL MATCH (family)-[c:Child*1..4]->(children:Person) -OPTIONAL MATCH (family)-[s:Sibling]->(siblings:Person) +OPTIONAL MATCH (n)-[p:Parent*..]->(ancestors:Person) +optional MATCH (n)-[cc:Child*..]->(descendants:Person) +OPTIONAL MATCH (ancestors)-[c:Child*1..4]->(children:Person) +OPTIONAL MATCH (ancestors)-[s:Sibling]->(siblings:Person) OPTIONAL MATCH (n)-[ds:Sibling]->(direct_siblings:Person) -OPTIONAL MATCH (family)-[fsp:Spouse]->(fspouse:Person) +OPTIONAL MATCH (ancestors)-[fsp:Spouse]->(fspouse:Person) OPTIONAL MATCH (children)-[csp:Spouse]->(cspouse:Person) OPTIONAL MATCH (n)-[sp:Spouse]->(spouse:Person) -WITH collections.to_set(collect(n) + collect(family) + collect(children) + collect(direct_siblings) + collect(fspouse) + collect(cspouse) + collect(spouse) + collect(siblings)) as people, -collections.to_set(collect(c) + collect(p) + collect(s) + collect(ds) + collect(fsp) + collect(csp) + collect(sp)) as relationships +WITH + collections.to_set( + collect(n) + + collect(descendants) + + collect(ancestors) + + collect(children) + + collect(direct_siblings) + + collect(fspouse) + + collect(cspouse) + + collect(spouse) + + collect(siblings) + ) as people, + collections.to_set( + collect(c) + + collect(cc) + + collect(p) + + collect(s) + + collect(ds) + + collect(fsp) + + collect(csp) + + collect(sp) + ) as relationships UNWIND people as ppl RETURN collect({ id: id(ppl),