How to write Gremlin query to find parent vertices which have a specified edge?
I am a newbie to gremlin queries. I have a graph like below, and my source vertex is P3, I want to write a query that will get all the parentancestor vertices (an vertex is parentancetor of P3 if there is an path from that vertex to P3 with edges of type 'contains') of type 'Part' and have an Owner associated to them. So in this case the query should return P1 and P2 but not P.
Query to create sample data:
g.addV(id, 'P1').property('label','part').as('p1')
.addV(id, 'P2').property('label','part').as('p2')
.addV(id, 'P3').property('label','part').as('p3')
.addV(id, 'P4').property('label','part').as('p4')
.addV(id, 'owner1').property('label','owner').as('o1')
.addV(id, 'owner2').property('label','owner').as('o2')
.addE('contains').from('p1').to('p2')
.addE('contains').from('p2').to('p3')
.addE('contains').from('p4').to('p3')
.addE('owns').from('o1').to('p1')
.addE('owns').from('o2').to('p2')
This is the query that I came up with, but the traversal stops once it finds a part vertex that has an owner vertex associated to it. How to update it to return both P1 and P2.
g.V('P3')
.union(
inE().hasLabel('owns').inV(),
repeat(inE().hasLabel('contains')
.outV().hasLabel('part'))
.until(inE().hasLabel('owns'))
).dedup()
I also tried using a sideEffect step to collect part vertices but didn't get required result.
g.V('P3').union(
inE().hasLabel('owns').inV(),
repeat(inE().sideEffect(hasLabel('owns').outV().as('parts'))
.hasLabel('contains')
.outV().hasLabel('part'))
)
.select('parts').dedup()
graph-databases gremlin
add a comment |
I am a newbie to gremlin queries. I have a graph like below, and my source vertex is P3, I want to write a query that will get all the parentancestor vertices (an vertex is parentancetor of P3 if there is an path from that vertex to P3 with edges of type 'contains') of type 'Part' and have an Owner associated to them. So in this case the query should return P1 and P2 but not P.
Query to create sample data:
g.addV(id, 'P1').property('label','part').as('p1')
.addV(id, 'P2').property('label','part').as('p2')
.addV(id, 'P3').property('label','part').as('p3')
.addV(id, 'P4').property('label','part').as('p4')
.addV(id, 'owner1').property('label','owner').as('o1')
.addV(id, 'owner2').property('label','owner').as('o2')
.addE('contains').from('p1').to('p2')
.addE('contains').from('p2').to('p3')
.addE('contains').from('p4').to('p3')
.addE('owns').from('o1').to('p1')
.addE('owns').from('o2').to('p2')
This is the query that I came up with, but the traversal stops once it finds a part vertex that has an owner vertex associated to it. How to update it to return both P1 and P2.
g.V('P3')
.union(
inE().hasLabel('owns').inV(),
repeat(inE().hasLabel('contains')
.outV().hasLabel('part'))
.until(inE().hasLabel('owns'))
).dedup()
I also tried using a sideEffect step to collect part vertices but didn't get required result.
g.V('P3').union(
inE().hasLabel('owns').inV(),
repeat(inE().sideEffect(hasLabel('owns').outV().as('parts'))
.hasLabel('contains')
.outV().hasLabel('part'))
)
.select('parts').dedup()
graph-databases gremlin
The picture is nice, but could you please provide a Gremlin script that creates your sample data - here is an example stackoverflow.com/questions/51388315/…
– stephen mallette
Jan 2 at 19:43
@stephenmallette Now I added script for source data, also removed cosmosdb specific query format.
– Sri Harsha Velicheti
Jan 2 at 20:15
add a comment |
I am a newbie to gremlin queries. I have a graph like below, and my source vertex is P3, I want to write a query that will get all the parentancestor vertices (an vertex is parentancetor of P3 if there is an path from that vertex to P3 with edges of type 'contains') of type 'Part' and have an Owner associated to them. So in this case the query should return P1 and P2 but not P.
Query to create sample data:
g.addV(id, 'P1').property('label','part').as('p1')
.addV(id, 'P2').property('label','part').as('p2')
.addV(id, 'P3').property('label','part').as('p3')
.addV(id, 'P4').property('label','part').as('p4')
.addV(id, 'owner1').property('label','owner').as('o1')
.addV(id, 'owner2').property('label','owner').as('o2')
.addE('contains').from('p1').to('p2')
.addE('contains').from('p2').to('p3')
.addE('contains').from('p4').to('p3')
.addE('owns').from('o1').to('p1')
.addE('owns').from('o2').to('p2')
This is the query that I came up with, but the traversal stops once it finds a part vertex that has an owner vertex associated to it. How to update it to return both P1 and P2.
g.V('P3')
.union(
inE().hasLabel('owns').inV(),
repeat(inE().hasLabel('contains')
.outV().hasLabel('part'))
.until(inE().hasLabel('owns'))
).dedup()
I also tried using a sideEffect step to collect part vertices but didn't get required result.
g.V('P3').union(
inE().hasLabel('owns').inV(),
repeat(inE().sideEffect(hasLabel('owns').outV().as('parts'))
.hasLabel('contains')
.outV().hasLabel('part'))
)
.select('parts').dedup()
graph-databases gremlin
I am a newbie to gremlin queries. I have a graph like below, and my source vertex is P3, I want to write a query that will get all the parentancestor vertices (an vertex is parentancetor of P3 if there is an path from that vertex to P3 with edges of type 'contains') of type 'Part' and have an Owner associated to them. So in this case the query should return P1 and P2 but not P.
Query to create sample data:
g.addV(id, 'P1').property('label','part').as('p1')
.addV(id, 'P2').property('label','part').as('p2')
.addV(id, 'P3').property('label','part').as('p3')
.addV(id, 'P4').property('label','part').as('p4')
.addV(id, 'owner1').property('label','owner').as('o1')
.addV(id, 'owner2').property('label','owner').as('o2')
.addE('contains').from('p1').to('p2')
.addE('contains').from('p2').to('p3')
.addE('contains').from('p4').to('p3')
.addE('owns').from('o1').to('p1')
.addE('owns').from('o2').to('p2')
This is the query that I came up with, but the traversal stops once it finds a part vertex that has an owner vertex associated to it. How to update it to return both P1 and P2.
g.V('P3')
.union(
inE().hasLabel('owns').inV(),
repeat(inE().hasLabel('contains')
.outV().hasLabel('part'))
.until(inE().hasLabel('owns'))
).dedup()
I also tried using a sideEffect step to collect part vertices but didn't get required result.
g.V('P3').union(
inE().hasLabel('owns').inV(),
repeat(inE().sideEffect(hasLabel('owns').outV().as('parts'))
.hasLabel('contains')
.outV().hasLabel('part'))
)
.select('parts').dedup()
graph-databases gremlin
graph-databases gremlin
edited Jan 2 at 20:14
Sri Harsha Velicheti
asked Jan 2 at 19:34
Sri Harsha VelichetiSri Harsha Velicheti
322315
322315
The picture is nice, but could you please provide a Gremlin script that creates your sample data - here is an example stackoverflow.com/questions/51388315/…
– stephen mallette
Jan 2 at 19:43
@stephenmallette Now I added script for source data, also removed cosmosdb specific query format.
– Sri Harsha Velicheti
Jan 2 at 20:15
add a comment |
The picture is nice, but could you please provide a Gremlin script that creates your sample data - here is an example stackoverflow.com/questions/51388315/…
– stephen mallette
Jan 2 at 19:43
@stephenmallette Now I added script for source data, also removed cosmosdb specific query format.
– Sri Harsha Velicheti
Jan 2 at 20:15
The picture is nice, but could you please provide a Gremlin script that creates your sample data - here is an example stackoverflow.com/questions/51388315/…
– stephen mallette
Jan 2 at 19:43
The picture is nice, but could you please provide a Gremlin script that creates your sample data - here is an example stackoverflow.com/questions/51388315/…
– stephen mallette
Jan 2 at 19:43
@stephenmallette Now I added script for source data, also removed cosmosdb specific query format.
– Sri Harsha Velicheti
Jan 2 at 20:15
@stephenmallette Now I added script for source data, also removed cosmosdb specific query format.
– Sri Harsha Velicheti
Jan 2 at 20:15
add a comment |
1 Answer
1
active
oldest
votes
I revised your sample data code a bit as the syntax was wrong:
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('part').property(id, 'P1').as('p1').
......1> addV('part').property(id, 'P2').as('p2').
......2> addV('part').property(id, 'P3').as('p3').
......3> addV('part').property(id, 'P4').as('p4').
......4> addV('owner').property(id, 'owner1').as('o1').
......5> addV('owner').property(id, 'owner2').as('o2').
......6> addE('contains').from('p1').to('p2').
......7> addE('contains').from('p2').to('p3').
......8> addE('contains').from('p4').to('p3').
......9> addE('owns').from('o1').to('p1').
.....10> addE('owns').from('o2').to('p2').iterate()
I think you can simplify your traversal to just be a simple repeat()
:
gremlin> g.V('P3').emit(inE('owns')).repeat(__.in('contains'))
==>v[P2]
==>v[P1]
Note the placement of the emit()
step which controls the vertices that are output from the loop.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012166%2fhow-to-write-gremlin-query-to-find-parent-vertices-which-have-a-specified-edge%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I revised your sample data code a bit as the syntax was wrong:
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('part').property(id, 'P1').as('p1').
......1> addV('part').property(id, 'P2').as('p2').
......2> addV('part').property(id, 'P3').as('p3').
......3> addV('part').property(id, 'P4').as('p4').
......4> addV('owner').property(id, 'owner1').as('o1').
......5> addV('owner').property(id, 'owner2').as('o2').
......6> addE('contains').from('p1').to('p2').
......7> addE('contains').from('p2').to('p3').
......8> addE('contains').from('p4').to('p3').
......9> addE('owns').from('o1').to('p1').
.....10> addE('owns').from('o2').to('p2').iterate()
I think you can simplify your traversal to just be a simple repeat()
:
gremlin> g.V('P3').emit(inE('owns')).repeat(__.in('contains'))
==>v[P2]
==>v[P1]
Note the placement of the emit()
step which controls the vertices that are output from the loop.
add a comment |
I revised your sample data code a bit as the syntax was wrong:
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('part').property(id, 'P1').as('p1').
......1> addV('part').property(id, 'P2').as('p2').
......2> addV('part').property(id, 'P3').as('p3').
......3> addV('part').property(id, 'P4').as('p4').
......4> addV('owner').property(id, 'owner1').as('o1').
......5> addV('owner').property(id, 'owner2').as('o2').
......6> addE('contains').from('p1').to('p2').
......7> addE('contains').from('p2').to('p3').
......8> addE('contains').from('p4').to('p3').
......9> addE('owns').from('o1').to('p1').
.....10> addE('owns').from('o2').to('p2').iterate()
I think you can simplify your traversal to just be a simple repeat()
:
gremlin> g.V('P3').emit(inE('owns')).repeat(__.in('contains'))
==>v[P2]
==>v[P1]
Note the placement of the emit()
step which controls the vertices that are output from the loop.
add a comment |
I revised your sample data code a bit as the syntax was wrong:
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('part').property(id, 'P1').as('p1').
......1> addV('part').property(id, 'P2').as('p2').
......2> addV('part').property(id, 'P3').as('p3').
......3> addV('part').property(id, 'P4').as('p4').
......4> addV('owner').property(id, 'owner1').as('o1').
......5> addV('owner').property(id, 'owner2').as('o2').
......6> addE('contains').from('p1').to('p2').
......7> addE('contains').from('p2').to('p3').
......8> addE('contains').from('p4').to('p3').
......9> addE('owns').from('o1').to('p1').
.....10> addE('owns').from('o2').to('p2').iterate()
I think you can simplify your traversal to just be a simple repeat()
:
gremlin> g.V('P3').emit(inE('owns')).repeat(__.in('contains'))
==>v[P2]
==>v[P1]
Note the placement of the emit()
step which controls the vertices that are output from the loop.
I revised your sample data code a bit as the syntax was wrong:
gremlin> g = TinkerGraph.open().traversal()
==>graphtraversalsource[tinkergraph[vertices:0 edges:0], standard]
gremlin> g.addV('part').property(id, 'P1').as('p1').
......1> addV('part').property(id, 'P2').as('p2').
......2> addV('part').property(id, 'P3').as('p3').
......3> addV('part').property(id, 'P4').as('p4').
......4> addV('owner').property(id, 'owner1').as('o1').
......5> addV('owner').property(id, 'owner2').as('o2').
......6> addE('contains').from('p1').to('p2').
......7> addE('contains').from('p2').to('p3').
......8> addE('contains').from('p4').to('p3').
......9> addE('owns').from('o1').to('p1').
.....10> addE('owns').from('o2').to('p2').iterate()
I think you can simplify your traversal to just be a simple repeat()
:
gremlin> g.V('P3').emit(inE('owns')).repeat(__.in('contains'))
==>v[P2]
==>v[P1]
Note the placement of the emit()
step which controls the vertices that are output from the loop.
answered Jan 2 at 20:28
stephen mallettestephen mallette
26.6k33080
26.6k33080
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54012166%2fhow-to-write-gremlin-query-to-find-parent-vertices-which-have-a-specified-edge%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
The picture is nice, but could you please provide a Gremlin script that creates your sample data - here is an example stackoverflow.com/questions/51388315/…
– stephen mallette
Jan 2 at 19:43
@stephenmallette Now I added script for source data, also removed cosmosdb specific query format.
– Sri Harsha Velicheti
Jan 2 at 20:15