zeevo
zeevo

Reputation: 23

In tinkerpop gremlin, how do I search for Vertices where a property value is a subset of an argument?

I have a graph with Vertices that have a property "foo" that is a Set. I want to find all Vertices where "foo" is a subset of my input. For example,

g.addV('v').property('foo', 'bar').property('foo','baz')

I want to query this graph such that if given the input of ["bar, "quux", "quuz"], I will find no vertices that qualify because the Vertex property is not a subset of ["bar, "quux", "quuz"]

If I were given input of ["bar", "quux, "quuz", "baz"], then this Vertex would be returned as ["bar", "baz"] is a subset of ["bar", "quux, "quuz", "baz"]

Upvotes: 2

Views: 546

Answers (1)

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14371

This kind of set intersection is tricky as the Gremlin language does not have a containsAll type of method the way Groovy does. If you are able to include Groovy closures in your query (not all implementations allow them), one way to do it is as follows:

gremlin> g.addV('v').property('foo', 'bar').property('foo','baz')
==>v[61286]  

gremlin>  g.V().hasLabel('v').
......1>        filter {["bar", "quux", "quuz", "baz"].
......2>                containsAll(it.get().values('foo').
......3>                collect())}  
==>v[61286]  

gremlin> g.V().hasLabel('v').property(set,'foo','abc')
==>v[61286]

gremlin> g.V().hasLabel('v').valueMap()
==>[foo:[bar,baz,abc]]

gremlin>  g.V().hasLabel('v').
......1>        filter {["bar", "quux", "quuz", "baz"].
......2>                containsAll(it.get().values('foo').
......3>                collect())}  
gremlin>   

In general use of closures is not recommended as not all implementations allow them and they are less likely to be able to take advantage of back end optimizations and indices.

Upvotes: 2

Related Questions