h.zak
h.zak

Reputation: 1527

Filter by list with order in neo4J cypher

Is it possible to order and filter by relation in neo4j ? for example :

I have gardien and kid with relations (father,mother,brother, sister) and I want to list gardiens and kids with their relationships with this type of filter:

if the gardien is the father of the kid: 
   then return (father, kid)
else if the kid doesn't have the father :
   then return his (mother, kid)
else if the kid doesn't have the father or mother:
   then return his (brother, kid)
otherwise :
   return his (sister, kid)

How can do a request like this with cypher ?

Thank you in advance,

Upvotes: 1

Views: 291

Answers (1)

mastisa
mastisa

Reputation: 2053

If you can add (father, mother, ...) as property to relation I think this query can help you

MATCH (:Human {type:'kid'})-[r:Relate]-(gurdian:Human)
WITH m ,CASE r.type 
    WHEN 'father' THEN 0
    WHEN 'mother' THEN 1
    WHEN 'brother' THEN 2
    ELSE 3
    END as value
RETURN gurdian ORDER BY value

My data test:

Create (b:Human {type: 'kid'}),
(f:Human {type: 'father'}),
(m:Human {type: 'mother'}), 
(br:Human {type: 'brother'}),
(b)-[:Relate {type: 'father'}]->(f),
(b)-[:Relate {type: 'mother'}]->(m),
(b)-[:Relate {type: 'brother'}]->(br)

Upvotes: 1

Related Questions