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"
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.
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.
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?
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)```
put the query on several lines with the triple """ at the beginning and at the end of the query;
put the query on several lines with one " 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.