返回neo4j中最常见的路径 [英] Returning most common paths in neo4j

查看:397
本文介绍了返回neo4j中最常见的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的结构:

I have a very simple structure:

U1-:VISITS->P1-:VISITS->P2-:VISITS->P3-VISITS->P4...

每个VISITS关系的等级评定为1到10.我对以U1-:VISITS-> P1-:VISITS-> P2开头的关系感兴趣,其中第一等级为< 2,第二等级大于5.每个页面节点都有页面链接作为属性.之后,我对用户访问的接下来的2页感兴趣.这应该返回路径列表.我对用户采用的最频繁的路径感兴趣,并对它们出现的次数进行排序.我的查询未返回正确的路径计数.我做错了什么?

Each VISITS relationship has a rating on a scale 1 to 10. I am interested in relationships that start with U1-:VISITS->P1-:VISITS->P2 where 1st rating is <2 and 2nd rating is greater than 5. Each page node has page link as a property. After that, I am interested in the next 2 pages the user visits. This should return a list of paths. I am interested in the most frequent paths the user takes and ordering them number of times they appear. My query doesn't return the correct count of paths. What did I do wrong?

MATCH p=(a)-[r:VISITS]-(b)-[t:VISITS]-(c)-[q*1..2]-(page:Page) WHERE r.rating<2 AND t.rating>5 RETURN EXTRACT (n IN nodes(p)|n.page_id) ,count(p) ORDER BY count(p) DESC;

例如:

U1->P1->P2
U2->P1->P2
U3->P3->P4

应该有

P1,P2  2
P3,P4  1

作为最终结果.

这是我的解决方案,可以为上述问题(u-> p1-> p2)返回正确的结果:

This is my solution that returns the correct result for the above problem (u->p1->p2):

MATCH p=(a)-[r:VISITS]-(b:Page)-[t:VISITS]-(page:Page) WHERE r.rating<2 AND t.rating>5 WITH EXTRACT (n IN nodes(p)|n.page_id) AS my_pages,t AS rels RETURN DISTINCT(my_pages) AS pages,count(DISTINCT rels) as count;

我现在需要扩展它以包括更长的路径.

I need to extend it now to include longer paths.

推荐答案

我注意到的第一件事(它只是一个抄写错误,是在关系上没有方向.而且,您不是使用标签,因此您可以在路径的任何子部分进行匹配.这样做可能会更好:

The first thing that I notice (and it make have just been a transcription error, is that there are no directions on the relationships. Also, you're not using labels, so you could be matching on any sub-section of the path. This might work better:

MATCH p=(a:User)-[r:VISITS]->(b:Page)-[t:VISITS]->(c:Page)-[q*1..2]->(page:Page)
  WHERE r.rating<2 AND t.rating>5
  RETURN EXTRACT (n IN nodes(p)|n.page_id) ,count(p)
  ORDER BY count(p) DESC;

如果没有标签,也可以添加WHERE NOT(()-[:VISITS]->(a))

If you don't have labels, you could maybe also add WHERE NOT(()-[:VISITS]->(a))

这篇关于返回neo4j中最常见的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆