首页 > 其他分享 >Cypher中多个match表示pipe效果 一个match查询结果给另外一个match使用

Cypher中多个match表示pipe效果 一个match查询结果给另外一个match使用

时间:2023-08-09 15:12:28浏览次数:43  
标签:Cypher name Wall pipe Street match Sheen OPTIONAL MATCH

https://neo4j.com/docs/cypher-manual/current/clauses/optional-match/

OPTIONAL MATCH

Introduction

OPTIONAL MATCH matches patterns against a graph database, just as MATCH does. The difference is that if no matches are found, OPTIONAL MATCH will use a null for missing parts of the pattern. OPTIONAL MATCH could therefore be considered the Cypher® equivalent of the outer join in SQL.

When using OPTIONAL MATCH, either the whole pattern is matched, or nothing is matched. The WHERE clause is part of the pattern description, and its predicates will be considered while looking for matches, not after. This matters especially in the case of multiple (OPTIONALMATCH clauses, where it is crucial to put WHERE together with the MATCH it belongs to.

To understand the patterns used in the OPTIONAL MATCH clause, read Patterns.

OPTIONAL MATCH就像将模式与图形数据库进行匹配一样MATCH。不同之处在于,如果未找到匹配项,OPTIONAL MATCH将使用 null来表示模式中缺失的部分。 OPTIONAL MATCH因此,可以将 Cypher ®视为SQL 中外连接的等价物。

使用 时OPTIONAL MATCH,要么匹配整个模式,要么不匹配任何内容。该WHERE子句是模式描述的一部分,其谓词将在查找匹配时而不是之后考虑。OPTIONAL这在有多个 ( )子句的情况下尤其重要,将其与其所属的MATCH放在一起至关重要。

Example graph

The following graph is used for the examples below:

graph optional match clause

To recreate the graph, run the following query in an empty Neo4j database:

 
CREATE
  (charlie:Person {name: 'Charlie Sheen'}),
  (martin:Person {name: 'Martin Sheen'}),
  (michael:Person {name: 'Michael Douglas'}),
  (oliver:Person {name: 'Oliver Stone'}),
  (rob:Person {name: 'Rob Reiner'}),
  (wallStreet:Movie {title: 'Wall Street'}),
  (charlie)-[:ACTED_IN]->(wallStreet),
  (martin)-[:ACTED_IN]->(wallStreet),
  (michael)-[:ACTED_IN]->(wallStreet),
  (oliver)-[:DIRECTED]->(wallStreet),
  (thePresident:Movie {title: 'The American President'}),
  (martin)-[:ACTED_IN]->(thePresident),
  (michael)-[:ACTED_IN]->(thePresident),
  (rob)-[:DIRECTED]->(thePresident),
  (martin)-[:FATHER_OF]->(charlie)

OPTIONAL MATCH in more detail

Like SQL, Cypher queries are constructed using various clauses which are chained together to feed intermediate results between each other. For example, the matching variables from one MATCH clause will provide the context in which the next clause exists. However, there are two important differences between Neo4j and SQL which helps to explain OPTIONAL MATCH further.

  1. While it is both possible and advised to enforce partial schemas using indexes and constraints, Neo4j offers a greater degree of schema flexibility than a relational database. Nodes and relationships in a Neo4j database do not have to have a specific property set to them because other nodes or relationships in the same graph have that property (unless there is a existence constraint created on the specific property).

  2. Queries in Cypher are run as pipelines. If a clause returns no results, it will effectively end the query as subsequent clauses will have no data to execute upon.

For example, the following query returns no results:

与 SQL 一样,Cypher 查询是使用各种子句构建的,这些子句链接在一起以在彼此之间提供中间结果。例如,一个MATCH子句中的匹配变量将提供下一个子句存在的上下文。然而,Neo4j 和 SQL 之间有两个重要的区别,这有助于OPTIONAL MATCH进一步解释。

  1. 虽然可以并且建议使用索引和约束来强制执行部分模式,但 Neo4j 提供了比关系数据库更大程度的模式灵活性。Neo4j 数据库中的节点和关系不必设置特定的属性,因为同一图中的其他节点或关系具有该属性(除非在特定属性上创建了存在约束)。

  2. Cypher 中的查询作为管道运行。如果一个子句没有返回结果,它将有效地结束查询,因为后续子句将没有数据可执行。

例如,以下查询不返回结果:

 
MATCH (a:Person {name: 'Martin Sheen'})
MATCH (a)-[r:DIRECTED]->()
RETURN a.name, r
 
(no changes, no records)

This is because the second MATCH clause returns no data (there are no DIRECTED relationships connected to Martin Sheen in the graph) to pass on to the RETURN clause.

However, replacing the second MATCH clause with OPTIONAL MATCH does return results. This is because, unlike MATCHOPTIONAL MATCH enables the value null to be passed between clauses.

 

这是因为第二个MATCH子句没有返回任何数据(图中没有DIRECTED连接到的关系Martin Sheen)以传递到该RETURN子句。

但是,将第二个MATCH子句替换为OPTIONAL MATCH确实会返回结果。这是因为,与 不同MATCHOPTIONAL MATCH它允许值null在子句之间传递。

 

MATCH (p:Person {name: 'Martin Sheen'})
OPTIONAL MATCH (p)-[r:DIRECTED]->()
RETURN p.name, r
Table 1. Result
p.namer

"Martin Sheen"

<null>

Rows: 1

OPTIONAL MATCH can therefore be used to check graphs for missing as well as existing values, and to pass on rows without any data to subsequent clauses in a query.

Optional relationships

If the existence of a relationship is optional, use the OPTIONAL MATCH clause. If the relationship exists, it is returned. If it does not, null is returned in its place.

 
MATCH (a:Movie {title: 'Wall Street'})
OPTIONAL MATCH (a)-->(x)
RETURN x

Returns null, since the Movie node Wall Street has no outgoing relationships.

Table 2. Result
x

<null>

Rows: 1

On the other hand, the following query does not return null since the Person node Charlie Sheen has one outgoing relationship.

 
MATCH (a:Person {name: 'Charlie Sheen'})
OPTIONAL MATCH (a)-->(x)
RETURN x   见上面的图即可知道答案是wall street
Table 3. Result
x

{"title":"Wall Street"}

Rows: 2

Properties on optional elements

If the existence of a property is optional, use the OPTIONAL MATCH clause. null will be returned if the specified property does not exist.

 
MATCH (a:Movie {title: 'Wall Street'})
OPTIONAL MATCH (a)-->(x)
RETURN x, x.name

Returns the element x (null in this query), and null for its name property, because the Movie node Wall Street has no outgoing relationships.

Table 4. Result
xx.name

<null>

<null>

Rows: 1

The following query only returns null for the nodes which lack a name property.

 
MATCH (a:Person {name: 'Martin Sheen'})
OPTIONAL MATCH (a)-->(x)
RETURN x, x.name 见上图即知为啥有三个答案
Table 5. Result
xx.name

{"title":"Wall Street"}

<null>

{"name":"Charlie Sheen"}

"Charlie Sheen"

{"title":"The American President"}

<null>

Rows: 3

Optional typed and named relationship

It is also possible to look for specific relationship types when using OPTIONAL MATCH:

 
MATCH (a:Movie {title: 'Wall Street'})
OPTIONAL MATCH (a)-[r:ACTED_IN]->()
RETURN a.title, r

This returns the title of the Movie node Wall Street, and since this node has no outgoing ACTED_IN relationships, null is returned for the relationship denoted by the variable r.

Table 6. Result
a.titler

"Wall Street"

<null>

Rows: 1

However, the following query does not return null since it is looking for incoming relationships of the type ACTED_IN to the Movie node Wall Street.

 
MATCH (a:Movie {title: 'Wall Street'})
OPTIONAL MATCH (x)-[r:ACTED_IN]->(a)
RETURN a.title, x.name, type(r)
Table 7. Result
a.titlex.nametype(r)

"Wall Street"

"Michael Douglas"

"ACTED_IN"

"Wall Street"

"Martin Sheen"

"ACTED_IN"

"Wall Street"

"Charlie Sheen"

"ACTED_IN"

Rows: 3

标签:Cypher,name,Wall,pipe,Street,match,Sheen,OPTIONAL,MATCH
From: https://www.cnblogs.com/bonelee/p/17616894.html

相关文章

  • Cypher中的group by功能实现
    Cypher语言并没有原生的 GROUPBY 关键字,但聚合函数(例如 COUNT)隐含地引入了分组。 https://neo4j.com/docs/cypher-manual/current/functions/aggregating/#grouping-key-examples聚合函数采用一组值并计算它们的聚合值。可以对所有匹配路径进行聚合计算,也可以通过引入分......
  • [SIGMOD 2020]In-Memory Subgraph Matching An In-depth Study
    In-MemorySubgraphMatching:AnIn-depthStudy一篇subgraphmatching的survey总结文章共分析了8中代表性的内存式子图匹配算法,在过滤、排序、扩展、优化四个方面做了对比。定义文中的重要符号表生成树的概念:生成树是一个连通图的最小连通子图问题定义子图同构问题是......
  • 【题解】 Pattern Matching in A Minor "Low Space" CCPC Mianyang 2022
    https://vjudge.net/contest/573644#problem/K字符串匹配,但卡空间。考虑哈希做法,不妨把\(s\)每\(20000\)个字符哈希成一个字符,于是\(s\)长度只有\(500\),可以跑个KMP。于是对于\(t\),我们只需要同时维护\(20000\)个KMP的指针。但如果字符串长度不是\(20000\)的倍......
  • Redis Pipeline管道技术
    1.什么是pipelinePipeline是Redis提供的一种批量请求机制,可以在client端对多条命令进行打包,然后一次性发送给服务器,避免了多次网络往返的开销。2.pipeline的优势为了解释pipeline的作用,我们先思考一个问题:如果客户端需要依次执行多条Redis命令,该如何处理?客户端一次执行一条......
  • 【ES】match和term的区别
    1、term查询是基于字段的精确匹配查询,不应用分词器。match查询是全文搜索查询,对搜索字符串和字段内容都应用相同的分词器,并使用布尔逻辑进行匹配。2、match:会使用分词器,对全文进行匹配搜索(模糊搜索),要注意的是,Keyword不会做分词的。所以如果是用在keyword上面,match和term......
  • BootstrapBlazor组件库,Menu组件NavLinkMatch导致的多个菜单项被被同时选中
    BootstrapBlazor组件库,Menu组件NavLinkMatch导致的多个菜单项被被同时选中BootstrapBlazor版本更新到7.9.0后,Menu组件出现首页同时被选中的情况默认首页/路径是选中状态,但是当选中其他路径后,首页路径还是选中状态。这是因为在BootstrapBlazor组件最新版中支持了一个新特性。可......
  • iptables: No chain/target/match by that name
    部署完docker后执行脚本报错在启动run.sh脚本后出现iptables:Nochain/target/matchbythatname.报错解决办法:重启dockerPS:重启docker后,通过以下命令可以查看最新防火墙配置:iptables-L应该看到iptables配置中ChainDocker设置已更新(与1中历史结果进行对比)原因解释重新......
  • 函数式编程之pipeline——很酷有没有
    Pipelinepipeline管道借鉴于UnixShell的管道操作——把若干个命令串起来,前面命令的输出成为后面命令的输入,如此完成一个流式计算。(注:管道绝对是一个伟大的发明,他的设哲学就是KISS–让每个功能就做一件事,并把这件事做到极致,软件或程序的拼装会变得更为简单和直观。这个设计理念......
  • 3DMatch
    详细描述链接:3DMatch数据集、github(介绍非常详细)官网主页:主页3DMatch数据集收集了来自于62个场景的数据,其中54个场景的数据用于训练,8个场景的数据用于评估,其具体名称查看train.txt和test.txt。3DMatch数据常用于3D点云的关键点,特征描述子,点云配准等任务。......
  • mongodb match 模糊匹配
    MongoDB的模糊匹配在使用MongoDB进行数据查询时,我们经常会遇到需要进行模糊匹配的情况。模糊匹配是指根据一定的规则,在数据库中查找与给定模式相匹配的数据。本文将介绍如何在MongoDB中进行模糊匹配,并提供相关的代码示例。1.模糊匹配基础在MongoDB中,模糊匹配通常使用正则表达式......