K.M.J
K.M.J

Reputation: 143

how to pass parameters with like clause and regular expression in neo4j

I am developing an application using nodejs and neo4j. I have to execute the following query with the parameters but this doesn't give result for where pi.genenames =~ '.$nameParam.'

Match (pi:proteinidentifier)-[pe:proteinidentifier_has_a_ensembleprotein]-> (e:ensembleprotein)
-[ppi:on_interaction_with]->(en:ensembleprotein)-[ep:ensembleprotein_has_a_proteinidentifier]-(pi2:proteinidentifier) 
where pi.genenames  =~ '.*$nameParam.*' OR pi.uniprot =  $nameParam OR pi.ensembleprotein =  $nameParam 
return pi,pe,e,ppi,en,ep,pi2  limit $limitval

Upvotes: 1

Views: 534

Answers (2)

cybersam
cybersam

Reputation: 66999

Cypher does not parse string literals to do parameter replacement.

That is why this does not work:

pi.genenames =~ '.*$nameParam.*'

However, here is a workaround that does work:

pi.genenames =~ ('.*' + $nameParam + '.*')

But the CONTAINS function is a better way of handling your use case.

Upvotes: 1

Christoph P
Christoph P

Reputation: 133

This is because .*$nameParam.* doesn't evaluate to the desired string within cypher. Try to pass the whole expression including *. so that the like clause looks like this:

=~ $nameParamRegex

Alternatively, you could use the contains keyword if the regex is as simple as here:

where pi.genenames contains $nameParam OR pi.uniprot = $nameParam etc.

Upvotes: 1

Related Questions