简体   繁体   English

写一个Cypher查询来创建随机节点之间的链接

[英]Write a Cypher query to CREATE links between random nodes

To seed a database, I would like to create a small set of Person nodes... 为了种子数据库,我想创建一小组Person节点......

WITH ["Amy","Bob","Cal","Dan","Eve"]
AS names
FOREACH (r IN range(0, size(names)-1) |
  CREATE (:Person {name: names[r]})
)

... and I would like to create a random connection for each Person. ...我想为每个人创建一个随机连接。 Is it possible to do this in a single query? 是否可以在单个查询中执行此操作?

I imagine that I would need to add each new Person to a collection, and work with a variable created from FLOOR(RAND() * size(names)), but the official documentation does not give many clues as to how to do this. 我想我需要将每个新Person添加到一个集合中,并使用从FLOOR(RAND()* size(names))创建的变量,但官方文档并没有提供很多关于如何执行此操作的线索。

Good question! 好问题!

A couple of things, first I often prefer UNWIND over FOREACH , particularly in a case like this: 有几件事,首先我经常更喜欢UNWIND不是FOREACH ,特别是在这样的情况下:

WITH ["Amy","Bob","Cal","Dan","Eve"] AS names
UNWIND names AS name
CREATE (:Person {name: name})

As far as creating random relationships, Michael Hunger has a good blog post covering it: 就创建随机关系而言,Michael Hunger有一篇很好的博客文章报道:

http://jexp.de/blog/2014/03/quickly-create-a-100k-neo4j-graph-data-model-with-cypher-only/ http://jexp.de/blog/2014/03/quickly-create-a-100k-neo4j-graph-data-model-with-cypher-only/

In your case it would be something like: 在你的情况下,它将是这样的:

MATCH (p1:Person), (p2:Person)
WITH p1, p2
WHERE rand() < 0.1
MERGE p1-[:LIKES]->p2

Just be careful with that as the first MATCH specifies a full cartesian product of all people with all other people which can grow quickly as your Person nodes grow. 请注意这一点,因为第一个MATCH指定了所有其他人的完整笛卡尔积,可以随着Person节点的增长而快速增长。 Michael puts a LIMIT on his WITH in the post 迈克尔在帖子中对他的WITH进行了LIMIT

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

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