循环进入密码,UNWIND或FOREACH(Neo4j) [英] Loop in cypher, UNWIND or FOREACH (Neo4j)

查看:190
本文介绍了循环进入密码,UNWIND或FOREACH(Neo4j)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的neo4j数据库中有一个关系:

I have a relation in my neo4j database :

(r:RateableEntity)<-[t:TAG]-(h:HashTags)

现在,我想查询一个返回包含以下内容的列表的查询:

Now I want to have a query that returns a list that includes:

  1. hashtagName的列表及其在数据库中的频率,以hashtagCount的形式显示,并列出与该hashtag相关的项目. hashtagNamehashtagItems带有id标签.
  1. A list of hashtagName and their frequency in the database as hashtagCount and a list of items that related to this hashtags. hashtagName and hashtagItems have id label.

注意:我从输入参数中接收到hashtaghashtagItems的数字​​作为变量.

Note: I'm receiving the number of hashtag and hashtagItems from input parameter as variable.

这是我从密码查询中得到的结果:

And this is the result that I expected from my cypher query:

"hashtagList": [ 
{
  "hashtagName": "hashtagName1",
  "hashtagCount": number of times hashtag has been used in database,
  "hashtagItems": [ list of relevant items for hashtagName1 ]
},
{
  "hashtagName": "hashtagName2",
  "hashtagCount": number of times hashtag has been used in database,
  "hashtagItems": [ list of relevant items for hashtagName2 ]
},
...
]

我写了这个密码:

MATCH p = (r:RateableEntity)<-[t:TAG]-(h:HashTag)
UNWIND TAIL (NODES(p)) AS hash
WITH COUNT(hash) as Count, h, hash
ORDER BY hash LIMIT 3
WHERE h.tag in hash.tag
MATCH (r:RateableEntity)<-[:TAG]-(h:HashTag)
 RETURN DISTINCT h.tag, r.id, Count
 LIMIT 3

但它返回此结果:

h.tag       r.id                                  Count
"vanessa"   "cdd14968-404c-41e9-84d5-bf147030a023"  15
"vanessa"   "b7e74f38-44e4-4b7f-b2c4-8301023ffa9b"  15
"vanessa"   "2064d3e4-2995-4202-b178-bb2a6f230ab0"  15

非常感谢您的帮助.

推荐答案

需要记住的一些事情:

  1. 密码运算符针对每一行执行.

  1. Cypher operators execute for each row.

尽量不要将UNWIND视为循环结构.所有这些操作是对变量与列表元素的直角乘积进行笛卡尔乘积运算.

Try not to think of UNWIND as a looping structure. All this does is do a cartesian product of the variables on a row with the elements of a list.

因此,当您取消缠绕列表时,列表的每个元素都会有一行,以及该行已经存在的所有变量.然后,当发生对每行执行的后续操作(例如MATCH或WITH)时,它看起来像是一个循环结构,但实际上并非如此.

So when you UNWIND a list, you will have a row for each element of the list, along with all the variables that were already present for the row. Then when a subsequent operation happens (like a MATCH or a WITH) that executes for every row, so it seems like a looping structure, but it really isn't.

无论如何,这里不需要UNWIND.对于两个节点的匹配模式,tail(nodes(p))将只是一个仅包含最后一个节点的单元素列表.它没有更改行数(因为列表大小为1),因此这里无济于事.

In any case, UNWIND isn't needed here. For a two-node matched pattern, tail(nodes(p)) will just be a single-element list containing just the last node. It hasn't changed the number of rows (since the list size is 1), and won't help you here.

此查询应该更好地工作:

This query should work better:

MATCH (h:HashTag)
WITH h LIMIT 3 // best to limit early to avoid doing unnecessary work
WITH h, h.tag as hashtagName, size((h)-[:TAG]->()) as hashtagCount, [(h)-[:TAG]->(r:RateableEntity) | r.id] as hashtagItems
WITH h {hashtagName, hashtagCount, hashtagItems} as entry
RETURN collect(entry) as hashtagList

编辑

如果您想要按大小排列前3个主题标签,则可以使用以下修改后的查询:

If you want the top 3 hashtags by size, then you can use the modified query below:

MATCH (h:HashTag)
WITH h, size((h)-[:TAG]->()) as hashtagCount
ORDER BY hashtagCount DESC
LIMIT 3
WITH h, hashtagCount, h.tag as hashtagName, [(h)-[:TAG]->(r:RateableEntity) | r.id] as hashtagItems
WITH h {hashtagName, hashtagCount, hashtagItems} as entry
RETURN collect(entry) as hashtagList

这篇关于循环进入密码,UNWIND或FOREACH(Neo4j)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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