基于关系相似度的推荐

Recommendation based on similartiy of relationship

我想创建一个基于当前购物车内容与其他(已经下的)订单相比较的推荐引擎,并考虑有一个像这样的图表:

(u:User)-[po:PUT_ORDER]->(o:Order)-[cp:CONTAINS_PRODUCT]->(p:Product)

我是 Neo4j/Cypher 的新手,如果有人能就以下问题为我指出正确的方向,我将不胜感激:

  1. 是否可以根据我当前的购物车获取其他已下订单的最相似产品(至少是我当前购物车中的产品)并推荐增量产品,即减去产品我们有共同点并推荐其他的...可以吗?

  2. 假设没有其他订单包含我购物车中的所有产品。有没有一种方法可以根据他们有多少共同的产品降序对结果中的订单进行分组,并为每个订单获取 "delta products"(假设前 10 个订单)?

问题不是很清楚,但我会尽力提供答案:

Is it possible based on my current shopping cart fetch other already put orders that have most similar products (at least the products I have in current shopping cart) and recommend the delta products, i.e. subtract products we have in common and recommend the other ones... Is that possible?

是的,这是可能的。减法可能非常棘手(因为 Cypher 目前缺少 MINUS 子句),但您可以使用列表理解来计算它:

WITH [1, 2, 3, 4] AS xs, [3, 4, 5] AS ys
RETURN [x IN xs WHERE NOT x IN ys | x]

此外,如果您沿着关系进行减法,您可能需要使用否定模式条件 (WHERE NOT <pattern>):

MATCH (p:Product) ...
WHERE NOT (p)-[:SOME_REL_TYPE]->(:SomeNodeLabel)
RETURN p

Let's say there are no other orders that have all the products I have in my shopping cart. Is there a way to group already put orders in result based on how many products they have in common descending and fetch the "delta products" for every order (let's say top 10 orders)?

要计算常见产品的数量,请使用针对相同 p 节点的 MATCH 子句。

MATCH
  (u1:User {id: $currentUser})-[:PUT_ORDER]->(:Order)-[:CONTAINS_PRODUCT]->(p:Product),
  (u2:User)-[:PUT_ORDER]->(:Order)-[:CONTAINS_PRODUCT]->(p)
WITH u1, u2, count(p) AS commonProducts
ORDER BY commonProducts DESC
LIMIT 10
// continue calculation with u1, u2