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

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

问题描述

我有一个形式的图表:

(products:Product)-[:in_stock { updated: timestamp }]->(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天全站免登陆