Neo4j:匹配多个标签(2 个或更多) [英] Neo4j: Match multiple labels (2 or more)

查看:46
本文介绍了Neo4j:匹配多个标签(2 个或更多)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个搜索,我想从 2 个标签(或条件)开始遍历.例如,我需要找出所有标签为男"或女"且其属性名称为 =~ '.ail.' 的节点.

I would like to do a search, and I would like to start traversing from 2 labels (OR condition). For example, I need to find out all the nodes which have labels either 'Male' or 'Female' and whose property, name =~ '.ail.'.

推荐答案

你可以把这个条件放在WHERE子句中:

You can put this condition in the WHERE clause:

MATCH (n)
WHERE n:Male OR n:Female
RETURN n

编辑

正如@tbaum 指出的那样,这将执行 AllNodesScan.我在标签相当新的时候写了答案,并希望查询规划器最终为每个标签使用 NodeByLabelScan 来实现它,就像它在单标签情况下一样

As @tbaum points out this performs an AllNodesScan. I wrote the answer when labels were fairly new and expected the query planner to eventually implement it with a NodeByLabelScan for each label, as it does for the single label case

MATCH (n)
WHERE n:Male
RETURN n

我仍然认为这是查询的合理表达,并且期望查询规划器使用标签扫描来实现它是合理的,但是从 Neo4j 2.2.3 开始,查询仍然使用 AllNodesScan 和标签过滤器.因此,这是一个更详细的替代方案.由于标签析取表示一个集合联合,并且这个联合可以用不同的方式表达,我们可以用查询规划器实现的方式来表达它,而不扫描所有节点,而是以每个标签的 NodeByLabelScan 开头.

I still think this is a reasonable expression of the query and that it is reasonable to expect the query planner to implement it with label scans, but as of Neo4j 2.2.3 the query is still implemented with an AllNodesScan and a label filter. Here is therefore a more verbose alternative. Since the label disjunction signifies a set union and this union can be expressed in different ways, we can express it in a way that the query planner implements without scanning all nodes, and instead starts with a NodeByLabelScan per label.

MATCH (n:Male)
WHERE n.name =~ '.ail.'
RETURN n
UNION MATCH (n:Female)
WHERE n.name =~ '.ail.'
RETURN n

这意味着对每个标签表达一次查询并使用显式UNION 将它们连接起来.这并非不合理,至少对于较少数量的标签,但我不清楚为什么查询规划器不能从更简单的查询中推断出相同的实现,所以我打开了一个 github 问题 这里.

This means expressing the query once for each label and joining them with an explicit UNION. This is not unreasonable, at least for smaller number of labels, but it's not clear to me why the query planners shouldn't be able to infer the same implementation from the simpler query so I have opened a github issue here.

这篇关于Neo4j:匹配多个标签(2 个或更多)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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