从neo4j C#客户端调用algo.similarity.jaccard算法不起作用

时间:2019-12-05 14:31:01

标签: algorithm neo4j neo4jclient

我有以下查询,当我在Neoj4浏览器上执行该查询时,它运行良好:

MATCH (p1:Book{friendlyUrl:"a-modern-witch"})-[r:BOOK_ADDED_TO_CATALOG]->(catalog1:Catalog)
WITH p1, collect(id(catalog1)) AS p1Catalog
MATCH (p2:Book)-[:BOOK_ADDED_TO_CATALOG]->(catalog2:Catalog)
WHERE p1 <> p2
WITH p1, p1Catalog, p2, collect(id(catalog2)) AS p2Catalog
RETURN p1.title AS from,
p2.title AS to,
algo.similarity.jaccard(p1Catalog, p2Catalog) AS similarity

ORDER BY similarity DESC

我想将其转换为c#代码,并且这样做:

var result = _graphClient.Cypher
        .Match ("(p1:Book{friendlyUrl:'a-modern-witch'})-[r:BOOK_ADDED_TO_CATALOG]->(catalog1:Catalog)")
        .With ("p1, collect(id(catalog1)) AS p1Catalog")
        .Match ("(p2:Book)-[:BOOK_ADDED_TO_CATALOG]->(catalog2:Catalog)")
        .Where ("p1 <> p2")
        .With ("p1, p1Catalog, p2, collect(id(catalog2)) AS p2Catalog")
        .Call ("algo.similarity.jaccard(p1Catalog, p2Catalog)")
        .Yield ("value AS similiarity")
        .Return ((p1, p2, similiarity) => new {
          bk1 = p1.As<BookDetailsDto> (),
          bk2 = p2.As<BookDetailsDto> (),
        })
        .Limit (10);

不幸的是,当我通过API调用它时,出现以下错误:

  

处理请求时发生未处理的异常。   NeoException:SyntaxException:类型不匹配:预期的List,List或List但为List(第6行,第30列(偏移量:327))   CALL algo.similarity.jaccard(p1Catalog,p2Catalog)

neo4jClient构建的密码查询与顶部的原始查询完全不同:

MATCH (p1:Book{friendlyUrl:'harry-potter-and-the-deathly-hallows'})-[r:BOOK_ADDED_TO_CATALOG]->(catalog1:Catalog)
WITH p1, collect(id(catalog1)) AS p1Catalog
MATCH (p2:Book)-[:BOOK_ADDED_TO_CATALOG]->(catalog2:Catalog)
WHERE p1 <> p2
WITH p1, p1Catalog, p2, collect(id(catalog2)) AS p2Catalog
CALL algo.similarity.jaccard(p1Catalog, p2Catalog)
YIELD value AS similiarity
RETURN p1 AS bk, p2 AS bk2
LIMIT 10

1 个答案:

答案 0 :(得分:1)

好的,所以这有两部分-密码不匹配的原因以及错误的含义-它们都相关,但是我们将首先解决密码的差异-这样您就可以得到结果

在您的Cypher中,您不需要做CALL-您只需RETURN Jaccard,为了获得对Cypher的真实反映,您需要:

var result = _graphClient.Cypher
    .Match ("(p1:Book{friendlyUrl:'a-modern-witch'})-[r:BOOK_ADDED_TO_CATALOG]->(catalog1:Catalog)")
    .With ("p1, collect(id(catalog1)) AS p1Catalog")
    .Match ("(p2:Book)-[:BOOK_ADDED_TO_CATALOG]->(catalog2:Catalog)")
    .Where ("p1 <> p2")
    .With ("p1, p1Catalog, p2, collect(id(catalog2)) AS p2Catalog")
    .Return ((p1, p2, similiarity) => new {
      bk1 = p1.As<BookDetailsDto> (),
      bk2 = p2.As<BookDetailsDto> (),
      similarity = Return.As<double>("algo.similarity.jaccard(p1Catalog, p2Catalog)")
    })
    .OrderByDescending("similarity")
    .Limit (10);

其中的关键是删除.Call错误,调​​用以及在算法调用中使用Return.As<>

第二部分-(您得到的错误消息)与此细微变化直接相关,通过使用CALL,您可以从使用Jaccard的“功能”版本切换到“程序”版本-他们拥有不同的签名,功能版本如下所示:

"algo.similarity.jaccard(vector1 :: LIST? OF NUMBER?, vector2 :: LIST? OF NUMBER?) :: (FLOAT?)"

因此,它期望LIST? OF NUMBER?输出COLLECT(id(catalog))。您可以通过在浏览器中运行以下Cypher来获取此信息:

CALL dbms.functions() YIELD name, signature, description
WHERE name CONTAINS 'jaccard'
RETURN *

现在,Prodedure版本如下:

"algo.similarity.jaccard(data = null :: LIST? OF MAP?, config = {} :: MAP?) :: (nodes :: INTEGER?, sourceNodes :: INTEGER?, targetNodes :: INTEGER?, similarityPairs :: INTEGER?, computations :: INTEGER?, write :: BOOLEAN?, writeRelationshipType :: STRING?, writeProperty :: STRING?, min :: FLOAT?, max :: FLOAT?, mean :: FLOAT?, stdDev :: FLOAT?, p25 :: FLOAT?, p50 :: FLOAT?, p75 :: FLOAT?, p90 :: FLOAT?, p95 :: FLOAT?, p99 :: FLOAT?, p999 :: FLOAT?, p100 :: FLOAT?)"

正在寻找LIST? OF MAP?-不是您所获得的CALL dbms.procedures() YIELD name, signature, description WHERE name CONTAINS 'jaccard' RETURN * -错误消息。

此密码为:

{{1}}

Anyhews-第一个C#代码应该可以使您排序。