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

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

问题描述

我正在尝试使用密码创建查询,以查找"厨师可能缺少的食材,我的图形设置如下:

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 colors". (ingredient_value)的键/值可能为value ="red",并且是(ingredient, name="dye colors")的一部分.

(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

但是这什么也没返回.

but this returned nothing.

这是可以通过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.

推荐答案

更新01/10/2013:

在Neo4j 2.0 参考中对此进行了解:

Came across this in the Neo4j 2.0 reference:

尽量不要使用可选关系. 首先,

Try not to use optional relationships. Above all,

不要这样使用它们:

don’t use them like this:

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.

而是这样做:

Instead do this like so:

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

?标记使关系成为可选关系.

The ? mark makes the relationship optional.

在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天全站免登陆