0

图数据库

上图是我的图表的样子,我想查询以下输出:

[
    {
        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',
                    },
                ],
            },
        ],
    }
]

但是对于我当前的查询:

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()
)

这就是我得到的:

[
    {
        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 

正如您从实际输出中看到的那样,我的查询问题是chart1重复的variable1,而chart2' 的变量year3来自chart1. 我是否必须更改图表的结构?还是我必须添加其他属性以供参考?请帮忙。

查询种子图:

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:我正在使用 Azure Cosmos DB 的 gremlin API。

4

1 回答 1

1

我可以提出一个我不确定是不是最简单的解决方案,但它确实有效:

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())

我在这里测试过: https ://gremlify.com/6h

于 2020-04-16T09:01:10.340 回答