Reputation: 288
Suppose I have a list of attribute values as an ArrayList, How can I filter nodes by the values in the list.
Is something like this possible...
g.V().filter {it.get().value("name") in list}
also is it compatible with TinkerPop 2.x
Upvotes: 1
Views: 7406
Reputation: 11453
If the arrayList is part of existing traversal collected using store
, you can use something like below.
Listing here in case someone runs into this situation.(like I did)
Say you have collection names
collected in previous traversals and want to filter vertices based on that in subsequent traversal / steps.
g.V().out().store('names').by('name')
.out()
.where(within('names'))
.by('name')
.by()
.dedup()
...... further traversal
The above solution is derived from my question here answered by @Kelvin Lawrence
Upvotes: 0
Reputation: 6792
Using the filter()
step can work:
gremlin> [ 'Titan' : Titan.version(), 'TinkerPop' : Gremlin.version()]
==>Titan=0.5.4
==>TinkerPop=2.5.0
gremlin> g = TitanFactory.open('inmemory')
==>titangraph[inmemory:[127.0.0.1]]
gremlin> v0 = g.addVertex().setProperty('name', 'amith')
==>null
gremlin> v1 = g.addVertex().setProperty('name', 'jason')
==>null
gremlin> v2 = g.addVertex().setProperty('name', 'stephen')
==>null
gremlin> g.commit()
==>null
gremlin> l = ['amith', 'jason', 'florian'] // list to match
==>amith
==>jason
==>florian
gremlin> g.V().filter{ l.contains(it.getProperty('name')) }.map()
15:06:30 WARN com.thinkaurelius.titan.graphdb.transaction.StandardTitanTx - Query requires iterating over all vertices [()]. For better performance, use indexes
==>{name=amith}
==>{name=jason}
Keep in mind that Titan 0.5.4 is quite dated (released in Feb 2015), and it depends on TinkerPop 2.5.0 (released in Apr 2014). Titan and TinkerPop 2.x are no longer under active development.
You should consider moving on to JanusGraph, which is a fork of Titan, has an active and open community, and is keeping current with the latest releases of Apache TinkerPop 3.x.
Upvotes: 3
Reputation: 2809
You can simply use the has
step for the filtering as it can take a list of values thanks to the within
predicate:
gremlin> g = TinkerFactory.createModern().traversal()
==>graphtraversalsource[tinkergraph[vertices:6 edges:6], standard]
gremlin> names=['josh','peter']; g.V().has('name', within(names))
==>v[4]
==>v[6]
edit: My answer doesn't really address the question anymore as it now asks for a TinkerPop 2 solution. I'm still leaving it here in case anyone stumbles upon this question while looking for a solution for TinkerPop 3.
Upvotes: 9