如果在Memgraph中的可选匹配的结果不满足特定条件,如何返回空值?

huangapple go评论69阅读模式
英文:

How to return nothing if the results of an optional match do not meet certain conditions in Memgraph?

问题

我目前正在处理一个涉及到图数据库中特定类型查询的任务。这个任务要求我执行一个可选匹配操作,该操作旨在检索可能存在或不存在于数据库中的节点和关系。

如果可选匹配操作找到了数据,我不希望查询无条件地返回这些结果。相反,结果必须满足特定条件,然后才能包含在最终输出中。换句话说,即使与可选匹配子句匹配的数据可能存在,我希望我的查询在数据满足一些附加条件之前实际上不起作用,除非数据也符合一些附加条件。如果检索到的数据不符合这些条件,整个查询不应返回结果。

这是可以实现的吗?

我已经开始了这段代码:

MATCH (originServer:Server{ip:'192.168.0.1'})-

->(trafficLimit:BandwidthCap)
MATCH (trafficLimit)-[endLink:Dest]->(destinationServer:Server{ip:'192.168.0.2'})
OPTIONAL MATCH (trafficLimit:BandwidthCap)-[viaPath:Via]->(intermediateRouter:Router)
WHERE (intermediateRouter IS NULL) OR any(i in ['Cisco'] where i = intermediateRouter.brand)
RETURN originServer, link, trafficLimit, endLink, destinationServer, viaPath, intermediateRouter;

经过额外的调整,我得到了这个代码 - 但它只返回了ELSE部分的NULL - 无法弄清楚原因。

MATCH (originServer:Server{ip:'192.168.0.1'})-

->(trafficLimit:BandwidthCap)
MATCH (trafficLimit)-[endLink:Dest]->(destinationServer:Server{ip:'192.168.0.2'})
OPTIONAL MATCH (trafficLimit:BandwidthCap)-[viaPath:Via]->(intermediateRouter:Router)
OPTIONAL MATCH (trafficLimit:BandwidthCap)-[exceptPath:Via]->(exceptionRouter:Router)
RETURN
CASE trafficLimit
WHEN intermediateRouter IS NULL AND exceptionRouter IS NULL THEN trafficLimit.cap
WHEN intermediateRouter = "Cisco" THEN trafficLimit.cap
ELSE NULL
END

英文:

I'm currently working on a task that involves a specific type of query in a graph database. The task requires me to perform an optional match operation, which is designed to retrieve nodes and relationships that may or may not exist in the database.

If the optional match operation does find data, I don't want the query to return those results unconditionally. Instead, the results must fulfill certain specific conditions before they're included in the final output. In other words, even though the data matching the optional match clause might exist, I want my query to effectively act as if it doesn't, unless the data also meets some additional criteria. If the retrieved data does not meet these criteria, the overall query should not return a result.

Is this something that can be achieved?

I've started with this code:

MATCH (originServer:Server{ip:'192.168.0.1'})-
->(trafficLimit:BandwidthCap) MATCH (trafficLimit)-[endLink:Dest]->(destinationServer:Server{ip:'192.168.0.2'}) OPTIONAL MATCH (trafficLimit:BandwidthCap)-[viaPath:Via]->(intermediateRouter:Router) WHERE (intermediateRouter IS NULL) OR any(i in ['Cisco'] where i = intermediateRouter.brand) RETURN originServer, link, trafficLimit, endLink, destinationServer, viaPath, intermediateRouter;

After additional tweaking, I've arrived at this - but it only returns the NULL from the ELSE section - can't figure out why.

MATCH (originServer:Server{ip:'192.168.0.1'})-
->(trafficLimit:BandwidthCap) MATCH (trafficLimit)-[endLink:Dest]->(destinationServer:Server{ip:'192.168.0.2'}) OPTIONAL MATCH (trafficLimit:BandwidthCap)-[viaPath:Via]->(intermediateRouter:Router) OPTIONAL MATCH (trafficLimit:BandwidthCap)-[exceptPath:Via]->(exceptionRouter:Router) RETURN CASE trafficLimit WHEN intermediateRouter IS NULL AND exceptionRouter IS NULL THEN trafficLimit.cap WHEN intermediateRouter = "Cisco" THEN trafficLimit.cap ELSE NULL END

答案1

得分: 0

在查看您的问题后,我发现错误源于您查询中的关系 exceptPath,看起来您无意中将其分配为 :Via 类型,而不是正确的 :ExceptVia 类型。这种错误分类会导致条件始终满足标准,导致永久性的 'true' 评估。为解决此问题,我建议将关系更正为其正确类型 :ExceptVia。在进行此调整后,您的代码应该按预期运行。以下是已更正版本的示例:

MATCH (originServer:Server{ip:'192.168.0.1'})-
->(trafficLimit:BandwidthCap) MATCH (trafficLimit)-[endLink:Dest]->(destinationServer:Server{ip:'192.168.0.2'}) OPTIONAL MATCH (trafficLimit:BandwidthCap)-[viaPath:Via]->(intermediateRouter:Router) OPTIONAL MATCH (trafficLimit:BandwidthCap)-[exceptPath:ExceptVia]->(exceptionRouter:Router) RETURN CASE WHEN intermediateRouter IS NULL AND exceptionRouter IS NULL THEN trafficLimit.cap WHEN exceptionRouter IS NULL AND any(n in ['Cisco'] WHERE n = intermediateRouter.brand) THEN trafficLimit.cap, trafficLimit.id WHEN intermediateRouter IS NULL AND NOT any(n in ['Cisco'] WHERE n = exceptionRouter.brand) THEN trafficLimit.cap ELSE NULL END AS cap

此Cypher查询考虑了更正后的关系类型。

英文:

Looking over your problem, it appears to me that the error originates from the relationship exceptPath in your query, which you seem to have unintentionally assigned as a :Via type, instead of the correct :ExceptVia type. This misclassification would cause the condition to meet the criteria consistently, resulting in a perpetual 'true' evaluation. To resolve this issue, I would suggest revising the relationship to its correct type :ExceptVia. After making this adjustment, your code should function as expected. Here is how the corrected version might look:

MATCH (originServer:Server{ip:'192.168.0.1'})-
->(trafficLimit:BandwidthCap) MATCH (trafficLimit)-[endLink:Dest]->(destinationServer:Server{ip:'192.168.0.2'}) OPTIONAL MATCH (trafficLimit:BandwidthCap)-[viaPath:Via]->(intermediateRouter:Router) OPTIONAL MATCH (trafficLimit:BandwidthCap)-[exceptPath:ExceptVia]->(exceptionRouter:Router) RETURN CASE WHEN intermediateRouter IS NULL AND exceptionRouter IS NULL THEN trafficLimit.cap WHEN exceptionRouter IS NULL AND any(n in ['Cisco'] WHERE n = intermediateRouter.brand) THEN trafficLimit.cap, trafficLimit.id WHEN intermediateRouter IS NULL AND NOT any(n in ['Cisco'] WHERE n = exceptionRouter.brand) THEN trafficLimit.cap ELSE NULL END AS cap

This Cypher query takes into account the revised relationship type.

huangapple
  • 本文由 发表于 2023年7月31日 20:20:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/76803560.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定