Search code examples
c#neo4jcyphergraph-algorithmneo4jclient

How to use the neo4j graph algorithms with Neo4jClient


I would like to use the closeness centrality graph algorithm with Neo4jClient a .Net client for neo4j.

The query to use closeness centrality in Cypher is:

CALL algo.closeness.stream('Node', 'LINK')
YIELD nodeId, centrality

RETURN algo.getNodeById(nodeId).id AS node, centrality
ORDER BY centrality DESC
LIMIT 20;

My attempt at a translation to C#:

var clcsCent =
_graphClient.Cypher.Call("algo.closeness.stream('SitePoint', 'SEES')")
.Yield("node,centrality")
.Return((node,centrality)=>new {
Int32 = node.As<Int32>(),
Double = centrality.As<Double>()
})
.Results;

SitePointis my class for nodes which have SEES relationships between them.

The exception I get is:

SyntaxException: Unknown procedure output: `node` (line 2, column 7 (offset: 
55))
"YIELD node,centrality"
        ^

What is the correct C# syntax for this query?


Solution

  • Simple solution - change 'node' for nodeId:

    var clcsCent =
    _graphClient.Cypher.Call("algo.closeness.stream('SitePoint', 'SEES')")
    .Yield("nodeId,centrality")
    .Return((nodeId,centrality)=>new {
    Int32 = nodeId.As<Int32>(),
    Double = centrality.As<Double>()
    })
    .Results;
    

    This returns an IEnumerable where each element is an anonymous type with two properties for the nodeId and its centrality score. Both Int32 = nodeId.As<Int32>() and Double = centrality.As<Double>() look like they should be more concise.

    The documentation for closeness centrality gives 'node' as the name of the return type but it seems like it should be nodeId.

    A useful resource for these cypher to C# translations is the cypher examples page on the Neo4jClient github pages