Neo4j使用数组属性查询节点无结果,求排查操作错误原因
问题:Neo4j中使用数组属性查询节点无结果
我在Neo4j的Product节点上有一个存储为字符串数组的属性keyterms。通过以下语句可查询到该节点的keyterms值:
MATCH (n:Product) WHERE n.id_product = 8 RETURN n.keyterms
查询结果为:
["financial tool", "personal finance"]
但当我尝试使用keyterms字段查询该节点时,执行以下语句却无记录返回:
MATCH (n:Product) WHERE ["financial tool"] in n.keyterms RETURN n.keyterms
返回结果:no records
请问我哪里操作出错了?
解答
你的问题出在查询条件的判断逻辑上:
n.keyterms是字符串数组,内部元素均为单个字符串(比如"financial tool")- 你写的
["financial tool"] in n.keyterms是在判断「一个包含单个字符串的数组」是否是n.keyterms的元素,但n.keyterms里根本没有数组类型的元素,因此无法匹配到结果。
正确的写法是直接判断单个字符串是否存在于目标数组中:
MATCH (n:Product) WHERE "financial tool" IN n.keyterms RETURN n.keyterms
如果需要判断多个值的匹配情况,还可以结合ALL或ANY函数,比如判断两个字符串是否同时存在于数组中:
MATCH (n:Product) WHERE ALL(term IN ["financial tool", "personal finance"] WHERE term IN n.keyterms) RETURN n.keyterms
内容的提问来源于stack exchange,提问作者Tom
相关产品推荐
相关产品推荐

