Re: [Neo4j] How to query nodes with multiple relationships?

116 views
Skip to first unread message

Michael Hunger

unread,
Aug 9, 2012, 5:16:52 PM8/9/12
to ne...@googlegroups.com
I don't really understand your question?

Does the second query not work for you?

Michael

Am 09.08.2012 um 21:48 schrieb fli.rpx:

> I am trying to write a query to return nodes with two relationships.
>
> One way to do it: start a=node(1405) match a-[:A*1..3]->m, a-[:B*1..3]->m return distinct m.name;
>
> This will work, but takes much a lot more time than a query like this: start a=node(1405) match a-[:A|B*1..3] return distinct m;
>
> I think maybe neo4j does two traversals in the first query.
yes it does and their results span up a cross product which is filtered down by distinct again.
>
> Is it possible to do a query to get nodes with both relationships like the second query?

fli.rpx

unread,
Aug 10, 2012, 12:59:48 PM8/10/12
to ne...@googlegroups.com
Hi Michael,

Thanks for your reply.

There are differences between the two queries. The first query, "start a=node(1405) match a-[:A*1..3]->m, a-[:B*1..3]->m return distinct m.name", has AND relationship. Its result set will be the intersection of two traversals.

The second query, "start a=node(1405) match a-[:A|B*1..3] return distinct m; ", is OR relationship.

These two queries have performance difference. The first one will take two traversals then compare. The second one only has one traversal.

I am wondering if I can write the first query in a way of the second query. Something like this?:

start a=node(1405) match a-[:A&B*1..3] return distinct m;


Thanks,

Fudong

Michael Hunger

unread,
Aug 10, 2012, 1:13:09 PM8/10/12
to ne...@googlegroups.com
you can write the first one like this:

start a=node(1405) match a-[:A*1..3]->m
where a-[:B*1..3]->m
return distinct m.name

Please remember that distinct is an expensive operation (in term of non-lazyness and memory consumption) as it has to fetch all data and merge the results in memory.

You may at least try to run it without distinct to see the difference.

Michael

fudong li

unread,
Aug 10, 2012, 7:11:57 PM8/10/12
to ne...@googlegroups.com
I got to use distinct, otherwise it returns 10K instead of 20 records.

Your query is not working well for me:
start a=node(1405) match a-[:A*1..3]->m
where a-[:B*1..3]->m
return distinct m.name

 I guess there are two traversals. The first one return 10K records, and the second also returns 10K records, then the two set compares.
It took forever to finish on my machine.

Following query works really well. 

start a=node(747) match a<-[:A*1..3]-m with a, collect(distinct m) as blocked match (a)<-[:B*1..3]-(m) where m in blocked return m.name;

I guess the distinct to remove duplicates made it very efficient. I got the query from you in another thread.

Why does the "where m not in blocked" not work for me? It gave errors below:

SyntaxException: `--' expected but `n' found

Think we should have better error message here? Help us by sending this query to cyp...@neo4j.org.

Thank you, the Neo4j Team.

"start a=node(747) match a<-[:share_family*1..1]-m with a, collect(distinct m) as blocked match (a)<-[:share_plaintiff*1..1]-(m) where m not in blocked return m.name"

Thanks.

Fudong

Michael Hunger

unread,
Aug 10, 2012, 7:38:50 PM8/10/12
to ne...@googlegroups.com
syntax NOT(m in blocked)

"start a=node(747) match a<-[:share_family*1..1]-m with a, collect(distinct m) as blocked match (a)<-[:share_plaintiff*1..1]-(m) where m not in blocked return m.name"

if your paths are only of length ONE please leave off the *1..1

Michael
Reply all
Reply to author
Forward
0 new messages