Cypher multiple optional relationships and multiple paths

1,557 views
Skip to first unread message

Joe Smith

unread,
Jun 14, 2012, 10:25:59 AM6/14/12
to ne...@googlegroups.com
I am using Neo4j 1.7. I have this cypher query and it gives me error:

start from=node(1), to=node(2)
match
   path1=from-[?:hasGroup]->()-[?:blocks]->to,
   path2=from-[?:hasGroup]->()-[?:blocksCity]->()<-[?:lives]-to,
   from-[:hasRole]->role
where
   path1 is null and path2 is null
return role

The error was: You pattern has at least one path between two bound elements, and these patterns are undefined for the time being.

at org.neo4j.cypher.internal.pipes.matching.PatternGraph$$anonfun$getDoubleOptionals$$anonfun$apply$2.apply(PatternGraph.scala:90)

The query itself should be self explanatory. Is there a different way of doing this?

Thanks,
Joe

Andres Taylor

unread,
Jun 14, 2012, 11:07:43 AM6/14/12
to ne...@googlegroups.com
Hi Joe,

Your problem path is path2 - in Cypher, a single path can't have more than two optional relationships. I think that marking the first and the last as optional should be enough. 

I'll make the error message clearer!

Andrés

Joe Smith

unread,
Jun 14, 2012, 12:08:52 PM6/14/12
to ne...@googlegroups.com
Hi Andres,

Marking the first and last one worked. Thanks a lot. I agree, a clearer error message would help a lot.

Joe

Joe Smith

unread,
Jun 14, 2012, 4:36:02 PM6/14/12
to ne...@googlegroups.com
For some reason, this query is not stable. In my JUnit tests, sometime it fails, sometime is succeeds in terms of the number of rows it returns. I am suspecting there is some threading issue or something really interesting happening.

Here is the business case that I need to address:

Return all the roles of another person. I can only see these roles if:

1. That person does not have a group

or

2. That person has a group but the group does not block me

or

3. That person has a group, which block 0 to many cities, non of which I lives in.



That person can have 0 or 1 such group.

I would really appreciate if someone could provide me with a cypher query on this.

Thanks,
Joe

Joe Smith

unread,
Jun 15, 2012, 12:21:54 AM6/15/12
to ne...@googlegroups.com
I posted the business case as a separate thread. Thanks.

Andres Taylor

unread,
Jun 15, 2012, 1:51:13 AM6/15/12
to ne...@googlegroups.com
Hi again Joe,

Your mail about the optional bug made me look into this piece of code, and I haven't stopped digging since.

I have found (and fixed) a number of problems around what I call double-optional paths - that is a path in a pattern that connects two bound nodes over exactly two optional relationships. The canonical example is: a-[?]->X<-[?]-b, with a and b being bound nodes. Just when you sent your email about tripple-optional paths, I was looking into this type of patterns - two bound nodes connected with more than one double-optional path. That's exactly what your query ends up being - path1 and path2 are both double-optional paths, and they both connect the same two bound nodes - from and to.

This work is still not done entirely, but I'm confident that the 1.8 release will handle these types of patterns much better.

Having said that - this type of query shouldn't be used in 1.8 Cypher. The relationship predicates are now enough to do it, so your query would look like:

start from=node(1), to=node(2)
match from-[:hasRole]->role
where not(from-[:hasGroup]->()-[:blocks]->to) and 
      not(from-[:hasGroup]->()-[:blocksCity]->()<-[:lives]-to)
return role

Which I think is easier to understand, and allows Cypher to use much easier (and faster) pattern matching.

When this work is done, we'll see if it gets backported to 1.7 or not.

Thanks for your patience,

Andrés

Andres Taylor

unread,
Jun 15, 2012, 1:53:21 AM6/15/12
to ne...@googlegroups.com
On Fri, Jun 15, 2012 at 7:51 AM, Andres Taylor <andres...@neotechnology.com> wrote:

When this work is done, we'll see if it gets backported to 1.7 or not.

Just to be clear - I'm talking about the bug fixes around double optional paths, not about back porting pattern predicates to 1.7.

Andrés 

Joe Smith

unread,
Jun 15, 2012, 9:11:59 AM6/15/12
to ne...@googlegroups.com
Hi Andres,

Thank you very much for detailed explanations. It would be great that the fixes around double optional paths to be back ported to 1.7. I am looking forward to it.

Thanks again for the great work on this product!

Joe

Peter Neubauer

unread,
Jul 16, 2012, 6:13:48 AM7/16/12
to ne...@googlegroups.com
Julien,
this is probably a bug that is closed in 1.8. Could you try with that
and see if the error disappears?

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

Wanna learn something new? Come to @graphconnect.


On Mon, Jul 16, 2012 at 12:00 PM, Julien Guepin <julien...@gmail.com> wrote:
> Hi,
>
> I got the same error than Joe (Your pattern has at least one path between
> two bound elements, and these patterns are undefined for the time being.)
> when I performed the same type of query to find nodes that are not
> connected.
> I read this thread and changed my query following your advices. My query is
> now like that:
>
> START entity=node({id}) , n180=node(180)
> MATCH entity-[:SUBJECT]->cat
> WHERE not(entity-[:SUBJECT]->n180) and
> not(entity-[:SUBJECT]->()-[:BROADER]->n180)
> RETURN cat.uri, id(cat), entity.uri
>
> Now, I get "SyntaxException: Unclosed parenthesis" on the WHERE clause when
> I run this query. I don't see unclosed parenthesis, so I don't understand
> why this is not working...
> I am using neo4j 1.7.2. Could you help me?
>
> Thanks,
> Julien

Julien Guepin

unread,
Jul 16, 2012, 8:01:49 AM7/16/12
to ne...@googlegroups.com
Indeed, the bug is fixed in 1.8.
Is there an ETA for 1.8 in a stable version? I would like to use this query in production.
Thank you,
Julien.

Peter Neubauer

unread,
Jul 16, 2012, 8:11:57 AM7/16/12
to ne...@googlegroups.com
Julien,
the GA is expected to be in a few weeks, 1.8.M06 was hopefully the
last milestone, and we are mainly doing QA, not new features in the
time between. Would that be ok?

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

Wanna learn something new? Come to @graphconnect.


Julien Guepin

unread,
Jul 16, 2012, 8:31:38 AM7/16/12
to ne...@googlegroups.com
That seems really great! Is there a page with all 1.8 new features? Cypher seems a lot more powerful.
Thanks! 

Julien.

Peter Neubauer

unread,
Jul 16, 2012, 8:33:25 AM7/16/12
to ne...@googlegroups.com
Well,
the high level features are as always in the respective changelogs,
see e.g. https://github.com/neo4j/community/blob/master/cypher/CHANGES.txt
for Cypher and so on :)

Cheers,

/peter neubauer

G: neubauer.peter
S: peter.neubauer
P: +46 704 106975
L: http://www.linkedin.com/in/neubauer
T: @peterneubauer

Wanna learn something new? Come to @graphconnect.


Reply all
Reply to author
Forward
0 new messages