添加顶点时有条件地在边上设置 属性 值 [GREMLIN API]
Conditionally set a property value on edge when adding a vertex [GREMLIN API]
我正在尝试添加一个顶点,该顶点将链接到另一个顶点,并且在它们的边之间具有条件 属性 值。
到目前为止,这是我想出的:
- 这运行没有错误,但我无法获得任何结果。
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))
我在这里做的是尝试在 image
顶点和 product
顶点之间设置新添加的边的 primary
属性,这取决于是否 product
已经连接到 image
,其中边已经将 primary
设置为 true。
如果 product
已经有一个带边缘的 image
属性: primary:true
那么将链接到产品的新添加的图像应该有边缘与 属性 primary:false
种子 azure 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)
我很惊讶你的遍历运行没有错误,因为我遇到了关于你使用 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()
不一致,但它不是一个糟糕的选择。
我正在尝试添加一个顶点,该顶点将链接到另一个顶点,并且在它们的边之间具有条件 属性 值。
到目前为止,这是我想出的: - 这运行没有错误,但我无法获得任何结果。
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))
我在这里做的是尝试在 image
顶点和 product
顶点之间设置新添加的边的 primary
属性,这取决于是否 product
已经连接到 image
,其中边已经将 primary
设置为 true。
如果 product
已经有一个带边缘的 image
属性: primary:true
那么将链接到产品的新添加的图像应该有边缘与 属性 primary:false
种子 azure 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)
我很惊讶你的遍历运行没有错误,因为我遇到了关于你使用 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()
不一致,但它不是一个糟糕的选择。