titangremlintinkerpop-blueprint

How to use "AND" condition in GremlinPipeLine


Suppose I have a list of vertices Person with attributes name and age.I want to write a query that results in a vertex with name="John" and age=22.

Pipe pipe = (Pipe) new GremlinPipeline(graph).V().has("name", "John");
Pipe pipe2=new GremlinPipeline(graph).V().has("age", "22");
List<Vertex>  verList = (List<Vertex>) new GremlinPipeline(graph).V().and(pipe,pipe2).toList();

I dont know if this is the correct way to implement it.It is throwing the below exception

Exception in thread "main" java.lang.ClassCastException: com.thinkaurelius.titan.graphdb.vertices.CacheVertex cannot be cast to com.tinkerpop.blueprints.Graph
at com.tinkerpop.pipes.transform.GraphQueryPipe.processNextStart(GraphQueryPipe.java:33)
at com.tinkerpop.pipes.transform.GraphQueryPipe.processNextStart(GraphQueryPipe.java:17)
at com.tinkerpop.pipes.AbstractPipe.next(AbstractPipe.java:89)
at com.tinkerpop.pipes.IdentityPipe.processNextStart(IdentityPipe.java:19)
at com.tinkerpop.pipes.AbstractPipe.hasNext(AbstractPipe.java:98)
at com.tinkerpop.pipes.util.Pipeline.hasNext(Pipeline.java:105)
at com.tinkerpop.pipes.transform.HasNextPipe.processNextStart(HasNextPipe.java:36)
at com.tinkerpop.pipes.transform.HasNextPipe.processNextStart(HasNextPipe.java:16)
at com.tinkerpop.pipes.AbstractPipe.next(AbstractPipe.java:89)
at com.tinkerpop.pipes.filter.AndFilterPipe.processNextStart(AndFilterPipe.java:35)
at com.tinkerpop.pipes.AbstractPipe.next(AbstractPipe.java:89)
at com.tinkerpop.pipes.util.Pipeline.next(Pipeline.java:115)
at com.tinkerpop.pipes.util.PipeHelper.fillCollection(PipeHelper.java:52)
at com.tinkerpop.gremlin.java.GremlinPipeline.toList(GremlinPipeline.java:1564)
at BulkLoad_New.search(BulkLoad_New.java:320)
at BulkLoad_New.main(BulkLoad_New.java:71)

Solution

  • I don't think there's much need for use of and step here. Just pipeline both has conditions:

    g.V.has('name','John').has('age',22)
    

    That's effectively an AND operation. In Java I guess this would be:

    new GremlinPipeline(graph).V().has("name", "John").has("age",22);
    

    Please read this post on how to convert from groovy to java for more information on that topic.