Excluding a list of vertices already visited

192 views
Skip to first unread message

Jean-Yves Lalanne

unread,
Apr 29, 2016, 2:46:11 PM4/29/16
to Gremlin-users
Hi,

I'm a new gremlin-user and I've created a graph with customers and products to learn it.

The edges are:
customer --[bought]--> product (to know who bought what)
product1 --[bought with]--> product2 (to know which products are usually bought together by customers)

Let's say we have this simple example:
customer1 --[bought]-->product1--[bought with]-->product2

I want to get all related products to customer1's shopping (excluding already bought products).

I wrote a query but the exclusion doesn't work. What am I doing wrong please?

gremlin> :> g.V().has('db_id','321').out('bought').as('exclude').in('bought with').where(neq('exclude')).values("name").groupCount().next().sort{-it.value}[0..2]
==>DummyProduct-72=8
==>DummyProduct-68=7
==>DummyProduct-20=7

gremlin> :> g.V().has('db_id','321').out('bought').dedup().values("name").order()
==>DummyProduct-10
==>DummyProduct-17
==>DummyProduct-18
==>DummyProduct-19
==>DummyProduct-2
==>DummyProduct-20
==>DummyProduct-21
==>DummyProduct-22
==>DummyProduct-25
==>DummyProduct-26
==>DummyProduct-29
==>DummyProduct-30
==>DummyProduct-31
==>DummyProduct-33
==>DummyProduct-35
==>DummyProduct-41
==>DummyProduct-42
==>DummyProduct-46
==>DummyProduct-50
==>DummyProduct-53
==>DummyProduct-54
==>DummyProduct-57
==>DummyProduct-61
==>DummyProduct-62
==>DummyProduct-63
==>DummyProduct-66
==>DummyProduct-7
==>DummyProduct-70
==>DummyProduct-72
==>DummyProduct-77
==>DummyProduct-8
==>DummyProduct-81
==>DummyProduct-84
==>DummyProduct-85
==>DummyProduct-86
==>DummyProduct-9
==>DummyProduct-95
==>DummyProduct-96
==>DummyProduct-97
==>DummyProduct-98

Daniel Kuppitz

unread,
Apr 29, 2016, 2:56:01 PM4/29/16
to gremli...@googlegroups.com
Your query only excludes the product of the current traverser's path. You should aggregate all products bought by the user and use the aggregation side-effect to exclude those products.
See http://sql2gremlin.com/#_recommendation for a classic recommendation solution.

Cheers,
Daniel


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/9ff6053f-7ec4-4e27-8a5b-dbcc46105c26%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Marko Rodriguez

unread,
Apr 29, 2016, 3:07:58 PM4/29/16
to gremli...@googlegroups.com

Jean-Yves Lalanne

unread,
Apr 29, 2016, 5:11:55 PM4/29/16
to Gremlin-users
Thanks,

Looks like this solved my problem:
:> g.V().has('db_id','321').out('bought').aggregate('exclude').in('bought with').where(without('exclude')).values("db_id").groupCount().next().sort{-it.value}[0..2]

I don't understand why the exclusion with 'as' didn't work. It worked for:
:> g.V().has('db_id','321').as('exclude').out('bought').in('bought').where(neq('exclude')).values("db_id").groupCount().next().sort{-it.value}[0..2]

So 'as' is limited to a single vertex if I want to use it to filter results?

JY

Daniel Kuppitz

unread,
Apr 29, 2016, 5:29:54 PM4/29/16
to gremli...@googlegroups.com
So 'as' is limited to a single vertex if I want to use it to filter results?

Yes, your exclude label only refers to a single vertex (or rather element), namely the one of the current path.

Btw. instead of leaving the traversal with .next().sort{-it.value}[0..2] you can do this:

.order(local).by(values, decr).limit(local, 3)

Cheers,
Daniel


Reply all
Reply to author
Forward
0 new messages