简体   繁体   English

在ArangoDB中以编程方式创建边缘

[英]Programmatically creating edges in ArangoDB

What is the simplest way to quickly create edges in ArangoDB programmatically? 以编程方式在ArangoDB中快速创建边的最简单方法是什么?

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. 我希望能够选择一个属性,对于集合A中的每个文档,为集合B中的每个文档创建一条在等效属性中具有相同值的边。

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":
}

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. 假设电子邮件中fromto字段中的值对应于我们可能在people集合中找到的电子邮件地址。

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. 我希望能够将集合,属性和边线参数作为输入,然后,对于人员集合中的每个文档,在电子邮件集合中的每个文档中,在from属性中具有相同电子邮件地址的每个文档创建一条边线作为当前文档的email属性。

So far, I think that Foxx may be the best tool for this, but I am a bit overwhelmed by the documentation. 到目前为止,我认为Foxx可能是最好的工具,但是我对文档有些不知所措。

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. 最终,我想基于定义边的文档之间的共享属性来创建完整的CRUD,包括一个“ upsert”等效项-更新边(如果已存在)并创建边(如果不存在)。

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. 我知道用标准HTTP API进行单独的API调用会太慢,因为我需要查询Arango中集合中的每个文档并返回大量结果。

Is there already a Foxx service that does this? 已经有执行此操作的Foxx服务吗? If not, where should I start to create one? 如果没有,我应该从哪里开始创建?

A single AQL query should suffice: 单个AQL查询就足够了:

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

The email addresses in the vertex collection people are matched with the from email addresses of the emails vertex collection. email的顶点集合地址people将与匹配from该电子邮件地址emails顶点集合。 For every match, a new edge is inserted into an edge collection sent , linking people and email records. 对于每次匹配,都会在sent的边缘集合中插入一个新的边缘,从而将人员和电子邮件记录链接在一起。

If both vertex collections contain a small number of documents, it is okay to execute this query without indexes (eg 1,000 persons and 3,000 emails took about 2 seconds in my test). 如果两个顶点集合都包含少量文档,则可以在没有索引的情况下执行此查询(例如,在我的测试中,1,000个人和3,000电子邮件花费了大约2秒钟)。 For larger datasets, make sure to create a hash index in people on the email attribute, and in emails a hash index on from . 对于较大的数据集,请确保在email属性上的people创建哈希索引,并在emails from上创建哈希索引。 It reduced the execution time to about 30ms in my test. 在我的测试中,它将执行时间减少到大约30ms。

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

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