如何 运行 进行多匹配搜索而不返回 null?
How can I run a multiple-MATCH search without returning null?
嗨,如果您想了解我将要参考的图表,我有一个示例查询:
CREATE (u:User {name: "Tim Cook"})-[:FOLLOWS]->(t:Thing {name: "Italy"}), (t)<-[:TAGGED]-(p:Post {name: "Post 1"})<-[:WROTE]-(u1:User {name: "Elon Musk"}), (u)-[:FOLLOWS]->(u2:User {name: "Oprah"})-[:WROTE]->(p1:Post {name: "Post 2"}), (u)-[:FOLLOWS]->(u3:User {name: "Joe Biden"})-[:WROTE]->(p2:Post {name: "Post 3"})
也在这里:
我正在尝试获取所有 post 用户直接关注作者(“写”post 的人)或用户关注被“标记”的事物的所有 posts在 post.
所以在示例查询中,如果你是 Tim Cook,你应该收到 Posts 2 和 3 因为你直接关注作者,而 Post 1 因为你关注被标记的意大利在那 post.
到处找,好难找。降落在这里。我正在使用“可选”,这样如果用户没有关注任何人,第一个不会崩溃整个查询,或者如果他们没有关注任何东西,第二个不会崩溃。但是,按照我下面的操作方式,它 returns Null 对于任何一个为空的查询。
所以如果第一部分没有匹配项,第二部分没有匹配项 6,我最终得到一个长度为 7 的数组。[null, post1, post2, .. .post6].
请问我应该怎么做呢?提前致谢!
OPTIONAL MATCH (this)-[:FOLLOWS]->(u1)-[:WROTE]->(post:Post)
RETURN post
ORDER BY post.id
UNION
OPTIONAL MATCH (this)-[:FOLLOWS]->(t:Thing)<-[:TAGGED]-(post:Post)
RETURN post
ORDER BY post.id
this
指的是上例中的Tim Cook用户节点。
如果用户关注作者(“WROTE”的用户),我也想 return。所以我想我可以添加一个“EXISTS”或者有没有办法指定哪个查询 returned a post?例如。如果 post 是由上面的第一个 MATCH return 编辑的,那么他们显然跟随了作者。如果 return 是在第二个而不是第一个,那么他们就不会。解决此问题的最佳方法是什么 query/how 我应该改为这样做吗?提前致谢!
您可以将路径中间的节点(用户或标签)视为通用对象,从而从那里使用多关系类型模式匹配:
MATCH (u:User {name: "Tim Cook"})
MATCH (u)-[:FOLLOWS]->(thing)-[:WROTE|TAGGED]-(p:Post)
RETURN p.name
╒════════╕
│"p.name"│
╞════════╡
│"Post 3"│
├────────┤
│"Post 2"│
├────────┤
│"Post 1"│
└────────┘
```
From there, if you want to know if the user is following the author, it's just plain logic, you return if the node in the middle is of type 'User' (which would validate the assumption since the first relationship is FOLLOWS thing)
```
MATCH (u:User {name: "Tim Cook"})
MATCH (u)-[:FOLLOWS]->(thing)-[:WROTE|TAGGED]-(p:Post)
RETURN p.name, ('User' IN labels(thing)) AS followingAuthor
╒════════╤═════════════════╕
│"p.name"│"followingAuthor"│
╞════════╪═════════════════╡
│"Post 3"│true │
├────────┼─────────────────┤
│"Post 2"│true │
├────────┼─────────────────┤
│"Post 1"│false │
└────────┴─────────────────┘
```
嗨,如果您想了解我将要参考的图表,我有一个示例查询:
CREATE (u:User {name: "Tim Cook"})-[:FOLLOWS]->(t:Thing {name: "Italy"}), (t)<-[:TAGGED]-(p:Post {name: "Post 1"})<-[:WROTE]-(u1:User {name: "Elon Musk"}), (u)-[:FOLLOWS]->(u2:User {name: "Oprah"})-[:WROTE]->(p1:Post {name: "Post 2"}), (u)-[:FOLLOWS]->(u3:User {name: "Joe Biden"})-[:WROTE]->(p2:Post {name: "Post 3"})
也在这里:
我正在尝试获取所有 post 用户直接关注作者(“写”post 的人)或用户关注被“标记”的事物的所有 posts在 post.
所以在示例查询中,如果你是 Tim Cook,你应该收到 Posts 2 和 3 因为你直接关注作者,而 Post 1 因为你关注被标记的意大利在那 post.
到处找,好难找。降落在这里。我正在使用“可选”,这样如果用户没有关注任何人,第一个不会崩溃整个查询,或者如果他们没有关注任何东西,第二个不会崩溃。但是,按照我下面的操作方式,它 returns Null 对于任何一个为空的查询。
所以如果第一部分没有匹配项,第二部分没有匹配项 6,我最终得到一个长度为 7 的数组。[null, post1, post2, .. .post6].
请问我应该怎么做呢?提前致谢!
OPTIONAL MATCH (this)-[:FOLLOWS]->(u1)-[:WROTE]->(post:Post)
RETURN post
ORDER BY post.id
UNION
OPTIONAL MATCH (this)-[:FOLLOWS]->(t:Thing)<-[:TAGGED]-(post:Post)
RETURN post
ORDER BY post.id
this
指的是上例中的Tim Cook用户节点。
如果用户关注作者(“WROTE”的用户),我也想 return。所以我想我可以添加一个“EXISTS”或者有没有办法指定哪个查询 returned a post?例如。如果 post 是由上面的第一个 MATCH return 编辑的,那么他们显然跟随了作者。如果 return 是在第二个而不是第一个,那么他们就不会。解决此问题的最佳方法是什么 query/how 我应该改为这样做吗?提前致谢!
您可以将路径中间的节点(用户或标签)视为通用对象,从而从那里使用多关系类型模式匹配:
MATCH (u:User {name: "Tim Cook"})
MATCH (u)-[:FOLLOWS]->(thing)-[:WROTE|TAGGED]-(p:Post)
RETURN p.name
╒════════╕
│"p.name"│
╞════════╡
│"Post 3"│
├────────┤
│"Post 2"│
├────────┤
│"Post 1"│
└────────┘
```
From there, if you want to know if the user is following the author, it's just plain logic, you return if the node in the middle is of type 'User' (which would validate the assumption since the first relationship is FOLLOWS thing)
```
MATCH (u:User {name: "Tim Cook"})
MATCH (u)-[:FOLLOWS]->(thing)-[:WROTE|TAGGED]-(p:Post)
RETURN p.name, ('User' IN labels(thing)) AS followingAuthor
╒════════╤═════════════════╕
│"p.name"│"followingAuthor"│
╞════════╪═════════════════╡
│"Post 3"│true │
├────────┼─────────────────┤
│"Post 2"│true │
├────────┼─────────────────┤
│"Post 1"│false │
└────────┴─────────────────┘
```