Neo4j - 浏览器中的Cypher查询与驱动程序之间的区别? (WITH COLLECT,apoc等)

时间:2017-05-22 03:45:16

标签: javascript angular neo4j cypher

我在Neo4j浏览器和我的JavaScript(Angular)代码中运行相同的查询,但得到两个不同的结果。到目前为止,我对基本查询没有任何问题。我导入了neo4j-web驱动程序,因此可以在Angular中使用: import * as neo4j_driver from 'neo4j-driver/lib/browser/neo4j-web.min.js';

我需要有分层数据,因此我按照示例here

进行操作

以下查询适用于Neo4j浏览器 MATCH p=(n:Category)-[:subcategory*]->(m) WHERE NOT (m)-[:subcategory]->() WITH COLLECT(p) as ps CALL apoc.convert.toTree(ps) yield value RETURN value;

但是,在我的JS代码中执行完全相同的查询时:      const result = this.session.run( 'MATCH p=(n:Category)-[:subcategory*]->(m)' + 'WHERE NOT (m)-[:subcategory]->()' + 'WITH COLLECT(p) as ps' + 'CALL apoc.convert.toTree(ps) yield value' + 'RETURN value;' );

我在浏览器控制台中收到以下错误: Error: Invalid input 'a': expected whitespace, comment, ',', ORDER, SKIP, LIMIT, WHERE, LOAD CSV, START, MATCH, UNWIND, MERGE, CREATE, SET, DELETE, REMOVE, FOREACH, WITH, CALL, RETURN, UNION, ';' or end of input (line 1, column 100 (offset: 99)) "MATCH p=(n:Category)-[:subcategory*]->(m)WHERE NOT (m)-[:subcategory]->()WITH COLLECT(p) as psCALL apoc.convert.toTree(ps) yield valueRETURN value;" ^ at new n (neo4j-web.min.js:17) at t (neo4j-web.min.js:17) at l.value (neo4j-web.min.js:18) at l._dechunker.onmessage (neo4j-web.min.js:18) at l.value (neo4j-web.min.js:18) at l.value [as _state] (neo4j-web.min.js:18) at l.value (neo4j-web.min.js:18) at l.u._ch.onmessage (neo4j-web.min.js:18) at WebSocket._ws.onmessage (neo4j-web.min.js:18) at WebSocket.wrapFn [as __zone_symbol___onmessage] (zone.js:1032)

起初我认为Apoc可能是一个问题,因为它指的是插入符号;但是,我检查了其他Apoc函数,它们工作正常,然后我完全删除了Apoc行,而是运行:         const result = this.session.run( 'MATCH p=(n:Category)-[:subcategory*]->(m)' + 'WHERE NOT (m)-[:subcategory]->()' + 'WITH COLLECT(p) as ps' + 'RETURN ps;' );

如果从代码运行但在Neo4j浏览器中运行,也会导致错误。控制台出错: Error: Invalid input 'p': expected whitespace, comment, ',', ORDER, SKIP, LIMIT, WHERE, LOAD CSV, START, MATCH, UNWIND, MERGE, CREATE, SET, DELETE, REMOVE, FOREACH, WITH, CALL, RETURN, UNION, ';' or end of input (line 1, column 102 (offset: 101)) "MATCH p=(n:Category)-[:subcategory*]->(m)WHERE NOT (m)-[:subcategory]->()WITH COLLECT(p) as psRETURN ps;" ^ at new n (neo4j-web.min.js:17) at t (neo4j-web.min.js:17) at l.value (neo4j-web.min.js:18) at l._dechunker.onmessage (neo4j-web.min.js:18) at l.value (neo4j-web.min.js:18) at l.value [as _state] (neo4j-web.min.js:18) at l.value (neo4j-web.min.js:18) at l.u._ch.onmessage (neo4j-web.min.js:18) at WebSocket._ws.onmessage (neo4j-web.min.js:18) at WebSocket.wrapFn [as __zone_symbol___onmessage] (zone.js:1032)

有什么想法在这里发生了什么?我整天都在桌子上打我的头,我尝试的任何东西似乎都没有用。我是Neo4j的新手,可能会犯一些初学者错误,但我真的很想知道为什么相同的查询有不同的行为。

1 个答案:

答案 0 :(得分:3)

您应该在子句之间添加空格(如空格或换行符),否则查询字符串并不总是按照您的意图进行解析。

试试这个(注意前4个字符串末尾的额外空格):

const result = this.session.run(
  'MATCH p=(n:Category)-[:subcategory*]->(m) ' +
  'WHERE NOT (m)-[:subcategory]->() ' +
  'WITH COLLECT(p) as ps ' +
  'CALL apoc.convert.toTree(ps) yield value ' +
  'RETURN value;'
);

详细信息

您的原始查询产生了以下查询字符串:

MATCH p=(n:Category)-[:subcategory*]->(m)WHERE NOT (m)-[:subcategory]->()WITH COLLECT(p) as psCALL apoc.convert.toTree(ps) yield valueRETURN value;

Cypher解析器被psCALLvalueRETURN绊倒了。