user3359536
user3359536

Reputation:

Neo4j-Multiple match statement in cypher query with if else conditon

I have to search certain nodes like eq.page whether or not they have relation or not with some other node..like tags.

 If they are connected to tag nodes then search for the search string in page name and the tag names 

Else search for the search string in page name only
MATCH ...//nodes of certain type    
WHERE    
    if r is null'
      ...//Match query without relation for searching
    else 
      ...//Match query without relation for searching

Return ...

MATCH (n:page)<-[r:pagetag]-(tag)
if  r is null 
then n.title CONTAINS 'java'or tag.name IN ["java"] 
return distinct n.name
else  n.title CONTAINS 'java'return distinct n.name
  END

This query is giving error. May be syntax problem. But I want to search for like this only for the pages.

Upvotes: 2

Views: 1531

Answers (3)

user3359536
user3359536

Reputation:

Finally acheieved what I wanted. Thanks all. OPTIONAL Match worked great for me.

MATCH (s:page)
WITH s
OPTIONAL MATCH (s)<-[r:pagetag]-(tag)
WITH s,r,tag
Where s.pagename contains 'juniors'or tag.name contains 'math'
return distinct s.pagename

Upvotes: 2

Michael Hunger
Michael Hunger

Reputation: 41706

Isn't that just basic conditionals?

MATCH (n:page)<-[r:pagetag]-(tag)
WITH n,r,tag
WHERE r IS NULL AND (n.title CONTAINS 'java' or tag.name IN ["java"])
OR NOT r is NULL AND (n.title CONTAINS 'java')
return distinct n.name 

Upvotes: 1

Mvde
Mvde

Reputation: 141

I don't think you can use if else in Neo4j you have to use case, or foreach to simulate if .

Upvotes: 0

Related Questions