I am having trouble with MATCH by index and I am hoping you can help. Related discussion can be found at this post: Cypher Linked LIst: how to unshift and replace by index
First I run the following unshift query three times in order to populate the linked list with relationships and nodes
MATCH (p {id: '123A'})
OPTIONAL MATCH (p)-[r:Foo]->(c)
WITH p, r, COLLECT(c) AS cs
MERGE (cNew {
id:'456B' // and '789C' and 'WXYZ'
})
CREATE (p)-[rNew:Foo {
isFavorite:false,
isOpen:false,
currenttab:'all',
currentdate:2015-10-30T07:00:00.000Z
}]->(cNew)
FOREACH (x IN cs |
CREATE (cNew)-[:Foo {
isFavorite:r.isFavorite,
isOpen:r.isOpen,
currenttab:r.currenttab,
currentdate:r.currentdate
}]->(x)
DELETE r)
RETURN p, rNew, cNew
Afterwords I can fetch the output with this query so that I can see that I have three elements in my linkedlist array under the relationship label Foo
MATCH ()-[r:`language-arts_ALLRESOURCES`]->() RETURN r
isFavorite false
isOpen false
currenttab all
currentdate 2015-10-30T07:00:00.000Z
isFavorite false
isOpen false
currenttab all
currentdate 2015-10-30T07:00:00.000Z
isFavorite false
isOpen false
currenttab all
currentdate 2015-10-30T07:00:00.000Z
I then try to query the first index with this cypher query
MATCH (p { id:"123A" })
MATCH (p)-[:Foo*1]->()-[r:Foo]->(c)
RETURN c
But I am coming up with the error below:
Don't know how to compare that. Left: [:Foo[8258]
{isFavorite:false,isOpen:false,currenttab:"all",currentdate:"2015-10-
30T07:00:00.000Z"}] ($colon$colon); Right: :Foo[8260]
{isFavorite:false,isOpen:false,currenttab:"all",currentdate:"2015-10-
30T07:00:00.000Z"} (RelationshipProxy)
I tried fetching with this query:
MATCH (p { id:"123A" })
MATCH (p)-[:Foo*2]->(c)
RETURN c
and this is the output that comes back
id 789C // which is index 1
I also have some slight confusion as to whether index 1 should be fetched with the integer 0 or 1 or 2 but that confusion I am sure can be sorted out once I get passed this 'Don't know how to compare that.' error. Please help me with guidance as to the right way to fetch effectively by index and avoid the error I listed above.
I am very grateful for your help
** NOTE **
After reading cybersam's answer, I have run the following tests on the linked list structure mentioned above that contains three elements
This query returns the child at the index 0 position:
MATCH (p { id:'123A' })
MATCH (p)-[:Foo*0]->()-[r:Foo]->(c)
RETURN c
This query returns the child at the index 1 position:
MATCH (p { id:'123A' })
MATCH (p)-[:Foo]->()-[r:Foo]->(c)
RETURN c
This query returns the "Don't know how to compare that." error
MATCH (p { id:'123A' })
MATCH (p)-[:Foo*1]->()-[r:Foo]->(c)
RETURN c
This query returns the child at the index 2 position:
MATCH (p { id:'123A' })
MATCH (p)-[:Foo*2]->()-[r:Foo]->(c)
RETURN c
I am assuming this last one works for anything >= 2
Cypher seems to be confused by the (as far as I can tell) legal
[:Foo*1]
syntax. If you just used[:Foo]
instead, which logically equivalent, you would have avoided the error.But, actually, the way to get the index 0 child is:
Or, to get the 0 index child in a more generic way, this will work. This generic pattern should always work for any index (by replacing
0
with the desired index), but there seems to be a bug (as stated above) in Cypher when using1
:I have created neo4j issue 5799 for this.