cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Correct syntax for GDS call in Python

andy_hegedus
Graph Fellow

Hi,

I'm trying to replicate some of the working browser GDS code into Python script for automation purposes.

I am working on basic graph create functionality.
Within browser this works:

CALL gds.graph.create.cypher(
    "self_cite",
    "MATCH (n:patent)-[:Assigned_to]-(a:company) WHERE a.name='applied materials' RETURN id(n) AS id, labels(n) AS labels",
    "MATCH (a:patent)-[:Cites]-(b:patent) return id(a) AS source, id(b) AS target",
    {validateRelationships: FALSE}
);

I am using the Neo4J python driver and working with a Jupyter notebook.

Here is what I have:

def make_self_cite(tx):
    result = tx.run("CALL gds.graph.create.cypher('self_cite', "
                    "'MATCH (n:patent)-[:Assigned_to]-(a:company) WHERE a.name='applied materials' RETURN id(n) AS id, labels(n) AS labels', "
                    "'MATCH (a:patent)-[:Cites]-(b:patent) return id(a) AS source, id(b) AS target', {validateRelationships: FALSE});")

with driver.session() as session:
    selftest = session.write_transaction(make_self_cite)
driver.close()    

And I am getting
CypherSyntaxError: {code: Neo.ClientError.Statement.SyntaxError} {message: Invalid input 'p': expected 'n/N' (line 1, column 103 (offset: 102))
And trying to figure out the correct syntax.
I think that puts it at the where clause where I define 'applied materials' . What should be the correct syntax. And as extra credit how would I parameterize the 'applied materials' part to further allow automation?
Andy

1 REPLY 1

andy_hegedus
Graph Fellow

Ok
Got the first part figured out by backslash quoting.

def make_self_cite(tx):
    result = tx.run("CALL gds.graph.create.cypher('self_cite', "
                    "\"MATCH (n:patent)-[:Assigned_to]-(a:company) WHERE a.name=\'applied materials\' RETURN id(n) AS id, labels(n) AS labels\", "
                    "'MATCH (a:patent)-[:Cites]-(b:patent) return id(a) AS source, id(b) AS target', {validateRelationships: FALSE});")

with driver.session() as session:
    selftest = session.write_transaction(make_self_cite)
driver.close()    


Andy