The image above is how my graph looks like, and i would like to query the following output:
[
{
chart:{
name: 'chart1',
id: 'chart1',
label: 'chart',
pk: 'chart1',
},
variables: [
{
variable: {
name:'variable1'
id: 'variable1',
label: 'variable',
pk: 'variable1',
},
years: [
{
name:'year1'
id: 'year1',
label: 'year',
pk: 'year1',
},
{
name:'year2'
id: 'year2',
label: 'year',
pk: 'year2',
},
],
},
{
variable: {
name:'variable2'
id: 'variable2',
label: 'variable',
pk: 'variable2',
},
years: [
{
name:'year3'
id: 'year3',
label: 'year',
pk: 'year3',
},
],
}
],
},
{
chart:{
name: 'chart2',
id: 'chart2',
label: 'chart',
pk: 'chart2',
},
variables: [
{
variable: {
name:'variable2'
id: 'variable2',
label: 'variable',
pk: 'variable2',
},
years: [
{
name:'year4'
id: 'year4',
label: 'year',
pk: 'year4',
},
],
},
],
}
]
But with my current query:
g
.V()
.hasLabel('chart')
.project('chart', 'variables')
.by(valueMap(true))
.by(
out('visualizes')
.hasLabel('year')
.out('outputs')
.hasLabel('variable')
.project('variable', 'years')
.by(
valueMap(true).fold()
)
.by(
__.in('outputs')
.hasLabel('year')
.valueMap(true)
.fold()
)
.fold()
)
This is what im getting:
[
{
chart: {
id: 'chart1',
label: 'chart',
name: 'chart1',
pk: 'chart1',
},
variables: [
{
variable: [
{
id: 'variable1',
label: variable,
name: 'variable1',
pk: 'variable1'
}
],
years: [
{
id: 'year1',
label: 'year',
name: 'year1',
pk: 'year1',
},
{
id: 'year2',
label: "year",
name: 'year2',
pk: 'year2'
},
]
},
{
variable: [
{
id: 'variable1',
label: 'variable',
name: 'variable1',
'pk': 'variable1'
}
],
years: [
{
id: 'year1',
label: 'year',
name: 'year1',
pk: 'year1'
},
{
id: 'year2',
label: 'year',
name: 'year2',
pk: 'year2'
}
]
},
{
variable: [
{
id: 'variable2',
label: "variable",
name: 'variable2',
pk: 'variable2'
}
],
years: [
{
id: 'year3',
label: 'year',
name: 'year3',
pk: 'year3',
},
{
id: 'year4',
label: "year",
name: 'year4',
pk: 'year4'
}
]
}
]
},
{
chart: {
id: 'chart2',
label: 'chart',
name: 'chart2',
pk: 'chart2'
},
variables: [
{
variable: [
{
id: 'variable2',
label: 'variable',
name: 'variable2',
pk: 'variable2'
}
],
years: [
{
id: 'year3',
label: 'year',
name: 'year3',
pk: 'year3'
},
{
id: 'year4',
label: 'year',
name: 'year4',
pk: 'year4'
}
]
}
]
}
]
//this is the formatted version to be easily read. refer to this link for the actual json: https://pastebin.com/Y2ncHyPi
As you can see from the actual output my problem with the query, is chart1
duplicates variable1
and chart2
's variable have year3
which is from chart1
. Do i have to change the structure of my graph? or do i have to add additional properties for reference? please help.
Query for seeding graph:
g
.addV('chart')
.property('name', 'chart1')
.property('pk', 'chart1')
.property('id', 'chart1')
.as('chart1')
.addV('chart')
.property('name', 'chart2')
.property('pk', 'chart2')
.property('id', 'chart2')
.as('chart2')
.addV('year')
.property('name', 'year1')
.property('pk', 'year1')
.property('id', 'year1')
.as('year1')
.addV('year')
.property('name', 'year2')
.property('pk', 'year2')
.property('id', 'year2')
.as('year2')
.addV('year')
.property('name', 'year3')
.property('pk', 'year3')
.property('id', 'year3')
.as('year3')
.addV('year')
.property('name', 'year4')
.property('pk', 'year4')
.property('id', 'year4')
.as('year4')
.addV('variable')
.property('name', 'variable1')
.property('pk', 'variable1')
.property('id', 'variable1')
.as('variable1')
.addV('variable')
.property('name', 'variable2')
.property('pk', 'variable2')
.property('id', 'variable2')
.as('variable2')
.addE('visualizes')
.from('chart1')
.to('year1')
.addE('outputs')
.from('year1')
.to('variable1')
.addE('visualizes')
.from('chart1')
.to('year2')
.addE('outputs')
.from('year2')
.to('variable1')
.addE('visualizes')
.from('chart1')
.to('year3')
.addE('outputs')
.from('year3')
.to('variable2')
.addE('visualizes')
.from('chart2')
.to('year4')
.addE('outputs')
.from('year4')
.to('variable2')
PS: I am using Azure Cosmos DB's gremlin API.
I can suggest a solution that I'm not sure is the simplest one, but it works:
g.V().hasLabel('chart').
project('chart', 'variables').
by(valueMap(true)).
by(out('visualizes').
hasLabel('year').as('y').
out('outputs').
group().by().
by(select('y').fold()).unfold().
project('variable', 'years').
by(select(keys).
valueMap(true)).
by(select(values).unfold().
valueMap(true).fold()).fold())
I tested it here: https://gremlify.com/6h