3

g.V().hasLabel('Person').count() gives me number of person vertices present in my database, and g.V().hasLabel('Person').range(10,15) gives me a range of person vertices.

But is there a way to combine these two into a single Gremlin query?

This is just a simplified version of my query, but my actual query is quiet complex and repeating those number of traversals just to find the count, seem ineffective!

Phani
  • 1,851
  • 2
  • 15
  • 28

2 Answers2

2

I just realized, I could use groovy with gremlin to achieve what I want. Not sure how elegant this is!

def lst = g.V().hasLabel('Person').toList(); def result = ["count":0, "data":[] ]; result.count=lst.size();result.data=lst[2..3];result

This is working great even in my complex case.

Phani
  • 1,851
  • 2
  • 15
  • 28
2

I wouldn't recommend to do a full count() in every query. Rather count the total number once and cache it in your application.

That said, here's how you could do it anyway:

g.V().hasLabel('Person').aggregate('x').cap('x').
  project('page3', 'total').by(range(local, 10, 15)).by(count(local))

UPDATE:

In older versions you can try this:

g.V().hasLabel('Person').aggregate('x').cap('x').as('page3', 'total').
  select('page3', 'total').by(range(local, 10, 15)).by(count(local))
Daniel Kuppitz
  • 10,846
  • 1
  • 25
  • 34
  • Thank you Daniel for the answer, but when I tried this, I get: `No signature of method: org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal.project() is applicable for argument types: (java.lang.String, java.lang.String) values: [page3, total] Possible solutions: reset(), collect(), toSet(), inspect(), sort(), inject(groovy.lang.Closure) Display stack trace? [yN]` – Phani Aug 25 '16 at 09:30
  • I am using titan11 branch with Tinkerpop 3.1.1-incubating – Phani Aug 25 '16 at 09:33
  • I am not planning to cache the count in the application, because the data is transactional and the count becomes stale as soon as I serve it in the first response! Hence I would have to do a full count every time. Moreover, this is a sample query and the actual number of vertices / edges I would traverse in full counts would be less because of filters applied. – Phani Aug 25 '16 at 10:31
  • Thank you for the update Daniel, but when I tried the as(..).select(..), I get following error `Server could not serialize the result requested. Server error - Error during serialization: Class is not registered: java.util.LinkedList Note: To register this class use: kryo.register(java.util.LinkedList.class);. Note that the class must be serializable by the client and server for proper operation.` What I couldn't understand is the range step when normally used yields results, but in this case is giving LinkedList! – Phani Aug 26 '16 at 11:05
  • Well that's looks like an issue in Titan then. `LinkedList` should really be serializable by default. See if you can register a custom serializer (it's documented in the Titan docs how that works). – Daniel Kuppitz Aug 26 '16 at 14:02
  • Thank you Daniel, I'll try that! – Phani Aug 29 '16 at 07:48