简体   繁体   English

Neo4j / Cypher查询语法反馈

[英]Neo4j / Cypher query syntax feedback

I'm developing a kind of reddit service to learn Neo4j. 我正在开发一种reddit服务来学习Neo4j。 Everything works fine, I just want to get some feedback on the Cypher query to get the most recent news stories, the author and number of comments, likes and dislikes. 一切正常,我只想获得一些关于Cypher查询的反馈,以获取最新的新闻报道,作者和评论数量,喜欢和不喜欢。

I'm using Neo4j 2.0. 我正在使用Neo4j 2.0。

MATCH comments = (n:news)-[:COMMENT]-(o)
MATCH likes = (n:news)-[:LIKES]-(p)
MATCH dislikes = (n:news)-[:DISLIKES]-(q)
MATCH (n:news)-[:POSTED_BY]-(r)
WITH n, r, count(comments) AS num_comments, count(likes) AS num_likes, count(dislikes) AS num_dislikes
ORDER BY n.post_date
LIMIT 20
RETURN *

o , p , q , r are all nodes with the label user . opqr是具有标签user所有节点。 Should the label be added to the query to speed it up? 是否应将标签添加到查询中以加快速度?

Is there anything else you see that I could optimize? 你还有什么其他的东西可以优化吗?

I think you're going to want to get rid of the multiple matches. 我想你会想要摆脱多场比赛。 Cypher will filter on each one, filtering through one another, rather than getting all the information. Cypher将对每个进行过滤,相互过滤,而不是获取所有信息。

I would also avoid the paths like comments, and rather do the count on the nodes you are saving. 我也会避免使用注释之类的路径,而是对要保存的节点进行计数。 When you do MATCH xyz = (a)-[:COMMENT]-(b) then xyz is a path, which contains the source, relationship and destination node. 当你做MATCH xyz =(a) - [:COMMENT] - (b)然后xyz是一个路径,它包含源,关系和目标节点。

MATCH (news:news)-[:COMMENT]-(comment),(news:news)-[:LIKES]-(like),(news:news)-[:DISLIKES]-(dislike),(news:news)-[:POSTED_BY]-(posted_by) 
WHERE news.post_date > 0 
WITH news, posted_by, count(comment) AS num_comments, count(like) AS num_likes, count(dislike) AS num_dislikes 
ORDER BY news.post_date 
LIMIT 20 
RETURN *

I would do something like this. 我会做这样的事情。

MATCH (n:news)-[:POSTED_BY]->(r)
WHERE n.post_date > {recent_start_time}
RETURN n, r, 
        length((n)<-[:COMMENT]-()) AS num_comments,
        length((n)<-[:LIKES]-()) AS num_likes,
        length((n)<-[:DISLIKES]-()) AS num_dislikes,
ORDER BY n.post_date DESC
LIMIT 20

To speed it up and have not neo search over all your posts, I would probably index the post-date field (assuming it doesn't contain time information). 为了加快速度并且没有对你的所有帖子进行新搜索,我可能会对post-date字段进行索引(假设它不包含时间信息)。 And then send this query in for today, yesterday etc. until you have your 20 posts. 然后在今天,昨天等发送此查询,直到您有20个帖子。

MATCH (n:news {post_date: {day}})-[:POSTED_BY]->(r)
RETURN n, r, 
        length((n)<-[:COMMENT]-()) AS num_comments,
        length((n)<-[:LIKES]-()) AS num_likes,
        length((n)<-[:DISLIKES]-()) AS num_dislikes,
ORDER BY n.post_date DESC
LIMIT 20

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

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