[在添加顶点[GREMLIN API]时有条件地在边缘上设置属性值

问题描述 投票:1回答:1

我正在尝试添加一个将与另一个顶点链接的顶点,并且它们的边缘之间具有条件属性值。

到目前为止,这是我想出的:-这没有错误,但是我无法获得任何结果。

g.V().has('label', 'product')
   .has('id', 'product1')
   .outE('has_image')
   .has('primary', true)
   .inV()
   .choose(fold().coalesce(unfold().values('public_url'), constant('x')).is(neq('x')))
       .option(true, 
           addV('image')
               .property('description', '')
               .property('created_at', '2019-10-31 09:08:15')
               .property('updated_at', '2019-10-31 09:08:15')
               .property('pk', 'f920a210-fbbd-11e9-bed6-b9a9c92913ef')
               .property('path', 'product_images/87wfMABXBodgXL1O4aIf6BcMMG47ueUztjNCkGxP.png')
               .V()
               .hasLabel('product')
               .has('id', 'product1')
               .addE('has_image')
               .property('primary', false))
       .option(false, 
           addV('image')
               .property('description', '')
               .property('created_at', '2019-10-31 09:08:15')
               .property('updated_at', '2019-10-31 09:08:15')
               .property('pk', 'f920a930-fbbd-11e9-b444-8bccc55453b9')
               .property('path', 'product_images/87wfMABXBodgXL1O4aIf6BcMMG47ueUztjNCkGxP.png')
               .V()
               .hasLabel('product')
               .has('id', 'product1')
               .addE('has_image')
               .property('primary', true))

[我在这里做什么是在primary顶点和image顶点之间尝试设置新添加的边的product属性,这取决于product是否已连接到边缘的image已经将primary设置为true。

如果product已经具有带有边缘属性的imageprimary:true,那么将要链接到产品的新添加的图像应具有具有属性primary:false的边缘

种子天蓝色graphdb:

//add product vertex
g.addV('product').property(id, 'product1').property('pk', 'product1')

//add image vertex
g.addV('image').property(id, 'image1').property('public_url', 'url_1').property('pk', 'image1');


//link products to images

g.V('product1').addE('has_image').to(V('image1')).property('primary', true)

graph-databases gremlin azure-cosmosdb-gremlinapi
1个回答
2
投票

[我很惊讶您的遍历没有错误,因为我遇到了一些有关使用option()的语法问题,以及您混合使用T.id和“ id”的属性键时遇到的其他一些问题(后者可能成为您的问题的一部分,为什么它不能按原样工作,但我不太确定)。当然,我没有在CosmosDB上进行测试,所以也许他们对Gremlin语言拥有了如此的自由。

无论如何,假设我正确地遵循了您的解释,我认为有一种方法可以大大简化您的Gremlin。我认为您只需要这个:

g.V('product1').as('p').
  addV('image').
    property('description', '').
    property('created_at', '2019-10-31 09:08:15').
    property('updated_at', '2019-10-31 09:08:15').
    property('pk', 'f920a210-fbbd-11e9-bed6-b9a9c92913ef').
    property('path', 'product_images/87wfMABXBodgXL1O4aIf6BcMMG47ueUztjNCkGxP.png').
    addE('has_image').
      from('p').
    property('primary', choose(select('p').outE('has_image').values('primary').is(true), 
                          constant(false), constant(true)))

现在,我想说这是Gremlin上最惯用的方法,而且由于我尚未在CosmosDB上进行测试,所以我不能说这种方法是否对您有用,但是也许在下面的控制台会议中您会看到,它确实满足您的期望:

gremlin> g.V('product1').as('p').
......1>   addV('image').
......2>     property('description', '').
......3>     property('created_at', '2019-10-31 09:08:15').
......4>     property('updated_at', '2019-10-31 09:08:15').
......5>     property('pk', 'f920a210-fbbd-11e9-bed6-b9a9c92913ef').
......6>     property('path', 'product_images/87wfMABXBodgXL1O4aIf6BcMMG47ueUztjNCkGxP.png').
......7>     addE('has_image').
......8>       from('p').
......9>     property('primary', choose(select('p').outE('has_image').values('primary').is(true), constant(false), constant(true)))
==>e[31][product1-has_image->25]
gremlin> g.E().elementMap()
==>[id:31,label:has_image,IN:[id:25,label:image],OUT:[id:product1,label:product],primary:true]
gremlin> g.V('product1').as('p').
......1>   addV('image').
......2>     property('description', '').
......3>     property('created_at', '2019-10-31 09:08:15').
......4>     property('updated_at', '2019-10-31 09:08:15').
......5>     property('pk', 'f920a210-fbbd-11e9-bed6-b9a9c92913ef').
......6>     property('path', 'product_images/87wfMABXBodgXL1O4aIf6BcMMG47ueUztjNCkGxP.png').
......7>     addE('has_image').
......8>       from('p').
......9>     property('primary', choose(select('p').outE('has_image').values('primary').is(true), constant(false), constant(true)))
==>e[38][product1-has_image->32]
gremlin> g.E().elementMap()
==>[id:38,label:has_image,IN:[id:32,label:image],OUT:[id:product1,label:product],primary:false]
==>[id:31,label:has_image,IN:[id:25,label:image],OUT:[id:product1,label:product],primary:true]
gremlin> g.V('product1').as('p').
......1>   addV('image').
......2>     property('description', '').
......3>     property('created_at', '2019-10-31 09:08:15').
......4>     property('updated_at', '2019-10-31 09:08:15').
......5>     property('pk', 'f920a210-fbbd-11e9-bed6-b9a9c92913ef').
......6>     property('path', 'product_images/87wfMABXBodgXL1O4aIf6BcMMG47ueUztjNCkGxP.png').
......7>     addE('has_image').
......8>       from('p').
......9>     property('primary', choose(select('p').outE('has_image').values('primary').is(true), constant(false), constant(true)))
==>e[45][product1-has_image->39]
gremlin> g.E().elementMap()
==>[id:38,label:has_image,IN:[id:32,label:image],OUT:[id:product1,label:product],primary:false]
==>[id:45,label:has_image,IN:[id:39,label:image],OUT:[id:product1,label:product],primary:false]
==>[id:31,label:has_image,IN:[id:25,label:image],OUT:[id:product1,label:product],primary:true]

[如果看起来正确,并且在CosmosDB中无法正常工作,那是因为第9行使用Traversal作为property()的参数,但CosmosDB尚不支持。补救措施是将那条线稍微反转一下:

g.V('product1').as('p').
  addV('image').
    property('description', '').
    property('created_at', '2019-10-31 09:08:15').
    property('updated_at', '2019-10-31 09:08:15').
    property('pk', 'f920a210-fbbd-11e9-bed6-b9a9c92913ef').
    property('path', 'product_images/87wfMABXBodgXL1O4aIf6BcMMG47ueUztjNCkGxP.png').
    addE('has_image').
      from('p').
    choose(select('p').outE('has_image').values('primary').is(true), 
           property('primary', false), 
           property('primary', true))

我发现这种方法的可读性稍差,因为property()addE()不一致,但这不是一个糟糕的选择。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.