在 ArangoDB 中以编程方式创建边 [英] Programmatically creating edges in ArangoDB

查看:28
本文介绍了在 ArangoDB 中以编程方式创建边的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ArangoDB 中以编程方式快速创建边的最简单方法是什么?

What is the simplest way to quickly create edges in ArangoDB programmatically?

我想根据公共属性在文档之间创建关系.我希望能够选择一个属性,并且对于集合 A 中的每个文档,为集合 B 中的每个文档创建一个边缘,这些文档在等效属性中具有相同的值.

I would like to create relationships between documents based on a common attribute. I'd like to be able to select an attribute, and for every document in collection A, create an edge to every document in collection B that has the same value in an equivalent attribute.

例如,如果我已将电子邮件消息导入一个集合并将人员导入另一个集合,我希望在电子邮件和集合之间生成边.电子邮件的架构可能如下所示:

For example, if I've imported email messages into a collection and people into another collection, I would like to generate edges between the emails and collections. An email's schema might look like this:

{
  "_key":
  "subject":
  "body":
  "from":
  "to":
}

一个人的架构可能如下所示:

And a person's schema might look like this:

{
  "_key":
  "name":
  "email":
}

假设电子邮件消息中 fromto 字段中的值对应于我们可能在 people 集合中找到的电子邮件地址.

Let's say that the values in the from and to fields in the email messages correspond to email addresses that we may find in the people collection.

我希望能够将集合、属性和边参数作为输入,然后,对于 people 集合中的每个文档,为电子邮件集合中具有相同电子邮件地址的每个文档创建一个边from 属性作为当前文档的 email 属性.

I'd like to be able to take the collections, attributes, and edge parameters as input, then, for every document in the people collection, create an edge to every document in the email collection that has the same email address in the from attribute as the current document's email attribute.

到目前为止,我认为 Foxx 可能是最好的工具,但我对文档有点不知所措.

So far, I think that Foxx may be the best tool for this, but I am a bit overwhelmed by the documentation.

最终,我想根据定义边的文档之间的共享属性创建一个完整的 CRUD,包括一个upsert"等效项 - 如果边已经存在则更新它,如果它不存在则创建它.

Eventually, I'd like to create a full CRUD based on shared attributes between documents defining edges, including an "upsert" equivalent- updating an edge if it already exists and creating it if it doesn't.

我知道使用标准 HTTP API 对单个 API 调用执行此操作会太慢,因为我需要为集合中的每个文档查询 Arango 并返回大量结果.

I know that doing this with individual API calls with the standard HTTP API would be far too slow, since I would need to query Arango for every document in a collection and return very large numbers of results.

是否已经有一个 Foxx 服务可以做到这一点?如果没有,我应该从哪里开始创建?

Is there already a Foxx service that does this? If not, where should I start to create one?

推荐答案

一个 AQL 查询就足够了:

A single AQL query should suffice:

FOR p IN people
    FOR e IN emails
        FILTER p.email == e.from
        INSERT {_from: p._id, _to: e._id} INTO sent

顶点集合people中的email地址与emails顶点的from电子邮件地址匹配收藏.对于每场比赛,一个新的边被插入到边集合 sent 中,链接人和电子邮件记录.

The email addresses in the vertex collection people are matched with the from email addresses of the emails vertex collection. For every match, a new edge is inserted into an edge collection sent, linking people and email records.

如果两个顶点集合都包含少量文档,则可以在没有索引的情况下执行此查询(例如,在我的测试中,1,000 人和 3,000 封电子邮件花费了大约 2 秒).对于较大的数据集,请确保在 people 中的 email 属性上创建哈希索引,并在 emails 中创建 from 上的哈希索引.它在我的测试中将执行时间减少到大约 30 毫秒.

If both vertex collections contain a small number of documents, it is okay to execute this query without indexes (e.g. 1,000 persons and 3,000 emails took about 2 seconds in my test). For larger datasets, make sure to create a hash index in people on the email attribute, and in emails a hash index on from. It reduced the execution time to about 30ms in my test.

这篇关于在 ArangoDB 中以编程方式创建边的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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