2

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.

Ümit
  • 17,379
  • 7
  • 55
  • 74

3 Answers3

2

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
1

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 
Michael Hunger
  • 41,339
  • 3
  • 57
  • 80
  • Sir its not still working for me. As if the page node and tag node don't have relation then the particular page node doesn't get searched by this query. I m sorry but need your guidance.Till the time I am working on it. thank you –  Jun 28 '16 at 05:29
  • Actually Sir I am using neo4j ver 2.3.2 where the null check and the where not clauses were not working. Optional Match saved me. THanku so much. I have posted the correct query –  Jun 28 '16 at 06:51
0

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

Mvde
  • 141
  • 6
  • Do cases can have the queries like i want to make the condition for if-else –  Jun 24 '16 at 10:38
  • yes see http://stackoverflow.com/questions/27576427/cypher-neo4j-case-expression-with-merge – Mvde Jun 24 '16 at 11:16
  • please read my question again. May be I am unable to make you understand but that question didn't helped me. –  Jun 24 '16 at 12:21