如何在neo4j中通过时间戳过滤边缘? [英] How to filter edges by time stamp in neo4j?

查看:276
本文介绍了如何在neo4j中通过时间戳过滤边缘?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图形,其形式为:

I have a graph of the form:

(products:Product)-[:in_stock {更新:时间戳}]->(stock_items:StockItem {数量:q})-[:stored_at]->(locations:Location)

(products:Product)-[:in_stock { updated: timestamp }]->(stock_items:StockItem { quantity: q })-[:stored_at]->(locations:Location)

这显然更多,但是您明白了要点.通常会添加stock_item节点和in_stock边缘(位置和产品的数量不是很多),因此对于每个这些图,都会有很多这样的关系.我想按时间戳(自1970年1月1日以来的毫秒数)进行搜索和过滤,以仅提取最新的(最大值)并因此返回当前数量.

Obviously more to it that that but you get the gist. The stock_item nodes and in_stock edges are added often (locations and products not so much) so for each of these diagrams there'll be many of these relationships. I want to search and filter by the timestamp (milliseconds since Jan1 1970) to only pull the most recent (max value) and return therefore the current quantity.

我不知道如何进行过滤.有任何想法吗?

I can't figure how to do that filtering. Any ideas?

推荐答案

请考虑以下图形数据模型.这些天是在链接列表中连接的,但是它们包含时间戳记.如果要收集某个范围之间的Stats节点,则必须先在那些日节点上进行选择,然后才能选择紫色的Stats节点.从那里,我可以指定那些紫色的节点必须连接到黄色的Group节点,该节点连接到我指定的Location.

Consider the graph data model below. The days are connected in a linked list but they contain timestamps. If I want to collect the Stats nodes between a range, I must first select on those day nodes and then I can select the Stats nodes that are in purple. From there I can specify that those purple nodes must be connected to the Group node in yellow that is connected to theLocation that I specify.

现在,如果我将此模式转换为Cypher,则会得到以下信息:

Now if I translate this pattern into Cypher, I get the following:

MATCH (d:Day)
WHERE d.timestamp > 123456789 AND d.timestamp < 234567891
MATCH (topic:Topic), (location:Location { city: "San Francisco" })
WHERE topic.name in ["NoSQL"]
WITH topic, location, day
MATCH (topic)<-[:HAS_TOPIC]-(group:Group)-[:LOCATED_IN]->(location) 
WITH DISTINCT group, day
MATCH (group)-[:HAS_MEMBERS]->(stats:Stats)-[:ON_DAY]->(day)
WITH DISTINCT (day.month + "/" + day.day + "/" + day.year) as day, 
              group.name as group, 
              stats.count as members, 
              day.timestamp as timestamp
ORDER BY timestamp
RETURN day, group, members

如果重构模型以将in_stock关系转换为带有时间戳的节点,并将该节点建模为链接列表,则可以通过指定模式来选择最新的模型:

If you refactored your model to turn the in_stock relationship into a node with a timestamp, and model that node as a linked list, then you could select the most recent by specifying the pattern:

MATCH (product:Product { sku: 1234 })-[:HAS_UPDATE]->(update:InStock) 
WHERE NOT (update)-[:NEXT]->()
WITH update
MATCH (update)-[:STOCK_ITEMS]->(stockItems:StockItem),
      (stockItems)<-[:STORED_AT]-(location:Location)
RETURN location.name, stockItems.quantity

这是执行此操作最有效的方法.管理链接列表中的指针,使您既可以查询某个范围(在时间戳之间),也可以查询N个最新项.

This is the most performant way to do this. To manage pointers in a linked list that allow you to both query on a range (between timestamps) and to also query the N most recent items.

这篇关于如何在neo4j中通过时间戳过滤边缘?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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