简体   繁体   English

返回与另一个节点关系最密切的节点

[英]Return nodes with most relationship to another node

First of all i'm new to neo4j and cypher, so if this is trivial i'm sorry :) 首先,我是neo4j和cypher的新手,所以如果这很简单,对不起:)

I'm facing a problem I cannot solve, but I will try to explain what I have to do. 我面临一个我无法解决的问题,但是我将尽力解释我必须做的事情。

I have a graph containing some customers, that have made some orders containing products. 我有一个包含一些客户的图,这些客户已经下了一些包含产品的订单。 This means that the graph is setup like this: 这意味着图形的设置如下:

(c:Customer)-[r:PURCHASED]->(o:Order)-[f:ORDERS]->(p:Product)

Here is a representation of the graph I have to work on (This is only an example and not the actual graph): 这是我必须处理的图形的表示(这只是一个示例,而不是实际的图形): 在此处输入图片说明

I have to find customer C, and all the products he has ordered (p1-p6), which is not a problem. 我必须找到客户C及其订购的所有产品(p1-p6),这不是问题。 What I need to do is find the customer who have ordered most of the same products, and output that customer and the products. 我需要做的是找到已订购大多数相同产品的客户,然后输出该客户和产品。 This means that customer c2, has ordered 3 of the same products as customer c, compared to c3 who has only ordered 2 of the same. 这意味着与仅订购2个相同商品的c3相比,顾客c2已订购了3个与顾客c相同的产品。 I want c2 then. 那我要c2。

Here is what I have so far 这是我到目前为止的

MATCH (c:Customer {customerID: "C"})-[r:PURCHASED]->(o:Order)-
[f:ORDERS]->(p:Product),
(p)<-[f1:ORDERS]-(o1:Order)<-[r1:PURCHASED]-(c1:Customer)
WITH c1 AS c1, count(p) AS count, p AS p
WHERE count > 4
RETURN c1,p

This does not really give me what I want. 这并没有真正给我我想要的东西。 Since when I put count > 4, I get 1 customer and 1 product. 自从我输入count> 4以来,我得到了1个客户和1个产品。 This product has 3 f1:ORDERS relationships, to the customer. 该产品与客户具有3 f1:ORDERS关系。 I wanted a customer (or more customers) that had bought more than 4 of the same products as the customer with ID "C". 我想要一个(或多个)与ID为“ C”的客户购买了4种以上相同产品的客户。

Obviously i'm doing something wrong, and I can't seem to figure out how to get the correct result. 显然我做错了,我似乎无法弄清楚如何获得正确的结果。 I hope someone is able to help me 我希望有人能够帮助我

This should work for you: 这应该为您工作:

MATCH
  (c:Customer { customerID: "C" })-[r:PURCHASED]->(o:Order)-[f:ORDERS]->(p:Product),
  (p)<-[f1:ORDERS]-(o1:Order)<-[r1:PURCHASED]-(c1:Customer)
WITH c1, COLLECT(DISTINCT p) AS ps
RETURN c1, ps
ORDER BY LENGTH(ps) DESC 
LIMIT 1;

This query aggregates, for each c1 customer, the distinct products that that customer bought that are in common with the products bought by c . 该查询针对每个c1客户汇总该客户购买的与c购买产品相同的独特产品。 It then sorts those customers (in descending order) by how many products are in common; 然后,按共有多少个产品(按降序排列)对这些客户进行排序; and returns the top customer, along his collection of shared products. 并返回顶级客户以及他的共享产品集合。

Here is a console that shows the results with your sample data. 这是一个控制台 ,显示带有示例数据的结果。

You can use ORDER BY and LIMIT to do what you want: 您可以使用ORDER BYLIMIT来做您想做的事情:

MATCH (c:Customer {customerID: "C"})-[r:PURCHASED]->(o:Order)-
[f:ORDERS]->(p:Product),
(p)<-[f1:ORDERS]-(o1:Order)<-[r1:PURCHASED]-(c1:Customer)
WITH c1, count(p) AS count, p
ORDER BY count
RETURN c1, p LIMIT 1

This will order the result by count property, and return only the first one, so the one with the most products related. 这将按count属性对结果进行排序,并且仅返回第一个,因此与大多数产品相关的那个。

If this is not what you need (I did not understand your question very well) say it n the comments, I'll update my answer. 如果这不是您所需要的(我不太清楚您的问题),请在评论中说出来,我将更新我的答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM