Select doesn't iterate within gremlin query. Help requested

24 Views Asked by At

I've tried slicing and dicing this query multiple ways in order to allow the DB to a majority of the bulk processing vs. on the client side. To do this I plan on injecting a map, we can start with the two in the example below. Unfortunately merging or adding coalesce logic using hasLabel() and other results in the actual iteration of the list not happening. Example below:

The query below is what I'm trying to get to work. Unfortunately it seems to return the same ID. This tells me it's not iterating once fired off.

g.inject(
  [
    [stock_height:111, sh_id:"sh123", inputs: [house: "inputhouse123ia"], outputs: [[house: "outputhouse123oa", group_id: "gid-123a"],[house: "outputhouse123ob", group_id: "gid-123b"]]],
    [stock_height:111, sh_id:"sh1234", inputs: [house: "inputhouse1234ia"], outputs: [[house: "outputhouse1234oa", group_id: "gid-1234a"],[house: "outputhouse1234ob", group_id: "gid-1234b"]]]
  ]).
  unfold().as('sh').
  select('outputs').
  unfold().as('output').select('group_id').as('gid').
  mergeV([(label): 'Peoplegroup']).property('group_id', select('gid')).id()

Output: [ 17846400, 17846400, 17846400, 17846400 ]

Using select outside of addV() or mergeV() seems to work fine.

g.inject(
  [
    [stock_height:111, sh_id:"sh123", inputs: [house: "inputhouse123ia"], outputs: [[house: "outputhouse123oa", group_id: "gid-123a"],[house: "outputhouse123ob", group_id: "gid-123b"]]],
    [stock_height:111, sh_id:"sh1234", inputs: [house: "inputhouse1234ia"], outputs: [[house: "outputhouse1234oa", group_id: "gid-1234a"],[house: "outputhouse1234ob", group_id: "gid-1234b"]]]
  ]).
  unfold().as('sh').
  select('outputs').
  unfold().as('output').select('group_id').as('gid').
  select('gid')

Output: [ "gid-123a", "gid-123b", "gid-1234a", "gid-1234b" ]

Any ideas? I'm at a loss of new ideas, and any help would be greatly appreciated.

I was hoping to add 4 new gid vertices to the graph, or return the id's which already exist. The map up could be simplified for testing purposes.

1

There are 1 best solutions below

4
Kelvin Lawrence On

Because your mergeV is only matching on a label, the first time it is executed, none exist. So it creates a Peoplegroup node. After that each of the other mergeV find that initial one. You will notice that by the end the property is actually set to gid-1234b (the last one) as it has matched each one and replaced the property. To get a different behavior you need to introduce something more unique to match on, or just inject the whole map and not use property at all.

You can actually observe this behavior by changing the cardinality on the property step to list:

gremlin> g.inject(
......1>   [
......2>     [stock_height:111, sh_id:"sh123", inputs: [house: "inputhouse123ia"], outputs: [[house: 
"outputhouse123oa", group_id: "gid-123a"],[house: "outputhouse123ob", group_id: "gid-123b"]]],
......3>     [stock_height:111, sh_id:"sh1234", inputs: [house: "inputhouse1234ia"], outputs: [[house
: "outputhouse1234oa", group_id: "gid-1234a"],[house: "outputhouse1234ob", group_id: "gid-1234b"]]]
......4>   ]).
......5>   unfold().as('sh').
......6>   select('outputs').
......7>   unfold().as('output').select('group_id').as('gid').
......8>   mergeV([(label): 'Peoplegroup']).property(list,'group_id', select('gid')).id()
==>14
==>14
==>14
==>14        

gremlin> g.V().valueMap(true)
==>[id:14,label:Peoplegroup,group_id:[gid-123a,gid-123b,gid-1234a,gid-1234b]]