简体   繁体   English

Neo4j - 按照发布日期的顺序获取 Post 节点列表

[英]Neo4j - Getting a list of Post nodes in order of date posted

I've recently started sketching up a personal project which will involve a social network side.我最近开始草拟一个涉及社交网络方面的个人项目。 I have some professional experience with Neo4j and while it feels like a perfect match there is one query that concerns me.我对 Neo4j 有一些专业经验,虽然感觉就像是完美匹配,但有一个问题让我担心。

Imagine a general social network: users follow each other, users post posts, users can see the posts written by the users they're following.想象一个普通的社交网络:用户互相关注,用户发布帖子,用户可以看到他们关注的用户写的帖子。 This is cleanly expressed in Neo4j through :User and :Post labelled nodes, connected through :posted and :follows relationships.这在 Neo4j 中通过:User:Post标记节点清晰表达,通过:posted:follows关系连接。

So I could get the posts by users I follow using a query like:因此,我可以使用以下查询获取我关注的用户的帖子:

MATCH (:User {user_id: 1})-[:follows]->(:User)-[:posted]->(p:Post)
RETURN p

This is pretty clean and simple.这是非常干净和简单的。 My concern is that realistically I will want to get the most recent 10 posts, and then the 10 posts after that, and so on.我担心的是,实际上我希望获得最近的 10 个帖子,然后是之后的 10 个帖子,依此类推。

So I created an index on a created_at field in :Post nodes and added an ORDER BY p.created_at DESC clause to the query.因此,我在:Post节点中的created_at字段上创建了一个索引,并向查询添加了ORDER BY p.created_at DESC子句。 I thought this would allow me to sort them efficiently however running an EXPLAIN on this query shows that ORDER BY clauses do not, for the most part, use indexes to speed up this process.我认为这可以让我有效地对它们进行排序,但是在这个查询上运行EXPLAIN表明ORDER BY子句在大多数情况下不会使用索引来加速这个过程。 As such I'm unsure if there's a way to get these efficiently when the result set becomes significantly large.因此,当结果集变得非常大时,我不确定是否有办法有效地获得这些。

This may be inexperience or just approaching this data model incorrectly.这可能是缺乏经验或只是错误地接近此数据模型。 Can I get some input on this kind of problem?我可以就此类问题获得一些意见吗? Should I model my data differently?我应该以不同的方式为我的数据建模吗? Is my query/index wrong?我的查询/索引错误吗? Is there something I'm missing?有什么我想念的吗? How would you do this?你会怎么做?

EDIT 1: Example query for something like what I meant:编辑 1:类似于我的意思的示例查询:

MATCH (:User {user_id: 1})-[:follows]->(:User)-[:posted]->(p:Post)
RETURN p
ORDER BY p.created_at DESC
LIMIT 10

Also I've been thinking that using a range (in a WHERE clause) is a possibility to limit the result set size but still unsure of whether there's a better way?此外,我一直认为使用范围(在WHERE子句中)可以限制结果集大小,但仍然不确定是否有更好的方法?

EDIT 2 (Solution): This was the final query that made the Cypher planner use the index for this problem:编辑 2(解决方案):这是使 Cypher 规划器使用索引解决此问题的最终查询:

MATCH (:User {user_id: 1})-[:follows]->(:User)-[:posted]->(p:Post)
USING INDEX p:Post(created_at)
WHERE p.created_at < datetime()
RETURN p
ORDER BY p.created_at DESC
LIMIT 10

Neo4j 3.5 introduced some support for using indexes to perform ORDER BY operations , with some restrictions. Neo4j 3.5 引入了对使用索引执行ORDER BY操作的一些支持,但有一些限制。

But, currently (in neo4j 3.5.3), even when the usage of an index is supported for ORDER BY , the Cypher planner does not seem to automatically use it for that purpose.但是,目前(在 neo4j 3.5.3 中),即使ORDER BY支持使用索引,Cypher 规划器似乎也不会自动为此目的使用它。 In my experimentation with version 3.5.3, I found that if you do not use the index in a WHERE clause then the planner will not use the index at all.在我对 3.5.3 版本的实验中,我发现如果您不在WHERE子句中使用索引,那么规划器根本不会使用该索引。

So, as a simple workaround, you can just add a trivial WHERE clause using the index.因此,作为一个简单的解决方法,您可以使用索引添加一个简单的WHERE子句。 For example, here is a modified version of your query that will "trick" the planner into using the index for ORDER BY :例如,这是您的查询的修改版本,它将“欺骗”规划器以使用ORDER BY的索引:

MATCH (:User {user_id: 1})-[:follows]->(:User)-[:posted]->(p:Post)
WHERE p.created_at > 0
RETURN p
ORDER BY p.created_at DESC
LIMIT 10

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

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