如果关系不存在,则返回节点 [英] Return node if relationship is not present

查看:22
本文介绍了如果关系不存在,则返回节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用密码创建一个查询,该查询将查找"厨师可能缺少的成分,我的图表设置如下:

I'm trying to create a query using cypher that will "Find" missing ingredients that a chef might have, My graph is set up like so:

(ingredient_value)-[:is_part_of]->(ingredient)

(ingredient) 将具有 name="dye colours" 的键/值.(ingredient_value) 可以有一个键/值 value="red" 并且是"(ingredient, name="dye colours") 的一部分".

(ingredient) would have a key/value of name="dye colors". (ingredient_value) could have a key/value of value="red" and "is part of" the (ingredient, name="dye colors").

(chef)-[:has_value]->(ingredient_value)<-[:requires_value]-(recipe)-[:requires_ingredient]->(ingredient)

我正在使用此查询来获取配方所需的所有 ingredients,但不是它们的实际值,但我希望只返回那些 ingredients厨师没有,而不是每个食谱所需的所有成分.我试过

I'm using this query to get all the ingredients, but not their actual values, that a recipe requires, but I would like the return only the ingredients that the chef does not have, instead of all the ingredients each recipe requires. I tried

(chef)-[:has_value]->(ingredient_value)<-[:requires_value]-(recipe)-[:requires_ingredient]->(ingredient)<-[:has_ingredient*0..0]-chef

但这没有任何回报.

这是可以通过 cypher/neo4j 完成的事情还是最好通过返回所有成分并自己分类来处理的事情?

Is this something that can be accomplished by cypher/neo4j or is this something that is best handled by returning all ingredients and sorted through them myself?

奖励:还有一种方法可以使用密码将厨师拥有的所有值与食谱所需的所有值进行匹配.到目前为止,我只返回了由 chef-[:has_value]->ingredient_value<-[:requires_value]-recipe 返回的所有部分匹配,并自己汇总结果.

Bonus: Also is there a way to use cypher to match all values that a chef has to all values that a recipe requires. So far I've only returned all partial matches that are returned by a chef-[:has_value]->ingredient_value<-[:requires_value]-recipe and aggregating the results myself.

推荐答案

2013 年 1 月 10 日更新:

在 Neo4j 2.0 参考中遇到了这个:

Came across this in the Neo4j 2.0 reference:

尽量不要使用可选的关系.最重要的是,

不要这样使用它们:

MATCH a-[r?:LOVES]->() WHERE r IS NULL 确保它们不存在.

MATCH a-[r?:LOVES]->() WHERE r IS NULL where you just make sure that they don’t exist.

改为这样做:

MATCH a WHERE NOT (a)-[:LOVES]->()

<小时>

使用密码检查关系是否不存在:


Using cypher for checking if relationship doesn't exist:

...
MATCH source-[r?:someType]-target
WHERE r is null
RETURN source

?标记使关系可选.

在 neo4j 2 中:

In neo4j 2 do:

...
OPTIONAL MATCH source-[r:someType]-target
WHERE r is null
RETURN source

现在您可以检查不存在(空)的关系.

Now you can check for non-existing (null) relationship.

这篇关于如果关系不存在,则返回节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆