计算具有特定属性的节点

时间:2016-08-19 13:45:47

标签: neo4j cypher

我正在研究描述立法共同赞助的数据集。我正试图找回一张表格,上面写着该法案的名称,共同发起该法案的立法者人数,以及共和党人和民主党人的共同赞助人数。我觉得这应该很简单,但我不断收到语法错误。这是我到目前为止所做的:

[[NSURLCache sharedURLCache] removeAllCachedResponses];
for (UIView *view in self.view.subviews)
    {
      if ([view isKindOfClass:[UIWebView class]])
      {
        [[NSURLCache sharedURLCache] removeAllCachedResponses];
        UIWebView webView = (UIWebView )view;
        NSURL *websiteUrl = [NSURL URLWithString:urlstr];
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:websiteUrl];
        [webView loadRequest:urlRequest];
        [webView reload];
      }
    }

然而,在该表中,此查询产生的共和党和民主党赞助商的数量以及总赞助商的数量都是相同的。显然,众议员和民主党赞助商的数量应该等于总数。

此查询的正确语法是什么?

3 个答案:

答案 0 :(得分:1)

使用过滤器:

MATCH  (b:Bill{Year:"2016"})
      -[r:COAUTHORED_BY|COSPONSORED_BY|SPONSORED_BY]-
       (c:Legislators) 
WHERE b.name CONTAINS "HB" OR b.name CONTAINS "SB"
WITH b, collect(distinct c) as Legislators
RETURN b.name, 
       b.Short_description, 
       SIZE(Legislators) AS TOTAL, 
       SIZE(FILTER(c in Legislators WHERE c.Party = "Republican")) as Republican,
       SIZE(FILTER(c in Legislators WHERE c.Party = "Democratic")) as Democratic
ORDER BY TOTAL desc

答案 1 :(得分:0)

这是一个图模型问题,您不应该按其属性计算节点,如果某些节点可以具有相同的属性并且您想要在此属性中计数,则需要创建一个中间节点来设置该方:

SpannableStringBuilder facebook = new SpannableStringBuilder(getString(R.string.fa_icon_facebook) + " Facebook");
        facebook.setSpan(new CustomTypefaceSpan("", fontAwesome, this), 0, 1, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        facebook.setSpan(new CustomTypefaceSpan("", champagneRegularFont, this), 1, 10, Spanned.SPAN_EXCLUSIVE_INCLUSIVE);

然后在中间节点和聚会之间创建关系:

(b:Bill)-[:SPONSORED_AUTHORED]->(i:Intermediate)-[:TARGET]->(c:Legislators)

中间节点代表您在关系中实际拥有的数据,但它允许您在操作和派对之间创建关系,使计数更容易,更快。

请记住,这只是一个例子,在不知道上下文的情况下我不知道中级真实标签及其属性应该是什么,它只是这个概念的演示。

我使用此回答了一个问题,请随意查看(这是一个真实的例子,也许更容易理解):Neo4j can I make relations between relations?

答案 2 :(得分:0)

假设立法者只能是共和党人或民主党人(如果不是这样的话,我们需要作出一些调整):

MATCH (b:Bill{Year:"2016"})
WHERE b.name CONTAINS "HB" OR b.name CONTAINS "SB"
WITH b
OPTIONAL MATCH (b)-[:COAUTHORED_BY|COSPONSORED_BY|SPONSORED_BY]-(rep:Legislators)
WHERE rep.Party = "Republican"
OPTIONAL MATCH (b)-[:COAUTHORED_BY|COSPONSORED_BY|SPONSORED_BY]-(dem:Legislators)
WHERE dem.Party = "Democratic"
WITH b, COUNT(DISTINCT rep) as reps, COUNT(DISTINCT dem) as dems
RETURN b.name, b.Short_description, reps + dems AS TOTAL, reps, dems
ORDER BY TOTAL desc