Gremlin:返回在查询中创建的新边而不保存它们(相当于 APOC vRelationship)

发布于 2025-01-13 02:02:14 字数 1501 浏览 0 评论 0原文

我有一个包含两种类型节点的图表:人和房屋。在此图中,人们可以出售房屋(SELLS 关系),房屋可以由人们购买(IS_BOUGHT_BY 关系)。这可以表示为下图:

(Person_1)-[:SELLS]->(House_1)-[:IS_BOUGHT_BY]->(Person_2)

该图的基本示例可以通过运行在 Neo4j 中创建:

CREATE(Person_1: Person {name:'Person 1'})
CREATE(House_1: House {name:'House 1', deal: 10238})
CREATE(Person_2: Person {name:'Person 2'})

CREATE
(Person_1)-[:SELLS]->(House_1),
(House_1)-[:IS_BOUGHT_BY]->(Person_2)

或者可以通过执行在 Gremlin 中创建:

g.
 addV('Person').property('name', 'Person 1').as('p1').
 addV('House').property('name', 'House 1').property('deal', 10238).as('h1').
 addV('Person').property('name', 'Person 1').as('p2').
 addE('SELLS').from('p1').to('h1').
 addE('IS_BOUGHT_BY').from('h1').to('p2')

我想进行一个查询,返回与名为 < 的伪边连接的这些 Person 节点code>SELLS_HOUSE_TO 而不将此关系保存在数据库中。此外,此 SELLS_HOUSE_TO 关系必须将第 1 人出售并由第 2 人购买的房屋的交易 ID 作为交易属性。换句话说,我希望查询的输出遵循下图

(Person_1)-[:SELLS_HOUSE_TO {deal: house_1_deal}]->(Person_2)

id_dealHouse_1 节点的 deal 属性,它通过分别是 SELLSIS_BOUGHT_BY 关系。

通过使用 APOC 库提供的 vRelationship 函数,可以在 Neo4j 中轻松完成此查询:

MATCH (p1: Person)-[r1:SELLS]->(h1: House)-[r2:BUYS]->(p2: Person)
CALL apoc.create.vRelationship(p1, 'SELLS_HOUSE_TO', {deal: h1.deal}, p2) YIELD rel
RETURN p1, rel, p2

是否可以在 Gremlin 中执行类似的操作,而无需在某个步骤将边存储在数据库中?

I have a graph with two types of nodes: Persons and Houses. In this graph, persons can sell houses (SELLS relationship), and houses are bought by persons (IS_BOUGHT_BY relationship). This could be represented as the following diagram:

(Person_1)-[:SELLS]->(House_1)-[:IS_BOUGHT_BY]->(Person_2)

A basic example of this diagram can be created in Neo4j by running:

CREATE(Person_1: Person {name:'Person 1'})
CREATE(House_1: House {name:'House 1', deal: 10238})
CREATE(Person_2: Person {name:'Person 2'})

CREATE
(Person_1)-[:SELLS]->(House_1),
(House_1)-[:IS_BOUGHT_BY]->(Person_2)

or can be created in Gremlin by executing:

g.
 addV('Person').property('name', 'Person 1').as('p1').
 addV('House').property('name', 'House 1').property('deal', 10238).as('h1').
 addV('Person').property('name', 'Person 1').as('p2').
 addE('SELLS').from('p1').to('h1').
 addE('IS_BOUGHT_BY').from('h1').to('p2')

I want to make a query that returns these Person nodes connected with a fake edge called SELLS_HOUSE_TO without saving this relationship in the database. Also this SELLS_HOUSE_TO relationship must have as deal property the deal id of the house sold by Person 1 and bought by Person 2. In other words, I want the output of the query to follow the following diagram:

(Person_1)-[:SELLS_HOUSE_TO {deal: house_1_deal}]->(Person_2)

where id_deal is the deal property of the House_1 node, which connects Person_1 and Person_2 through the SELLS and IS_BOUGHT_BY relationships respectively.

This query can be easily done in Neo4j by using the vRelationship function provided by the APOC library:

MATCH (p1: Person)-[r1:SELLS]->(h1: House)-[r2:BUYS]->(p2: Person)
CALL apoc.create.vRelationship(p1, 'SELLS_HOUSE_TO', {deal: h1.deal}, p2) YIELD rel
RETURN p1, rel, p2

It is possible to do something similar in Gremlin without involving storing the edges in the database at some step?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

删除会话 2025-01-20 02:02:15

目前,Gremlin 除了可能使用内联代码(闭包/lambda)之外,无法将虚拟边注入到查询结果中。这可能是一个很好的功能请求,可以作为 Apache Tinkerpop 的 Jira 票证打开 此位置

作为一个短期解决方案,我认为您能做的最好的事情就是根据可视化的需要创建边缘,也许给它们一个独特的属性键和值,例如 "virtual":true 并当不再需要时删除这些边。

Currently, Gremlin, outside of perhaps using in-line code (closure/lambda), has no way to inject virtual edges into a query result. This might be a good feature request to open as a Jira ticket for Apache Tinkerpop at this location.

As a short term solution, I think the best you can do is to create the edges for the needs of a visualization and perhaps give them a unique property key and value something like "virtual":true and to delete such edges when no longer needed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文