Showing results for 
Search instead for 
Did you mean: 

Get relations of nodes found with a fulltext query


Greetings dear community,

I would like to ask about how I can get relations of nodes found by means of a full text query, here is how my query looks like:

CALL db.index.fulltext.queryNodes("SearchIndex", "*p0420*") YIELD node
WHERE node.isHidden IS NULL

So the found nodes could have a FaultCode label or Question label, both belong to the same index, what I want to do is, if the node label is FaultCode then I want to get its related questions, otherwise if the node label is Question, then I want to get its related fault code.

Can some help me express this in Cypher?

Thanks in advance


You could use a ‘case’ statement in the ‘return’ clause to conditionally return one or the other properties based on the labels. 


CALL db.index.fulltext.queryNodes("SearchIndex", "*p0420*") YIELD node
WHERE node.isHidden IS NULL
when :FaultCode then node.faultCode
when :Question then node.question
end as result


Thank you @glilienfield 

But, in my case in want to return related nodes not properties, based on the label of course. Is there a way to achieve that?

Sorry for my misunderstanding. A simple approach would be to add an ‘option match’ for both relationship regardless of node type. You will get null results for the match that has no result. This would be ok if the optional match results are mutually exclusive, returning only on result for each node type. 

another approach is to use one of the APOC library’s conditional procedures, which allow you to conditionally execute cypher queries.  In your case the ‘case’ procedure seems appropriate. Your conditions would be the node labels, and the queries would be the match to get the relationships.

You can also use a ‘call’ subquery in a certain way to conditionally execute a block  of cypher, but I don’t think this is applicable since you are returning data,