Adding a python list parameter in a neo4j query

Viewed 397

I'm about to run a query in neo4j with a parameter, but it always returns me an error.

> query= ("MATCH (p1:Item),(p2:Item) where p1.value=$name
> RETURN  p1.value AS from, p2.value AS to,
> gds.alpha.similarity.euclideanDistance((p1.embeddingNode2vec),
> (p2.embeddingNode2vec)) AS similarity order by similarity desc limit
> 40", name=references[2])

the p1.value the first element in the references list. This returns me a

SyntaxError: invalid syntax

error. Could you please tell me how to run it? my research so far: https://community.neo4j.com/t/how-to-use-dictionary-parameters-in-python-neo4j/18626

2 Answers
MATCH (p1:Item),(p2:Item) where p1.value=$params RETURN  p1.value AS from, p2.value AS to, gds.alpha.similarity.euclideanDistance((p1.embeddingNode2vec), (p2.embeddingNode2vec)) AS similarity order by similarity desc limit 40

and then call the query execution like this:

nodes = graphDB_Session.run(query, params= references[1])

Would be better to see more details of the code, but looks like the problem is the parenthesis. Usually, when I do multi-line queries in python I'll use the multi-line f-string (using triple quotes -> """)

So the code would look like:

query= f"""MATCH (p1:Item),(p2:Item) where p1.value={references[2]}
RETURN  p1.value AS from, p2.value AS to,
gds.alpha.similarity.euclideanDistance((p1.embeddingNode2vec),
(p2.embeddingNode2vec)) AS similarity order by similarity desc limit
40"""

obs: maybe you'll have to insert single-quotes for the parameter ('{references[2]') so Neo4J recognize it as a string and not a variable).

This way you don't need to pass parameters inside the session.run method. Just be careful and remember to correctly escape any curly braces in the code (just duplicate it { -> {{ )

If you want don't want to use a f-string, remove the parenthesis, keep the dollar-sign syntax ($name) instead of the curly-braced one and pass the parameter directly into the client.run().

Like this:

run = client.run(query, name=references[2])
Related