3

In my domain a node can have several relationships of the same type to other entities. Each relationship have several properties and I'd like to retrieve the nodes that are connected by at least 2 relationships that present a given property.

EG: A relationship between nodes have a property year. How do I find the nodes that have at least two outgoing relationships with the year set to 2012?

Why Chypher query so far looks like this (syntax error)

START x = node(*)
MATCH x-[r:RELATIONSHIP_TYPE]->y
WITH COUNT(r.year == 2012) AS years
WHERE HAS(r.year) AND years > 1
RETURN x;

I tried also nesting queries but I believe that it's not allowed in Cypher. The closest thing is the following but I do not know how to get rid of the nodes with value 1:

START n = node(*)
MATCH n-[r:RELATIONSHIP_TYPE]->c
WHERE HAS(r.year) AND r.year == 2012
RETURN n, COUNT(r) AS counter
ORDER BY counter DESC
Marsellus Wallace
  • 17,991
  • 25
  • 90
  • 154

1 Answers1

5

Try this query

START n = node(*)
MATCH n-[r:RELATIONSHIP_TYPE]->c
WHERE HAS(r.year) AND r.year=2012
WITH n, COUNT(r) AS rc
WHERE rc > 1
RETURN n, rc
Werner Kvalem Vesterås
  • 10,226
  • 5
  • 43
  • 50
  • i'm curious whether the `r.year` part is powered by index? either way, isn't this faster? `START r = relationship:yearIndex('year:2012') MATCH n-[r:RELATIONSHIP_TYPE]->c WITH n, COUNT(r) AS rc WHERE rc > 1 RETURN n, rc` – ulkas Dec 07 '12 at 13:22
  • @ulkas it isn't, but they don't mention an index in the question. You should definitely avoid node(*) and use indexes in start lookups when possible. :) – Eve Freeman Jan 14 '13 at 06:20