Submit a parameter in a neo4j algo query

Viewed 519

I am running Neo4j 3.4.1. I ran a clustering algorithm and created clusters with labels 'labelpart'. Now I want for each cluster find the node with the highest betweenness centrality. I can run the following query for a specific cluster_id, say 3212

CALL algo.betweenness.stream(
  'MATCH (p {labelpart:3212}) RETURN id(p) as id',
  'MATCH (p1 {labelpart:3212})-[]->(p2 {labelpart:3212}) RETURN id(p1) as source, id(p2) as target',
  {graph:'cypher', write: false}
) YIELD nodeId, centrality
RETURN nodeId,centrality order by centrality desc limit 1

Now, I want to do it programmatically for all cluster ids. I tried the following query using neo4j bolt driver for python:

from neo4j import GraphDatabase, basic_auth

and here is the query

cluster_id = 3212
query = """
CALL algo.betweenness.stream(
  'MATCH (p {labelpart:{cluster_id}}) RETURN id(p) as id',
  'MATCH (p1 {labelpart:{cluster_id}})-[]->(p2 {labelpart:{cluster_id}}) RETURN id(p1) as source, id(p2) as target',
  {graph:'cypher', write: false}
) YIELD nodeId, centrality
RETURN nodeId,centrality order by centrality desc limit 1
"""
results = session.run(query, parameters = {'cluster_id':cluster_id })

I get the following error message

....
ClientError: Failed to invoke procedure `algo.betweenness.stream`: Caused by: org.neo4j.cypher.ParameterNotFoundException: Expected parameter(s): cluster_id

Using a parameter works for a simple query, e.g. I can do the following

cluster_id = 3212
query1 = """
MATCH (p {labelpart:{cluster_id}}) RETURN id(p) as id
"""
results = session.run(query1, parameters = {'cluster_id':cluster_id })

Now, my guess is the problem is that the queries that I need to submit parameters to are themselves parameters of the algo.betweenness.stream() function.

Unfortunately,I couldn't find any directions on how to submit a parameter for such a query. Perhaps it's just not possible?

1 Answers

@David, Your guess is correct.

You need to write the query like:

query = """ CALL algo.betweenness.stream(   'MATCH (p {labelpart:{cluster_id}}) 
 RETURN id(p) as id',
 'MATCH (p1 {labelpart:{cluster_id}})-[]->(p2 {labelpart:{cluster_id}}) RETURN id(p1) 
as source, id(p2) as target',
{graph:'cypher', write: false, params:{cluster_id:{ext_parameter_name}}}
) YIELD nodeId, centrality RETURN nodeId,centrality order by
centrality desc limit 1 """

ext_parameter_name is the parameter to session.run. (Used different name to avoid confusion).

Related