编写Cypher查询以创建随机节点之间的链接 [英] Write a Cypher query to CREATE links between random nodes

查看:121
本文介绍了编写Cypher查询以创建随机节点之间的链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要播种数据库,我想创建一小组Person节点...

To seed a database, I would like to create a small set of Person nodes...

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?

我想我需要将每个新的Person添加到集合中,并使用从FLOOR(RAND()* size(names))创建的变量进行工作,但是官方文档并未提供有关如何处理的许多线索.做到这一点.

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.

推荐答案

好问题!

几件事,首先,我经常更喜欢UNWIND而不是FOREACH,尤其是在这样的情况下:

A couple of things, first I often prefer UNWIND over FOREACH, particularly in a case like this:

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

就建立随机关系而言,迈克尔·汉格(Michael Hunger)撰写了一篇不错的博客文章:

As far as creating random relationships, Michael Hunger has a good blog post covering it:

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

请小心一点,因为第一个MATCH指定了所有人与其他所有人的完整笛卡尔积,随着您的Person节点的增长,笛卡尔积可以快速增长.迈克尔在帖子的WITH上放置了LIMIT

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. Michael puts a LIMIT on his WITH in the post

这篇关于编写Cypher查询以创建随机节点之间的链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆