What's the Cypher equivalent to "find all nodes that have a property of name with value of X that don't have any ancestors with name = X"?

已查看 61 次
跳至第一个未读帖子

Behrang Saeedzadeh

未读,
2016年12月24日 04:40:202016/12/24
收件人 Neo4j
Let's pretend we have a tree like this in which each node is labeled with the name property:

ROOT
|
\---> X --> A --> Y --> A --> Z
|
|
\---> A --> A
|
|
\---> A

I want to return all the nodes named A that don't have an ancestor that is also named A. In other words, only the A nodes that are in bold and are underlined.

What Cypher query can return these nodes? 

Thanks in advance.

Benoît Simard

未读,
2016年12月24日 05:33:472016/12/24
收件人 ne...@googlegroups.com
Something like that ?

MATCH (n:A)
WHERE 
  size((:A)-->(n)) = 0 AND
 exists((:Root)-[*]->(n))
RETURN n



--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Behrang Saeedzadeh

未读,
2016年12月27日 05:52:082016/12/27
收件人 Neo4j
Hi Benoit,

A is the value of the name property for the nodes. So the query is more like this:

MATCH (n)
WHERE
 size ((m)-->(n)) = 0
 AND m.name = 'A'
 AND n.name = 'A'
RETURN n

However this won't work because I don't have m in the MATCH clause:

Variable `m` not defined (line 3, column 9 (offset: 24))
" size ((m)-->(n)) = 0"
         ^



On Saturday, December 24, 2016 at 9:33:47 PM UTC+11, Benoît Simard wrote:
Something like that ?

MATCH (n:A)
WHERE 
  size((:A)-->(n)) = 0 AND
 exists((:Root)-[*]->(n))
RETURN n


2016-12-24 10:40 GMT+01:00 Behrang Saeedzadeh <behr...@gmail.com>:
Let's pretend we have a tree like this in which each node is labeled with the name property:

ROOT
|
\---> X --> A --> Y --> A --> Z
|
|
\---> A --> A
|
|
\---> A

I want to return all the nodes named A that don't have an ancestor that is also named A. In other words, only the A nodes that are in bold and are underlined.

What Cypher query can return these nodes? 

Thanks in advance.

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.

Behrang Saeedzadeh

未读,
2016年12月27日 05:58:552016/12/27
收件人 Neo4j
This might work, but my dataset has about 2m nodes and it is very slow as it results a cartesian join:

MATCH (n), (p)
WHERE
 n.name = 'A' AND
 p.name = 'A' AND
 NOT (p)-->(n)
RETURN n

Tom Zeppenfeldt

未读,
2016年12月27日 06:45:252016/12/27
收件人 ne...@googlegroups.com
What about this approach ?


          WITH 'A' as name 
          MATCH p=((n:Root)-[:CHILD*]->(m {name:name})) 
          WHERE SINGLE(n in TAIL(nodes(p)) WHERE n.name=name) 
          RETURN m

which wil return only the first node with name=‘A’  , if any.

Behrang Saeedzadeh

未读,
2016年12月27日 09:07:072016/12/27
收件人 Neo4j
For some reason this is returning such nodes too:

 /ROOT/N1/A/N2/A

I will upload the database sometime later. I think that should make it easier to test the queries.

Max De Marzi Jr.

未读,
2016年12月28日 10:57:422016/12/28
收件人 Neo4j
I think Tom almost had it...either:

WITH 'A' as name 
          MATCH p=((n:Root)-[:CHILD*]->(m {name:name})) 
          WHERE SINGLE(n in nodes(p) WHERE n.name=name) 
          RETURN m

or maybe:

WITH 'A' as name 
          MATCH p=((n:Root)-[:CHILD*]->(m {name:name})) 
          WHERE NONE(n in TAIL(nodes(p)) WHERE n.name=name) 
          RETURN m

Behrang Saeedzadeh

未读,
2017年1月2日 05:58:152017/1/2
收件人 Neo4j
Thanks. The first query worked. I have attached a sample DB for those who are interested.
sample.db.zip
回复全部
回复作者
转发
0 个新帖子