cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Optional Match returns null when node is present

version 4.1.1

I have a scenario where OPTIONAL MATCH does not work. I have two labels I am comparing where I want all items in the first label and the items that exist in the second label.

match (b:OldComponent) 
optional MATCH (n:Component {name: b.name}) 
RETURN b.name, n.name order by b.name;

Returns:

╒═══════════════════════════════════╤════════╕
│"b.name"                           │"n.name"│
╞═══════════════════════════════════╪════════╡
│"#2S15 RED"                        │null    │
├───────────────────────────────────┼────────┤
│"#6FO <2%"                         │null    │
├───────────────────────────────────┼────────┤
│"115 LATEX, TOTE, LB"              │null    │

If I remove the OPTIONAL I get:

╒════════════════╤═══════════════╕
│"b.name"                           │"n.name"                           │
╞════════════════╪════════════════╡
│"#2S15 RED"                        │"#2S15 RED"                        │
├────────────────┼─────────────────┤
│"#6FO <2%"                         │"#6FO <2%"                         │

As you can see from the result without the OPTIONAL the data does exist.

I have rebuilt the constraints/indexes but still the same results.

I tried to create a sample set of data in another database but could not recreate the issue.

Any ideas?

Sorry for the formatting. Couldn't figure out how to get it corrected.

3 REPLIES 3

Isha
Node Clone

Hi @higerdm , in the 2nd line "optional MATCH (n:Component {name: b.name}) ", the value of the name property should be n.name as per your requirements. Then it should fetch correct results

b.name is correct. I'm wanting to lookup the Component with the same name as the OldComponent. The only difference between the statements is the "optional" keyword.

FIrst Statement
match (b:OldComponent)
optional MATCH (n:Component {name: b.name})
RETURN b.name, n.name order by b.name;

Second Statement
match (b:OldComponent)
MATCH (n:Component {name: b.name})
RETURN b.name, n.name order by b.name;

This looks buggy.

Can you try prefixing your first query with CYPHER runtime=SLOTTED and see if it works?

If it still isn't working, please run a PROFILE on both queries, and export and attach the plans here so we can take a look