Neo4j:求和关系属性,其中节点属性等于值A和值B(交集) [英] Neo4j: Sum relationship properties where node properties equal Value A and Value B (intersection)

查看:585
本文介绍了Neo4j:求和关系属性,其中节点属性等于值A和值B(交集)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我的问题是:在具有相关属性等于值A和值B的相关节点的情况下,如何对关系属性求和?

Basically my question is: how do I sum relationship properties where there is a related nodes that have properties equal to Value A and Value B?

例如:

我有一个简单的数据库,它具有以下关系:

I have a simple DB has the following relationship:

(site)-[:HAS_MEMBER]->(user)-[:POSTED]->(status)-[:TAGGED_WITH]->(tag)

在[:TAGGED_WITH]上,我有一个名为"TimeSpent"的属性.通过使用以下查询,我可以轻松地总结在特定日期和用户上花费的所有时间:

On [:TAGGED_WITH] I have a property called "TimeSpent". I can easily SUM up all the time spent for a particular day and user by using the following query:

MATCH (user)-[:POSTED]->(updates)-[r:TAGGED_WITH]->(tags)
WHERE user.name = "Josh Barker" AND updates.date = 20141120
RETURN tags.name, SUM(r.TimeSpent) as totalTimeSpent;

这给我返回了一个不错的表格,其中包含标签以及在每个标签上花费的相关时间. (即会议4.5).但是,如果我想进行一些高级搜索并说显示ProjectA的所有会议"(即#Meeting #ProjectA),就会出现问题.基本上,我正在寻找一个查询,我可以获取单个状态带有两个标签的所有关系(并且仅当同时具有两个标签时).然后,我可以求和该数字以求得我在#ProjectA中花了多少次会议的计数.

This returns to me a nice table with tags and associated time spent on each. (i.e. #Meeting 4.5). However, the question arises if I want to do some advanced searches and say "Show me all the meetings for ProjectA" (i.e. #Meeting #ProjectA). Basically, I am looking for a query that I can get all of the relationships where a single status has BOTH tags (and only if it has both). Then I can SUM that number up to get a count for how many meetings I spent in #ProjectA.

我该怎么做?

推荐答案

要创建一个通用解决方案,您可能需要一个或多个标签,可以使用类似的方法,将标签数组作为参数(并使用数组的长度,而不是硬编码的2.

To create a generic solution where you may want one or more tags you could use something like this, passing in the array of tags as a parameter (and using the length of the array instead of the hard coded 2.

MATCH (user)-[:POSTED]->(update)-[r:TAGGED_WITH]->(tag)
WHERE user.name = "Josh Barker" AND updates.date = 20141120 AND tag.name IN ['Meeting', 'ProjectA']
WITH update, SUM(r.TimeSpent) AS totalTimeSpent, COLLECT(tag) AS tags
WHERE LENGTH(tags) = 2
RETURN update, totalTtimeSpent

只要为tag.name建立索引,这应该很快.

As long as tag.name is indexed, this should be fast.

编辑-删除用户约束

MATCH (update)-[r:TAGGED_WITH]->(tag)
WHERE tag.name IN ['Meeting', 'ProjectA']
WITH update, SUM(r.TimeSpent) AS totalTimeSpent, COLLECT(tag) AS tags
WHERE LENGTH(tags) = 2
RETURN update, totalTtimeSpent

这篇关于Neo4j:求和关系属性,其中节点属性等于值A和值B(交集)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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