在ArangoDB中以编程方式创建边缘 [英] Programmatically creating edges in 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":
}
比方说,电子邮件中from
和to
字段中的值对应于我们可能在人员集合中找到的电子邮件地址.
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.
我希望能够将集合,属性和边线参数作为输入,然后,对于人员集合中的每个文档,为电子邮件集中具有相同电子邮件地址的每个文档创建边线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,包括等效"-更新边(如果已存在)并创建边(如果不存在).
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秒钟的时间).对于较大的数据集,请确保在email
属性的people
中创建哈希索引,在from
的emails
中创建哈希索引.在我的测试中,它将执行时间减少到大约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屋!