Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
10-27-2020 11:02 AM
Hi everyone, I am working on a recommender system. I am trying first some basic queries. I am stuck with the following query that works on the Neo4j Browser but it does not in Python:
"MATCH (client:Client{id:$client_id})-[:SELLS]-(product:Product{id:$product_id})"
"MATCH (product)-[:IN_CATEGORY]->(:Category)<-[:IN_CATEGORY]-(rec:Product)<-[i:INCLUDES]-()"
"WITH sum(i.quantity) AS quantity, rec.name AS recommendation"
"RETURN recommendation, quantity ORDER BY quantity DESC"
Python gives me the following error message:
{code: Neo.ClientError.Statement.SyntaxError} {message: Invalid input 'c': expected 'm/M' or 't/T' (line 1, column 239 (offset: 238))
This query is inside a function where I declare both parameters. It is quite similar to a category recommendation with almost the same structure.
This is the query that works on Neo4j browser:
MATCH (client:Client{id:$clientId})-[:SELLS]-(product:Product{id:$productId})
MATCH (product)-[:IN_CATEGORY]->(:Category)<-[:IN_CATEGORY]-(rec:Product)<-[i:INCLUDES]-(order:Order)
WITH sum(i.quantity) AS quantity, rec.name AS recommendation
RETURN recommendation, quantity ORDER BY quantity DESC
I cannot understand why the Neo4j Browser can execute the query but Python can't.
Solved! Go to Solution.
10-28-2020 10:01 AM
Normally, these formats should work:
"""
at the beginning and at the end of the query;"
at the beginning and at the end of each line without forgetting to put a space at the end of each line (not mandatory for the last line) like in the example of the doc.Which version of the driver are you using?
10-27-2020 03:35 PM
Hello @samsitta
Did you try to write the query on one line instead of 4? Just to check if it's from the syntax or the query itself.
Regards,
Cobra
10-27-2020 05:31 PM
No, I wrote the query on 4 lines as well.
10-27-2020 07:41 PM
How are your parameters defined in Python? I would expect the syntax to be something like shown in the answer here, rather than using $:
10-28-2020 01:32 AM
Hello @samsitta
$var
is the right way to define arguments in a query"MATCH (client:Client{id:$client_id})-[:SELLS]-(product:Product{id:$product_id}) "
"MATCH (product)-[:IN_CATEGORY]->(:Category)<-[:IN_CATEGORY]-(rec:Product)<-[i:INCLUDES]-() "
"WITH sum(i.quantity) AS quantity, rec.name AS recommendation "
"RETURN recommendation, quantity ORDER BY quantity DESC"
Regards,
Cobra
10-28-2020 09:10 AM
I am using Neo4j Python Driver. I actually have already used two very similar queries and had not problem at all. I will try your suggestion to use your suggestion to use one line instead. I am using spaces at the end of the first three lines already.
10-28-2020 09:32 AM
Thank you, I tried your suggestion and it worked with writing the query on one line. Do you have any idea why it is failing when I try to write it as four lines?
10-28-2020 09:33 AM
No problem could I see the Python function?
10-28-2020 09:42 AM
Sure, this is the one that finally worked:
def best_sellers(tx, client_id, product_id):
recommendations = []
result = tx.run("MATCH (client:Client{id:$client_id})-[:SELLS]->(product:Product{id:$product_id}) MATCH (product)-[:IN_CATEGORY]->(:Category)<-[:IN_CATEGORY]-(rec:Product)<-[i:INCLUDES]-() WITH sum(i.quantity) AS quantity, rec.name AS recommendation RETURN recommendation ORDER BY quantity DESC", client_id = client_id, product_id = product_id)
for item in result:
recommendations.append(item["recommendation"])
return recommendations
with driver.session() as session:
recommendations = session.read_transaction(best_sellers, client_id, product_id)
for recommendation in recommendations:
print(recommendation)```
10-28-2020 10:01 AM
Normally, these formats should work:
"""
at the beginning and at the end of the query;"
at the beginning and at the end of each line without forgetting to put a space at the end of each line (not mandatory for the last line) like in the example of the doc.Which version of the driver are you using?
10-28-2020 10:04 AM
For example, I am using this function and it is working just fine:
def same_category_recommendation(tx, client_id, product_id):
recommendations = []
result = tx.run("MATCH (client:Client{id:$client_id})-[:SELLS]-(product:Product{id:$product_id})"
"MATCH (product)-[:IN_CATEGORY]->(:Category)<-[:IN_CATEGORY]-(rec:Product)"
"RETURN rec.name AS recommendation", client_id = client_id, product_id = product_id)
for item in result:
recommendations.append(item["recommendation"])
return recommendations
with driver.session() as session:
recommendations = session.read_transaction(same_category_recommendation, client_id, product_id)
for recommendation in recommendations:
print(recommendation)
I am using neo4j==4.1.1
10-28-2020 10:08 AM
Indeed, that's weird, I have no more ideas but at least you have a solution when it doesn't work.
You can still create the string query before and put your query in and after call it in tx.run()
10-28-2020 11:27 AM
Thx! I might do that
All the sessions of the conference are now available online