1

I am looking to extend discussion held here about filtering vertices based on property value from collection. However, in my case the collection is dynamic e.g. generated using store

e.g.

following works using a static list ['red', 'green']. I am able to filter out vertices with just 'red', 'green' as value in property 'color_name'.

g.V().out().as('colors').store('color_list').by('name') 
.out()
.out().has('color_name', within(['red', 'green']))
.dedup()
.path()

but if I try to use collection from previous store it doesnt work. color_list has same members as above. = ['red', 'green']

 g.V().out().as('colors').store('color_list').by('name')
    .out()
    .out().has('color_name', within('color_list'))
    .dedup()
    .path()

I must be missing something. My understanding of gremlin traversal is still in infancy.

Anil_M
  • 10,893
  • 6
  • 47
  • 74

1 Answers1

1

For that to work you need to use a where step. The has step will interpret 'color_list' as a literal string and not the label for something named earlier in the query. The query needs to be along these lines:

 g.V().out().as('colors').store('color_list').by('name')
      .out()
      .out()
      .where(within('color_list'))
        .by('color_name')
        .by()
    .dedup()
    .path()
Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • That works perfectly. Many thanks for quick response. i don't yet understand .`where(within('color_list')) .by('color_name').by() ` logic - so we are looking within `color_list` collection with data filtered on `color_name` from previous traversal using first by ? why do we need a second by() ? Is there a reference documentation to understand it better. thx. – Anil_M Mar 31 '23 at 22:50
  • 1
    Think of it this way - `within` needs two values. The item flowing into the step, and the list of things we want to check against. The first `by` represents the item flowing in - in this case a vertex. The first `by` lists the property on that vertex to use. The second `by` tells the `where` step what to use for the 'color_list'. As in this case that is the list itself we can just use an empty `by()`. It is shorthand for `by(select('color_list'))` essentially. We need to provide two `by` because otherwise Gremlin will try to round robin the first one, and that will not work in this case. – Kelvin Lawrence Mar 31 '23 at 23:53