0

I want to import some sentences, splitting the content in words. For dong that I split the sentence and use FOREACH and CASE to check the size of the array, and based on that run a different kind of statements

The following Query is a reduced version of the one I'm using, but the error I get is the same

WITH SPLIT("my house", " ") AS SWords
WITH SIZE(SWords) as msize, SWords
FOREACH(myize IN CASE WHEN msize = 1 THEN
    [
        MERGE (w1:Word{name: SWords[0]})
    ]
    ELSE
    [
        MERGE (w2:Word{name: SWords[1]})
    ]
    END
    )

return that error:

Invalid input ')': expected whitespace or a relationship pattern (line 5, column 44 (offset: 176))
"            MERGE (w1:Word{name: SWords[0]})"
                                            ^
  1. Is the use of FOREACH + CASE correct?
  2. The MERGE statement is correct, why it fails?
  3. If I remove the values for word and use just MERGE (w1:Word) I get an error about the closing parentheses for the FOREACH

Code:

WITH SPLIT("my house", " ") AS SWords
WITH SIZE(SWords) as msize, SWords
FOREACH(myize IN CASE WHEN msize = 1 THEN
    [
        MERGE (w1:Word)
    ]
    ELSE
    [
        MERGE (w2:Word)
    ]
    END
    )

Error:

Invalid input ')': expected whitespace, comment, '.', node labels, '[', "=~", IN, STARTS, ENDS, CONTAINS, IS, '^', '*', '/', '%', '+', '-', '=', "<>", "!=", '<', '>', "<=", ">=", AND, XOR, OR or '|' (line 12, column 9 (offset: 332))
"        )"
         ^

Any help will be welcome!!! Thanks

davidd00
  • 23
  • 1
  • 8
  • 1
    May this help : http://stackoverflow.com/questions/27576427/cypher-neo4j-case-expression-with-merge ? – Raphaël Althaus Jun 24 '16 at 09:45
  • Thanks @Raphaël I already read that post and the blog entry related. I used it to create my code – davidd00 Jun 24 '16 at 10:11
  • But the accepted answer states that you need two foreach, and you try to do all in one. Or did I miss something ? – Raphaël Althaus Jun 24 '16 at 10:14
  • THANK YOU VERY MUCH @Raphaël, that's exactly the key, which I missed reading the post That's solved the issue. I'll add the solution to the Original post – davidd00 Jun 24 '16 at 10:25

2 Answers2

0

The solution (as explained in this question ) is to use two FOREACH, one for each condition:

WITH SPLIT("My house", " ") AS SWords
WITH SIZE(SWords) as msize, SWords
FOREACH(myize IN CASE WHEN msize = 1 THEN [1] ELSE [] END |
        MERGE (w2:Word{name: SWords[0]})
 )

FOREACH(myize IN CASE WHEN msize > 1 THEN [1] ELSE [] END |
        MERGE (w1:Word{name: SWords[1]})
 )

This will go for the second FOREACH and the node "house" will be added to the db

Anyway, I don't understand why the code I was using didn't work, at the end the CASE-WHEN-ELSE statement doesn't work as expected

Community
  • 1
  • 1
davidd00
  • 23
  • 1
  • 8
0

In Cypher, a collection ([...]) must contain values, not Cypher read/write statements (like MERGE).

Also, you only need a single FOREACH clause to get the proper results:

WITH SPLIT("My house", " ") AS SWords
WITH SIZE(SWords) as msize, SWords
FOREACH(name IN
  CASE
    WHEN msize = 1 THEN [SWords[0]]
    WHEN msize > 1 THEN [SWords[1]] 
    ELSE []
  END |
  MERGE (:Word{name: name})
);
cybersam
  • 63,203
  • 6
  • 53
  • 76