Re: [TinkerPop] Query Help

18 views
Skip to first unread message

Marko Rodriguez

unread,
May 14, 2013, 5:54:26 PM5/14/13
to gremli...@googlegroups.com
Hi,

So, this is my second day with gremlin and I've got a few scripts that I've cobbled together.  These were originally cypher queries.  They work as is, but I feel like there may be a more elegant or performant way to write them.

All of my answers are with respect to Gremlin 2.3.0. If you have been using Neo4j Gremlin, its currently only at 1.5 and lots of developments have been made since so please upgrade if possible.

The graph I'm working with looks something like this:
P <-child_of- C -located_at-> L

the C node also has the following relationship:
C <-designator- D
D can be the designator for more than 1 C

All nodes have an external ID that is generated on insertion (extId) and a type (extType).  I have an index that keys on both of those attributes (Model).

K.


Here I'm trying to find all the nodes of type P where the associated C count is different from the L count.
g.idx("Model")[[extType: "P"]].filter{c = it.in("child_of").count(); l = it.in("child_of").out("located_at").dedup().count(); c != l}
The double traversal of the "child_of" relationship seems unncessary, is there a way to rewrite this or optimize the filter?

First, you should not use manual indices. Most graph vendors are moving away from manual indices.

Second, you can do it like this:

g.V('extType','P').filter{
children = it.in('childOf').toList();
children.size() != children._().out('locatedAt').dedup.count()
}


In this script I'm trying to find other P nodes that share a designator D with a provider P node (pid).  The filter for extType P at the beginning is just a safeguard to ensure that the pid passed in really points to a P.
g.idx("Model")[[extId: pid]].filter{it.extType == "P"}.as("top").in("child_of").in("designator").dedup().out("designator").out("child_of").dedup().filter{p,m -> p.extId != m.top.extId}
Is this the presribed way to reference an instance(top) from a previous stage in the pipe?

Yea. That is what the AsMap is for. However, you could use the except()-step (which is just a nicer way of using AsMap for this situation).

g.V('extId','pid').has('extType','P').as('top').in('childOf').in('designator').dedup.out('designator').out('childOf').dedup.except('top')

Any help is greatly appreciated.  Thanks in advance!
  --m

Enjoy.

Please have a look at these two page as they may help you out a bit:
https://github.com/tinkerpop/gremlin/wiki/Traversal-Optimization (GraphQuery optimization is in 2.4.0-SNAPSHOT)

Take care,
Marko.

Reply all
Reply to author
Forward
0 new messages