gpt4 book ai didi

gremlin - TinkerPop Gremlin,如何过滤和分组子元素

转载 作者:行者123 更新时间:2023-12-05 04:34:38 25 4
gpt4 key购买 nike

我是 Gremlin 的新手,我需要帮助来设置最佳查询以选择唯一和过滤的结果。

从一个 team 开始,我会得到每个 player (注意:每个玩家可以为多个团队效力)由 is_friends_with

连接的团队

结果 (我想得到)

[
{
"Player": "Icardi",
"Teams": ["Valladolid"]
},
{
"Player": "Kroll",
"Teams": ["Valladolid"]
},

{
"Player": "Baggio",
"Teams": ["Eagles"]
},

{
"Player": "Papin",
"Teams": ["Valladolid","Eagls"]
},
]

图表

enter image description here

架构:

 g.addV('team').as('1').
property(single, 'name', 'Eagles').
addV('player').as('2').
property(single, 'name', 'Zico').addV('team').
as('3').
property(single, 'name', 'team A').
addV('team').as('4').
property(single, 'name', 'Horses').
addV('player').as('5').
property(single, 'name', 'Papin').
addV('player').as('6').
property(single, 'name', 'Ronaldo').
addV('player').as('7').
property(single, 'name', 'Visco').
addV('player').as('8').
property(single, 'name', 'Baggio').
addV('tournament').as('9').
addV('team').as('10').
property(single, 'name', 'Valladolid').
addV('player').as('11').
property(single, 'name', 'Kroll').
addV('player').as('12').
property(single, 'name', 'Icardi').
addE('owned').from('1').to('5').addE('owned').
from('1').to('6').addE('owned').from('1').
to('8').addE('owned').from('3').to('6').
addE('owned').from('3').to('7').
addE('created').from('3').to('9').
addE('is_friends_with').from('3').to('10').
addE('is_friends_with').from('3').to('1').
addE('owned').from('4').to('8').addE('owned').
from('4').to('2').addE('owned').from('4').
to('5').addE('owned').from('4').to('7').
addE('invited').from('9').to('1').
addE('invited').from('9').to('4').
addE('owned').from('10').to('11').
addE('owned').from('10').to('12').
addE('owned').from('10').to('5')

最佳答案

这是使用 group 的一种方法

gremlin>  g.V().
......1> has('name','team A').
......2> out('is_friends_with').as('a').
......3> out('owned').
......4> group().
......5> by('name').
......6> by(select('a').values('name').fold()).
......7> unfold()

==>Papin=[Valladolid, Eagles]
==>Icardi=[Valladolid]
==>Baggio=[Eagles]
==>Ronaldo=[Eagles]
==>Kroll=[Valladolid]

要获得与您的 JSON 匹配的确切格式,我们只需在查询中添加一个project 步骤即可。

gremlin>  g.V().
......1> has('name','team A').
......2> out('is_friends_with').as('a').
......3> out('owned').
......4> group().
......5> by('name').
......6> by(select('a').values('name').fold()).
......7> unfold().
......8> project('player','teams').
......9> by(keys).
.....10> by(values)

==>[player:Papin,teams:[Valladolid,Eagles]]
==>[player:Icardi,teams:[Valladolid]]
==>[player:Baggio,teams:[Eagles]]
==>[player:Ronaldo,teams:[Eagles]]
==>[player:Kroll,teams:[Valladolid]]

关于gremlin - TinkerPop Gremlin,如何过滤和分组子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71178226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com