0

I am new to Gremlin. Saw Answer from stephen mallette and here is the code from the same answer

g.v('u1','u2').out('like').groupCount().cap.next().sort{-it.value}

How can I improve this query to sort product having type=Camera based on likes. Consider paging with 10 record per page. Will it be a efficient solution to show the sorted result with large number of products?

Community
  • 1
  • 1
New Baby
  • 11
  • 2

1 Answers1

0

You really just need to apply a filter to get the product type you want:

g.v('u1','u2').out('like').has('type','camera').groupCount().cap.next().sort{-it.value}

It may or may not be efficient to do sorting/paging this way. I suppose it depends on how many likes you are dealing with. Thousands might not be a big deal, but hundreds of thousands might be less than what you can endure.

As you are using Titan you have more options than most graphs to solve this problem (if it actually is a problem - I suggest you generate test data and write some queries to see for sure). One thing you might do is denormalize the "type" property to the "like" edge so that you can apply your "type" filter on outE as opposed to forcing the traversal over out as shown above (forcing the traversal means Titan needs to find the vertex at the other end of the edge which adds to the amount of data Titan needs to retrieve). You can read more about these options here.

stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • are u saying that i should use edge "like" only not "type" property on it – New Baby Sep 15 '14 at 12:37
  • well, i thought you had 'type' on the 'product' vertex. if you denormalize `type` to the `likes` edge like I suggested then you would do something more like: `g.v('u1','u2').outE('like').has('type','camera').inV.groupCount().cap.next().sort{-it.value}` – stephen mallette Sep 15 '14 at 12:39
  • i will try this. In titan i was usgin throug java and i saw it has a bit complicate query comapared to gramlin. e.g pipe.start(v).inE("inEdge").has("inEdgeProperty", "XXXXX").outV().has('vertexProps''xyz')` in Titan using java first `v.query().direction(Direction.IN).labels("inEdge").has("inEdgeProperty", "XXXXX").vertices().iterator()` then iterate it separate for `has('vertexProps''xyz')` – New Baby Sep 15 '14 at 13:07
  • Gremlin is much more expressive than the lower level Titan/Blueprints API, so generally speaking Gremlin will lead to less code, more readable code and better maintainability over time. – stephen mallette Sep 15 '14 at 13:09
  • We can use gremlin using java with titan using `GremlinPipeline`. will it be comply with titan standard – New Baby Sep 15 '14 at 13:15
  • You can write Gremlin in java and it will work fine. However, you will find less support from the mailing list and less documentation with "gremlin java" than if you used groovy. You will also find that the gremlin java is verbose (as is the nature of java to some degree) so you won't have nice compact lines of Gremlin code as you would if you just used groovy. If you don't have a good reason for not using groovy, i would consider converting to a mixed java/groovy project (which is super easy especially if using maven) so that you can share classes between the two languages seamlessly. – stephen mallette Sep 15 '14 at 13:19
  • You will be much more productive if you take that approach. – stephen mallette Sep 15 '14 at 13:19
  • In java i tried this for whole graph `pipe.V().inE("like").has("vertexType","page").groupCount().cap().order(TransformPipe.Order.DECR)` it throws `.NullPointerException at com.tinkerpop.pipes.transform.GraphQueryPipe.processNextStart(GraphQueryPipe.java:33)` – New Baby Sep 17 '14 at 19:13
  • well, first of all i think you want to do `orderMap`, not `order`. Not sure why you are getting the NPE. how did you define `pipe`? – stephen mallette Sep 17 '14 at 19:44
  • `GremlinPipeline pipe = new GremlinPipeline();` – New Baby Sep 18 '14 at 04:09
  • You didn't define how the pipe starts. `new GremlinPipeline(someStartObject)` https://github.com/tinkerpop/gremlin/wiki/Using-Gremlin-through-Java – stephen mallette Sep 18 '14 at 10:48
  • I have seen that doc.the thing is i have to scan and order all vertices with `type=page` and having `like` edge as coming IN. So in that case how will I know that where to start. – New Baby Sep 18 '14 at 13:50
  • give the `GremlinPipeline` an iterator - `new GremlinPipeline(g.getVertices())`. i think that's how you do that in java. – stephen mallette Sep 18 '14 at 14:18