1

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 2

1
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])
0

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])
2
  • Just to add something, I prefer the f-string way because I've already noticed some bugs using the dollar syntax while creating sub-graphs. I still don't understand what happened but the f-string way was the only one to work.
    – andrepz
    Aug 20, 2021 at 10:21
  • This is EXTREMELY dangerous if your application accepts user input. It's the Neo4j equivalent of SQL injection. Use this solution only if you are absolutely, positively, one hundred percent sure that "references" does not and never will contain unsanitized data. Even then, think twice.
    – Dausuul
    Jul 28, 2022 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.