gpt4 book ai didi

neo4j - 如何在 Neo4j 中使用带有 `with` 的 `call.apoc.do.when` 语句?

转载 作者:行者123 更新时间:2023-12-04 08:56:22 24 4
gpt4 key购买 nike

首先,您可以使用以下命令创建示例节点,

CREATE (t1:Title { tid: '123abc'})
CREATE (t2:Title { tid: '123def'})
CREATE (t3:Title { tid: '123456'})
CREATE (t4:Title { tid: '123789'})
CREATE (u:User { pid: '456def'})

CREATE (t1)-[r:TO]->(t2)
CREATE (t2)-[r:TO]->(t3)
CREATE (t3)-[r:TO]->(t4)
CREATE (u)-[r:LIKE]->(t3)
这是图表,
graph
我在 Neo4j 中有一个类似的查询,
MATCH (t1:Title)-[r:TO]->(t2:Title)
WHERE t1.tid = '123abc'
AND NOT exists((t2)<-[:LIKE|:DISLIKE|:LOVE]-(:User {pid: '456def'}))
WITH COLLECT(r) AS rels
LIMIT 1

WITH rels
CALL apoc.do.when(
SIZE(rels) > 0,
'RETURN REDUCE(total = 0, x IN rels | total + x.weight) AS result',
'RETURN 0 AS result'
) YIELD value
RETURN value.result
我想在 rels 博客中使用 do.when 变量。 SIZE(rels) > 0 表达式没有问题,但是在 if statement 中返回这样的错误,

Failed to invoke procedure apoc.do.when: Caused by: org.neo4j.exceptions.SyntaxException: Variable rels not defined (line 1, column 31 (offset: 30))"RETURN REDUCE(total = 0, x IN rels | total + x.weight) AS result"



我是 Neo4j 的新手。我认为, REDUCE 有一个特殊情况。我怎么解决这个问题?提前致谢。

最佳答案

查看 documentation ,您可以添加包含内部语句参数的第三个参数:

MATCH (t1:Title)-[r:TO]->(t2:Title)
WHERE t1.tid = '123abc'
AND NOT exists((t2)<-[:LIKE|:DISLIKE|:LOVE]-(:User {pid: '456def'}))
WITH COLLECT(r) AS rels
LIMIT 1

WITH rels
CALL apoc.do.when(
SIZE(rels) > 0,
'RETURN REDUCE(total = 0, x IN rels | total + x.weight) AS result',
'RETURN 0 AS result',
{rels:rels}
) YIELD value
RETURN value.result

关于neo4j - 如何在 Neo4j 中使用带有 `with` 的 `call.apoc.do.when` 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63809594/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com