Reputation: 73
I have a kinda faceted search task to my domain model.
I have a graph that consists of two parts:
(:TAG)
's hierarhical tree with -[:IS_A]->
relations between parent tags and it's child(s). Note, that each banch of the tree has different levels of depth.
Every (:TAG)
node has unique "name"
(String) propery(:IMG)
nodes, that have unique "name"
(String) and "href"
(String) properties.(:IMG)
is connected to at least one (:TAG)
with the relation (:TAG)-[:DESCRIBES]->(:IMG)
, but there could be (:TAG)
nodes that don'c connect with any of (:IMG)
nodes.
I need to be able to tell any amount of tag's "names" and to be responsing all intersecting images nodes, but with following logic:
If every two (:TAG)
nodes has similar parent (:TAG)
node, so I need to response images conencted to both of them,
else (if they have different parent (:TAG)
node), so I need to response only images that connect to both of them at the same time.
--
What I already know:
MATCH
(img:IMG)<-[*]-(:TAG {name:'Tag Name A'}),
(img:IMG)<-[*]-(:TAG {name:'Tag Name B'}),
(img:IMG)<-[*]-(:TAG {name:'Tag Name C'})
RETURN img
MATCH (img:TEST_01_Img)
WHERE
(img)<-[*]-(:TAG {name:'Tag Name A'})
OR
(img)<-[*]-(:TAG {name:'Tag Name B'})
OR
(img)<-[*]-(:TAG {name:'Tag Name C'})
RETURN img
And I think have to somehow combine these two methods with (if...else) logic of combining results if images have same tag's parent and intersecting them if they have different tag's parents.
Maybe I need to use Conditional Cypher Execution, but I have no idea how, cuz I'm very new to this.
Thank you very much for your help in advance!
Upvotes: 0
Views: 327
Reputation: 533
Yes you can combine match [pattern] with where [pattern]. Example:
// Tagged A and B
MATCH (img)<-[*]-(:TAG {name:'Tag Name A'})
WHERE (img)<-[*]-(:TAG {name:'Tag Name B'})
// Tagged A but not B
MATCH (img)<-[*]-(:TAG {name:'Tag Name A'})
WHERE NOT (img)<-[*]-(:TAG {name:'Tag Name B'})
See Using Patterns in Where (https://neo4j.com/docs/cypher-manual/current/clauses/where/#query-where-patterns)
Upvotes: 1