Donna Schweitzer
Donna Schweitzer

Reputation: 367

Select specific array elements of gremlin query result

I have a dataset with products and orders. For a given product (here PRODUCT 2) I want to find the 5 products that were ordered most in combination with PRODUCT 2.

I have managed to query and sort all associated products with their frequency.

g.V().has('PRODUCT', 'id', '2').as('a').in('purchased').out('purchased').where(neq('a')).groupCount().order(local).by(values, Order.decr)

returns

==>[4:3,6:2,7:2,12:2,13:2,14:2,15:2,1:1,3:1,5:1,8:1,9:1,10:1,11:1,16:1,17:1]

gremlin gives me an array with all the results, so I cannot use the limit step or range step. How could I select only the first five entries of the array?

Upvotes: 0

Views: 697

Answers (1)

stephen mallette
stephen mallette

Reputation: 46216

Note that groupCount() returns a Map not an "array". Also, note that you already are using the mechanism to do what you want to do - Scope.local. Both limit() and range() can take a Scope so you can do:

 g.V().has('PRODUCT', 'id', '2').as('a').
   in('purchased').out('purchased').
   where(neq('a')).
   groupCount().
   order(local).by(values, Order.decr).
   limit(local,5)

It's worth remembering that a Map is just a collection and can therefore be deconstructed to its item with unfold() in which case:

 g.V().has('PRODUCT', 'id', '2').as('a').
   in('purchased').out('purchased').
   where(neq('a')).
   groupCount().
   order(local).by(values, Order.decr).
   unfold()
   limit(5)

Upvotes: 1

Related Questions