简体   繁体   English

在Neo4j密码查询中匹配为逻辑AND / OR

[英]Match as logical AND/OR in Neo4j cypher queries

I am new to Neo4j and I need to query a matching graph or any subgraph as follows: 我是Neo4j的新手,我需要查询匹配的图或任何子图,如下所示:

查询图

So I tried with OPTIONAL MATCH but I realized it would not provide me the required results. 因此,我尝试使用OPTIONAL MATCH,但我意识到它无法为我提供所需的结果。 This is the query I tried previously. 这是我之前尝试过的查询。

OPTIONAL MATCH (w:W)-[:WRITES]->(a1:A{name:"A1"})
WITH w,a1
OPTIONAL MATCH (w)-[:WRITES]->(a2:A{name:"A2"})
WITH w,a1,a2
OPTIONAL MATCH (w)-[:WRITES]->(a3:A{name:"A3"})
WITH w,a1,a2,a3
OPTIONAL MATCH (w)-[:WRITES]->(a4:A{name:"A4"})
RETURN w,a1,a2,a3,a4

Though it is an optional match, it is still a logical AND match with outer join (in the context of SQL) 尽管它是一个可选的匹配,但它仍然是与外部联接的逻辑AND匹配(在SQL上下文中)

In my case, I need both OR and AND results in each match which means it should match any subgraph of the above interpreted graph. 就我而言,每次匹配都需要OR和AND结果,这意味着它应该与上述解释图的任何子图都匹配。

So, I need to fetch results something as follows. 因此,我需要获取结果如下。

MATCH (w:W)-[:WRITES]->(a1:A{name:"A1"})

OR/AND 或/与

MATCH (w)-[:WRITES]->(a2:A{name:"A2"})

OR/AND 或/与

MATCH (w)-[:WRITES]->(a3:A{name:"A3"})

OR/AND 或/与

MATCH (w)-[:WRITES]->(a4:A{name:"A4"})
RETURN w,a1,a2,a3,a4

Is there any possibility to achieve my requirement through Neo4j cypher queries? 通过Neo4j密码查询是否有可能达到我的要求?

My results set should be look like this. 我的结果集应如下所示。

W, A1, A2, A3, A4
{name:w1,....}, {name:A1,....}, {name:A2,....}, {name:A3,....}, {name:A4,....}
{name:w1,....}, null, {name:A2,....}, null, {name:A4,....}
{name:w1,....}, null, null, null, {name:A4,....}
{name:w1,....}, {name:A1,....}, {name:A2,....}, null, null
{name:w1,....}, {name:A1,....}, null, {name:A3,....}, {name:A4,....}

If you want any :W nodes that are connected with a :WRITES relationship to any of the given :A nodes, then this query should work: 如果要将与:WRITES关系连接的任何:W节点连接到给定的:A节点中的任何一个,则此查询应该有效:

MATCH (w:W)-[:WRITES]->(a:A)
WHERE a.name in ["A1", "A2", "A3", "A4"]
RETURN w, a

EDIT 编辑

Ah, okay, so if I'm reading your update correctly, you want any :W node connected to one of these :A nodes, but then you want to know which of those :A nodes that node is connected to, and you want each of those :A nodes to have its own variable (this is the requirement that makes it a little tricky). 嗯,好吧,所以如果我正确地读取了更新,则您希望将任何:W节点连接到这些:A节点之一,但是您想知道该节点连接到了那些:A节点中的哪一个,并且您想要每个:A节点都有自己的变量(这使它变得有些棘手)。

You can use some variation of my above query to find the w node then use OPTIONAL MATCHES from w as in your original query to get the answer. 您可以使用上述查询的某种变体来找到w节点,然后像原始查询中一样使用w的可选匹配来获取答案。 Alternately, you can use something like the below query: 或者,您可以使用类似以下查询的内容:

MATCH (a1:A{name:"A1"}), (a2:A{name:"A2"}), (a3:A{name:"A3"}), (a4:A{name:"A4"})
UNWIND [a1, a2, a3, a4] as a
MATCH (w:W)-[:WRITES]->(a)
WITH a1, a2, a3, a4, w, collect(a) as connectedNodes
WITH w, CASE WHEN a1 in connectedNodes THEN a1 ELSE null END as a1, 
        CASE WHEN a2 in connectedNodes THEN a2 ELSE null END as a2,
        CASE WHEN a3 in connectedNodes THEN a3 ELSE null END as a3,
        CASE WHEN a4 in connectedNodes THEN a4 ELSE null END as a4
RETURN w, a1, a2, a3, a4

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

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