gpt4 book ai didi

neo4j - 使用 Cypher Neo4J 进行 IF...ELSE

转载 作者:行者123 更新时间:2023-12-03 01:30:04 24 4
gpt4 key购买 nike

Cypher 中的 IF/ELSE 语句的语法是否有任何更新?

我知道CASEFOREACH“黑客”,但它们读起来很难看:)

我想用可选参数做一些事情,例如:

CASE WHEN exists($refs.client) THEN MATCH (cl:client {uuid: $refs.client}) END
...

// and later use it like
CASE WHEN exists(cl) THEN DELETE tcr MERGE (t)-[:references]->(cl) END

// and again in my return
RETURN {
client: CASE WHEN exists(cl) THEN {uuid: cl.uuid} ELSE NULL END,
}

我知道这在上下文中没有多大意义,但我基本上传递了一个 refs 对象,它可能包含也可能不包含参数(或者参数存在并且为 NULL)

我在某处读到,可能有关于 Neo4j 中如何处理“if/else”的更新,所以我真的只是想检查一下,看看是否有人知道处理此类情况的“更好”方法。

目前,我只是在代码中处理所有查询并运行一堆较小的查询,但它需要重复查找来创建和删除引用。我想将其全部移至一个更大的查询中,以便我可以使用变量引用。

再说一次,我知道我可以使用 FOREACH...CASE,但是当有很多像这样的较小情况时,它就会变得很麻烦。

目前错误是

{ Error: Invalid input 'S': expected 'l/L' (line 7, column 9 (offset: 246))
" CASE true WHEN exists($refs.client) THEN MATCH (cl:client {uuid: $refs.client}) END"
^

我还知道,如果我传回已知值,则可以使用 WITH...CASE,但不能在其中执行 MATCH

想要在查询顶部的 CASE 内执行 MATCH 的原因之一是,如果 refs 上的属性存在,我希望查询失败但 MATCH 没有成功。使用OPTIONAL MATCH无法实现此目的。

编辑哦,还有...我正在使用MATCH (cl:client {uuid: $refs.client}) WHERE isn't($refs.client) 但我记得它无法正常工作。

编辑我可以做 MATCH...WHERE contains() 但以后如果我不能做 MERGE WHEREexists()

编辑为了说明我为什么询问 IF/ELSE,以下是我正在查看的查询。我对上面的示例进行了修改,这样就不会出错。

MATCH (u:user {uuid: $uid})-[:allowed_to {read: true}]->(c:company {uuid: $cid})
MATCH (t:timesheet {uuid: $tid})<-[:owns]-(:timesheets)<-[:owns]-(u)

// Make sure the incoming references are valid or fail query
// Here, I'd like only do a match IF $refs.client exists and IS NOT NULL. If it is null or does not exist, I don't want the query to fail. OPTIONAL MATCH will not fail if the value is passed in is invalid but will simply return NULL. Which is why IF/ELSE (or CASE) would be helpful here.
MATCH (cl:client {uuid: $refs.client})
MATCH (ca:case {uuid: $refs.case})
MATCH (s:step {uuid: $refs.step})
MATCH (n:note {uuid: $refs.note})

// clone timesheet entry to a revision
CREATE (t)-[:assembled_with]->(r:revision)
SET r = t, r.created_at = $data.updated_at

WITH *

// Get the old references
MATCH (t)-[tcr:references]->(rc:client)
MATCH (t)-[tcar:references]->(rca:case)
MATCH (t)-[tsr:references]->(rs:step)
MATCH (t)-[tnr:references]->(rn:note)

// Copy old references to revision (won't create new relationships with NULL)
MERGE (r)-[:references]->(rc)
MERGE (r)-[:references]->(rca)
MERGE (r)-[:references]->(rs)
MERGE (r)-[:references]->(rn)

// Update the current timesheet with new data
SET t += $data

// If new references are incoming, delete the old ones and update for new ones
DELETE tcr
DELETE tcar
DELETE tsr
DELETE tnr
MERGE (t)-[:references]->(cl)
MERGE (t)-[:references]->(ca)
MERGE (t)-[:references]->(s)
MERGE (t)-[:references]->(n)

WITH *

// Get the new count of revisions
MATCH (t)-[:assembled_with]->(_r:revision)

RETURN {
uuid: t.uuid,
start: t.start,
end: t.end,
description: t.description,
client: CASE WHEN exists(cl.uuid) THEN {uuid: cl.uuid} ELSE NULL END,
case: CASE WHEN exists(ca.uuid) THEN {uuid: ca.uuid} ELSE NULL END,
step: CASE WHEN exists(s.uuid) THEN {uuid: s.uuid} ELSE NULL END,
note: CASE WHEN exists(n.uuid) THEN {uuid: n.uuid} ELSE NULL END,
revisions: count(_r)
}

最佳答案

APOC Procedures刚刚更新支持 conditional cypher execution 。您需要 3.1.3.7 或更高版本(如果使用 Neo4j 3.1.x),或者 3.2.0.3 或更高版本(如果使用 Neo4j 3.2.x)。

以下是您提到的使用新程序的一些案例的示例:

CALL apoc.when($refs.client IS NOT NULL, 
"MATCH (cl:client {uuid: refs.client}) RETURN cl", '', {refs:$refs}) YIELD value
WITH value.cl as cl // which might be null...
...

...
CALL apoc.do.when(cl IS NOT NULL,
"DELETE tcr
MERGE (t)-[:references]->(cl)", '', {tcr:tcr, t:t, cl:cl}) YIELD value
...

...
RETURN {
client: cl {.uuid}, ...
}

在您的返回中, map 投影足以满足您的需求,如果 cl 存在,您将获得一个带有 uuid 的对象,如果不存在,则 client 为 null .

关于neo4j - 使用 Cypher Neo4J 进行 IF...ELSE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43481472/

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