Neo4J-优化3个合并查询到单个查询

时间:2018-11-30 11:26:14

标签: neo4j cypher

我正在尝试进行一个Cypher查询,该查询使2个节点并在它们之间添加关系。

要添加节点,我要检查该节点是否存在,如果存在,那么我只是继续进行并设置属性。

// Query 1 for creating or updating node 1

MERGE (Kunal:PERSON)
ON CREATE SET
    Kunal.name = 'Kunal',
    Kunal.type = 'Person',
    Kunal.created = timestamp()
ON MATCH SET
    Kunal.lastUpdated = timestamp()
RETURN Kunal

// Query 2 for creating or updating node 2

MERGE (Bangalore: LOC)
ON CREATE SET
    Bangalore.name = 'Bangalore',
    Bangalore.type = 'Location',
    Bangalore.created = timestamp()
ON MATCH SET
    Bangalore.lastUpdated = timestamp()
RETURN Bangalore

同样,我正在检查上面创建的节点之间是否存在关系,如果不存在,则创建它,否则将更新其属性。

// Query 3 for creating relation or updating it.

MERGE (Kunal: PERSON { name: 'Kunal', type: 'Person' })
MERGE (Bangalore: LOC { name: 'Bangalore', type: 'Location' })
MERGE (Kunal)-[r:LIVES_IN]->(Bangalore)
ON CREATE SET
    r.duration = 36
ON MATCH SET
    r.duration = r.duration + 1 
RETURN *

问题是这些是3个单独的查询,当我通过Python驱动程序运行它时将有3个数据库调用。有没有一种方法可以将这些查询优化为单个查询。

1 个答案:

答案 0 :(得分:2)

当然,您可以将三个查询连接为一个。 在这种情况下,您可以省略上一个查询的第一个MERGE和第二个MERGE (kunal:PERSON {name: ‘Kunal'}) ON CREATE SET kunal.type = 'Person', kunal.created = timestamp() ON MATCH SET kunal.lastUpdated = timestamp() MERGE (bangalore:LOC {name: 'Bangalore'}) ON CREATE SET bangalore.type = 'Location', bangalore.created = timestamp() ON MATCH SET bangalore.lastUpdated = timestamp() MERGE (kunal)-[r:LIVES_IN]->(bangalore) ON CREATE SET r.duration = 36 ON MATCH SET r.duration = r.duration + 1 RETURN * ,因为已经开始新查询了。

public async Task<IActionResult> Logout()
{
   await _signInManager.SignOutAsync();
   return View("Logout"); // or whatever url Redirect("http://localhost:5011/Home/")
}