asdf777
asdf777

Reputation: 25

Gremlin: Return new edges created in a query without saving them (equivalent to APOC vRelationship)

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?

Upvotes: 1

Views: 132

Answers (1)

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14391

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.

Upvotes: 2

Related Questions