简体   繁体   English

在 cypher/neo4j 中批量创建节点关系

[英]Batch node relationship creation in cypher/neo4j

What is the most efficient way to break down this CREATE cypher query?分解此 CREATE 密码查询的最有效方法是什么?

The end pattern is the following:结束模式如下:

(newTerm:term)-[:HAS_META]->(metaNode:termMeta)

In this pattern this is a single newTerm node and about ~25 termMeta nodes.在这种模式中,这是一个 newTerm 节点和大约 25 个 termMeta 节点。 The HAS_META relationship will have a single property (languageCode) that will be different for each termMeta node. HAS_META 关系将有一个属性(languageCode),每个 termMeta 节点都不同。

In the application, all of these nodes and relationships will be created at the same time.在应用程序中,将同时创建所有这些节点和关系。 I'm trying to determine the best way to add them.我正在尝试确定添加它们的最佳方式。

Is there anyway to add these without having to have perform individual query for each TermMeta node?无论如何要添加这些而不必为每个 TermMeta 节点执行单独的查询?

I know you can add multiple instances of a node using the following query format:我知道您可以使用以下查询格式添加节点的多个实例:

 "metaProps" : [ 
            {"languageCode" : "en", "name" : "1", "dateAdded": "someDate1"}, 
            {"languageCode" : "de", "name" : "2", "dateAdded": "someDate2"},
            {"languageCode" : "es", "name" : "3", "dateAdded": "someDate3"}, 
            {"languageCode" : "fr", "name" : "3", "dateAdded": "someDate4"}
        ]

But you can only do that for one type of node at a time and there (as far as I can tell) is no way to dynamically add the relationship properties that are needed.但是您一次只能对一种类型的节点执行此操作,并且(据我所知)无法动态添加所需的关系属性。

Any insight would be appreciated.任何见解将不胜感激。

There's no really elegant way to do it, as far as I can tell—from your example, I'm assuming you're using parameters.据我所知,没有真正优雅的方法可以做到这一点 - 从您的示例中,我假设您正在使用参数。 You can use a foreach to loop through the params and do a create on each one, but it's pretty ugly, and requires you to explicitly specify literal maps of your properties.您可以使用 foreach 循环遍历参数并对每个参数进行创建,但它非常难看,并且需要您明确指定属性的文字映射。 Here's what it would look like for your example:以下是您的示例的外观:

CREATE (newTerm:term)
FOREACH ( props IN {metaProps} | 
  CREATE newTerm-[:HAS_META {languageCode: props.languageCode}]->
           (:termMeta {name: props.name, dateAdded: props.dateAdded})
)
WITH newTerm
MATCH newTerm-[rel:HAS_META]->(metaNode:termMeta)
RETURN newTerm, rel, metaNode

If you don't need to return the results, you can delete everything after the FOREACH .如果不需要返回结果,可以删除FOREACH之后的FOREACH

Select and name each vertex differently and then create relations using it.以不同的方式选择和命名每个顶点,然后使用它创建关系。 For ex前任

match (n:Tag), (m:Account), (l:FOO) CREATE (n)-[r:mn]->(m),(m)-[x:ml]->(l)
match (n:Tag{a:"a"}), (m:Account{b:"x"}), (l:FOO) CREATE (n)-[r:mn]->(m),(m)-[x:ml]->(l)

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

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