语法在Neo4j中执行以下cypher语句时显示错误

时间:2016-05-02 05:23:38

标签: neo4j cypher

创建陈述

CREATE (n:STATION {name: 'A'})

CREATE (n:STATION {name: 'B'})

CREATE (n:STATION {name: 'C'})

MATCH (a:STATION {name: 'A'}),
      (b:STATION {name: 'B'})
MERGE (a)-[:ROUTE {route: 1}]->(b)
MERGE (a)-[:ROUTE {route: 2}]->(b)
MERGE (a)-[:ROUTE {route: 3}]->(b)
MERGE (b)-[:ROUTE {route: 1}]->(a)
MERGE (b)-[:ROUTE {route: 2}]->(a)
MERGE (b)-[:ROUTE {route: 3}]->(a)

MATCH (a:STATION {name: 'B'}),
      (b:STATION {name: 'C'})
MERGE (a)-[:ROUTE {route: 1}]->(b)
MERGE (a)-[:ROUTE {route: 2}]->(b)
MERGE (a)-[:ROUTE {route: 3}]->(b)
MERGE (b)-[:ROUTE {route: 1}]->(a)
MERGE (b)-[:ROUTE {route: 2}]->(a)
MERGE (b)-[:ROUTE {route: 3}]->(a)

以下语句在执行时显示语法错误:

MATCH p=((a:STATION {name: 'A'})-[*]->(b:STATION {name: 'C'}))
WHERE NONE (n IN nodes(p) 
        WHERE size(filter(x IN nodes(p) 
                         WHERE n = x))> 1)
WITH reduce(acc=[], (route in rels(p)):
CASE
    WHEN length(acc) > 0 and last(acc) = route.route THEN acc 
    ELSE acc + route.route  
END) as reducedRoutes
RETURN reducedRoutes, length(reducedRoutes) as len ORDER BY len

它显示的错误是:

Invalid input 'W': expected whitespace, comment, NodeLabel, '.', node
labels, '[', "=~", IN, STARTS, ENDS, CONTAINS, IS, '^', '*', '/', '%',
'+', '-', '=', "<>", "!=", '<', '>', "<=", ">=", AND, XOR, OR, ',' or
')' (line 7, column 9 (offset: 247)) 
"    WHEN length(acc) > 0 and last(acc) = route.name THEN acc "
     ^

我在Neo4j 3.0.0中执行cypher语句,是不是因为旧版Neo4j和这个版本之间的语法不同?

谢谢!

1 个答案:

答案 0 :(得分:0)

行。我通过将语句更改为

来解决问题
MATCH p=((a:STATION {name: 'A'})-[*]->(b:STATION {name: 'C'}))
WHERE NONE (n IN nodes(p) 
            WHERE size(filter(x IN nodes(p) 
                              WHERE n = x))> 1) 
WITH reduce(acc=[], r in rels(p) | 
  CASE
    WHEN length(acc) > 0 and last(acc) = r.route THEN acc 
    ELSE acc + r.route
  END) as reducedRoutes
RETURN reducedRoutes, length(reducedRoutes) as len
ORDER BY len ASC