简体   繁体   English

py2neo cypher在for循环中创建到中央节点的多个关系

[英]py2neo cypher create several relations to central node in for loop

just starting out with neo4j, py2neo and Cypher. 刚开始是neo4j,py2neo和Cypher。

I have encountered the following problem and google and my knowledge of what to ask have not yet given me an answer or a helpful hint in the right direction. 我遇到了以下问题,谷歌和我对要问的问题的了解还没有给我正确方向的答案或有用的提示。 Anyway: 无论如何:

Problem: I don't know how to, in python/py2neo, create relations between a unique starting node and a number of following nodes that I create dynamically in a for loop. 问题:我不知道如何在python / py2neo中创建唯一的起始节点与我在for循环中动态创建的多个后续节点之间的关系。

Background: I have a json object which defines a person object, who will have an id, and several properties, such as favourite colour, favourite food etc. 背景:我有一个json对象,它定义一个person对象,该对象将具有一个id,以及几个属性,例如喜欢的颜色,喜欢的食物等。

So at the start of my py2neo script I define my person. 因此,在我的py2neo脚本开始时,我定义了我的人。 After this I loop through my json for every property this person has. 之后,我遍历此人拥有的每个属性的json。

This works fine, and with no relations I end up with a neo4j chart with several nodes with the right parameters. 这可以正常工作,并且没有任何关系,我最终得到带有带有正确参数的多个节点的neo4j图表。

If I'm understanding the docs right I have to make a match to find my newly created person, for each new property I want to link. 如果我了解正确的文档,则必须对要链接的每个新属性进行匹配以找到新创建的人。 This seems absurd to me as I just created this person and still have the reference to the person object in memory. 对于我来说,这似乎很荒谬,因为我刚刚创建了这个人,但仍然在内存中引用了该人对象。 But for me it is unclear on how to actually write the code for creating the relation. 但是对我来说,如何实际编写用于创建关系的代码尚不清楚。 Also, as a relative newbie in both python and Cypher, best practices are still an unknown to me. 另外,作为python和Cypher的相对新手,最佳实践对我来说还是一个未知数。

What I understand is I can use py2neo 我了解的是我可以使用py2neo

graph = Graph(http://...)
tx = graph.begin()
p = Node("Person", id)
tx.create(p)

and then I can reference p later on. 然后我可以稍后参考p。 But for my properties, of which there can be many, I create a string in python like so (pseudocode here, I have a nice oneliner for this that fits my actual case with lambda, join, map, format and so on) 但是对于我的属性(可能有很多),我像这样在python中创建了一个字符串(此处为伪代码,为此我有一个不错的oneliner来适合我的实际情况,包括lambda,join,map,format等)

for param in params:
  par = "MERGE (par:" + param + ... )
  tx.append(par)
tx.process()
tx.commit()

How do I create a relation "likes" back to the person for each and every par in the for loop? 我如何为for循环中的每个par创建一个“喜欢”回到个人的关系?

Or do I need to rethink my whole solution? 还是我需要重新考虑整个解决方案?

Help?! 救命?! :-) :-)

//Jonas //乔纳斯

Considering you've created a node Alice and you want to create the other as dynamic, I'll suggest while dynamically parsing through the nodes, store it everytime (in the loop) in a variable, make a node out of it and then implement in Relationship Syntax. 考虑到您已经创建了一个节点Alice并且想要将另一个节点创建为动态节点,我建议在动态分析节点时,每次(在循环中)将其存储在变量中,从中取出一个节点,然后实施在关系语法中。 The syntax is 语法是

Relationship(Node_1, Relation, Node_2) 关系(Node_1,Relation,Node_2)

Now key thing to know here is type(Node_1) and type(Node_2) both will be Node. 现在,这里要知道的关键是type(Node_1)和type(Node_2)都将是Node。

I've stored all the nodes (only their names) from json in a list named nodes. 我已经将所有来自json的节点(仅它们的名称)存储在名为nodes的列表中。 Since you mentioned you only have reference to Alice a = ("Person", name:"Alice") for node in nodes: (excluding Alice) = Node(, name:"") = Relationship(a, , 自从您提到以来,您仅对节点中的节点引用了Alice a =(“ Person”,名称:“ Alice”):(不包括Alice)= Node(,名称:“”)= Relationship(a,,

Make sure to iterate variable name, else it'll keep overwriting. 确保迭代变量名,否则它将被覆盖。

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

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