简体   繁体   English

如何在 Neo4j cypher 查询中使用模式理解对关系大小进行条件检查

[英]How to use pattern comprehension for conditional check on relationship size in Neo4j cypher query

Using the following query I was able to make condition query statement based on relationship count.使用以下查询,我能够根据关系计数制作条件查询语句。

WITH ( (:LABEL_X)<-[]-(:LABEL_Y) ) AS res
CALL apoc.do.when(size( res ) = 2,
    "MATCH (c:LABEL_Y)  RETURN c",
    "MATCH (mu:LABEL_X)<-[mr]-(c:LABEL_Y) RETURN mu,mr,c",
{res:res}) YIELD value
RETURN value

However, this shows warning lint as below但是,这显示警告棉绒如下

This feature is deprecated and will be removed in future versions.此功能已弃用,将在未来版本中删除。

A pattern expression should only be used in order to test the existence of a pattern.模式表达式应该只用于测试模式的存在。 It should therefore only be used in contexts that evaluate to a boolean, eg inside the function exists() or in a WHERE-clause.因此,它应该只用在计算结果为 boolean 的上下文中,例如在 function exists() 内部或 WHERE 子句中。 All other uses are deprecated and should be replaced by a pattern comprehension.所有其他用途均已弃用,应由模式理解代替。

So, please suggest a way to get same result using pattern comprehension.因此,请提出一种使用模式理解获得相同结果的方法。 From the neo4j docs pattern comprehension, I see most of the things are in the RETURN statement.neo4j 文档模式理解中,我看到大部分内容都在 RETURN 语句中。 whereas, in my case I need to it before the apoc execution in order to decide on what query to execute in apoc.do.when call.然而,在我的例子中,我需要在 apoc 执行之前执行它,以便决定在 apoc.do.when 调用中执行什么查询。

Or suggest if you know any other way to achieve this.或者建议您是否知道任何其他方法来实现这一目标。

You can use it like this:你可以像这样使用它:

WITH [x = (:LABEL_X)<-[]-(:LABEL_Y) | x] AS res
CALL apoc.do.when(size( res ) = 2,
    "MATCH (c:LABEL_Y)  RETURN c",
    "MATCH (mu:LABEL_X)<-[mr]-(c:LABEL_Y) RETURN mu,mr,c",
{res:res}) YIELD value
RETURN value

Or simply calculate size directly, like this:或者直接计算大小,像这样:

WITH size([x = (:LABEL_X)<-[]-(:LABEL_Y) | x]) AS numberOfPaths
CALL apoc.do.when( numberOfPaths  = 2,
    "MATCH (c:LABEL_Y)  RETURN c",
    "MATCH (mu:LABEL_X)<-[mr]-(c:LABEL_Y) RETURN mu,mr,c",
{numberOfPaths:numberOfPaths}) YIELD value
RETURN value

You are counting (via size) the number of relationships between LABEL_X and LABEL_Y.您正在(通过大小)计算 LABEL_X 和 LABEL_Y 之间的关系数。 If the size equals 2 then you will return LABEL_Y else you will return LABEL_X, LABEL_Y and their relationships.如果大小等于 2,那么您将返回 LABEL_Y,否则您将返回 LABEL_X、LABEL_Y 及其关系。

This expression:这个表达式:

size((:LABEL_X)<-[]-(:LABEL_Y)) 

is the same with是一样的

MATCH res=(:Price_Point)-[]-(:Headphone)  
WITH size(collect(relationships(res))) as res 

Thus below query will avoid the warning error about deprecation of pattern expression因此下面的查询将避免关于弃用模式表达式的警告错误

MATCH res=(:LABEL_X)<-[]-(:LABEL_Y)
WITH size(collect(relationships(res))) as res
CALL apoc.do.when(
    res = 2,
    "MATCH (c:LABEL_Y)  RETURN c",
    "MATCH (mu:LABEL_X)<-[mr]-(c:LABEL_Y) RETURN mu, mr, c",
    {}
    ) YIELD value
RETURN value

Note that I removed the param {res:res} because it is not used anyway.请注意,我删除了参数 {res:res} 因为无论如何它都没有被使用。 Parameters is a map while in your query res is a pattern expression which is a list of lists.参数是 map 而在您的查询中 res 是一个模式表达式,它是一个列表列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM