can we relate a property with other property of same vertex

孤人 提交于 2020-06-29 03:21:23

问题


A sample vertex

g.addV('a').property('vehicle','v1').property('time',1000).property(list,'vehicle','v2').property(list,'time',830)

how can we map the values in property 'vehicle' to the values in property 'time'. I tired the following code

g.V(1).hasKey('vehicle').map(hasKey('time'))

To find path I tired following code

g.V().hasLabel('a').repeat(out().simplePath()).until(hasLabel('h')).path().by(union(label(),values('vehicle','time')).fold())

can you help me please


回答1:


Changing the data model makes these kinds of queries easier to write. If you use vertices and edges to model the vehicles and times instead of lists stored as properties then you can do something like this.

g.addV('A').as('a').
  addV('B').as('b').
  addV('C').as('c').
  addV('vehicle').as('v1').property('time',10).property('name','V1').
  addV('vehicle').as('v2').property('time',8).property('name','V2').
  addV('vehicle').as('v3').property('time',5).property('name','V3').
  addV('vehicle').as('v4').property('time',6).property('name','V4').
  addV('vehicle').as('v5').property('time',5).property('name','V5').
  addV('vehicle').as('v6').property('time',6).property('name','V6').
  addE('route').from('a').to('b').
  addE('route').from('b').to('c').
  addE('vehicle_info').from('a').to('v1').
  addE('vehicle_info').from('a').to('v2').
  addE('vehicle_info').from('b').to('v3').
  addE('vehicle_info').from('b').to('v4').
  addE('vehicle_info').from('c').to('v5').
  addE('vehicle_info').from('c').to('v6').
  iterate()

gremlin> g.V().hasLabel('A').
......1>   repeat(out('route').simplePath()).
......2>     until(hasLabel('C')).
......3>   path().
......4>     by(union(label(),out('vehicle_info').values('name','time').fold()).fold())

==>[[A,[V1,10,V2,8]],[B,[V3,5,V4,6]],[C,[V5,5,V6,6]]]  

EDITED to add:

You can filter by the time as follows:

gremlin> g.V().hasLabel('A').
......1>   repeat(out('route').simplePath()).
......2>     until(hasLabel('C')).
......3>   path().
......4>     by(union(label(),out('vehicle_info').has('time',between(5,10)).values('name','time').fold())
.fold()) 
==>[[A,[V2,8]],[B,[V3,5,V4,6]],[C,[V5,5,V6,6]]]  



回答2:


This can be accomplished like this:

g.V(18).property('time', __.values('vehicle'))

Which will map the list property like this:

gremlin> g.addV('a').property('vehicle','v1').property('time',1000).property(list,'vehicle','v2').property(list,'time',830)
==>v[18]
gremlin> g.V(18).valueMap()
==>[time:[1000,830],vehicle:[v1,v2]]
gremlin> g.V(18).property(list, 'time', __.values('vehicle'))
==>v[18]
gremlin> g.V(18).valueMap()
==>[time:[v1,v1],vehicle:[v1,v2]]


来源:https://stackoverflow.com/questions/62465145/can-we-relate-a-property-with-other-property-of-same-vertex

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!