Search code examples
gremlintinkerpopgremlinpython

search for value by key within dynamic collection


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.


Solution

  • 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()