Cypher:return 如果使用全文搜索不匹配则为字符串

Cypher: return a string if no match using fulltext search

我有一个 Cypher 查询

CALL db.index.fulltext.queryNodes('myIndex', 'coding')
YIELD node
RETURN node

如果索引实际与任何现有节点匹配,return 是 coding 节点,如果没有匹配,return 为 null。

如果没有匹配项,我不想return空值,我想return一个字符串或一条消息,如No match found

我在想我可以结合apoc.when()喜欢

CALL db.index.fulltext.queryNodes('myIndex', 'coding')
YIELD node

WITH node
CALL apoc.when(node is not null, 'RETURN node', 'RETURN "No match found"', {node:node})

但是我得到一个错误

Query cannot conclude with CALL (must be RETURN or an update clause) (line 5, column 1 (offset: 77))
"CALL apoc.when(node is not null, 'RETURN node', 'RETURN "No match found"', {node:node})"

我试过添加

YIELD value
RETURN value

在语句末尾,但它不会 return 没有匹配项时的消息,就像未使用 apoc.when() 一样工作。

您可以简单地使用合并:

CALL db.index.fulltext.queryNodes('myIndex', 'coding')
YIELD node
RETURN coalesce(node, "No match found") as result

问题是在 YIELD returns null 之后,之后的所有操作都不会计算(类似于 MATCH 与 OPTIONAL MATCH 的工作方式)。

我能够按照 Tomaz 在他的回答中建议的那样创建一个集合、用案例展开并合并来克服这个问题。

CALL db.index.fulltext.queryNodes('myIndex', 'coding')
YIELD node
WITH collect(node) as nodes
UNWIND (CASE nodes WHEN [] then [null] else nodes end) as n
RETURN coalesce(n, "No match found")