如何在Neo4j中使用类型层次结构? [英] How to work with type hierarchies in Neo4j?

查看:188
本文介绍了如何在Neo4j中使用类型层次结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法来模拟Neo4j中的类型层次结构?例如,如果我想建立一个汽车类层次结构,我可能会有一个基本类型的汽车,然后有扩展的类,如SportCar等。



我希望能够创建SportCar的实例,但运行查询以获取所有Car。这可能吗?如果是这样的技术是什么?



我想要做的是创建一个标签层次结构 - 但我不认为这在neo4j中得到了支持。

解决方案

Paul,

首先,我建议您阅读图形数据库一本免费的O'Reilly电子书,可在这个链接中找到。



作为一个答案的快速刺入,有许多方法可以做这种事情,最好的选择取决于你正在试图解决的问题。一种方法是构建一个汽车节点,然后使用类型化的关系将您的SportCar节点与Car节点相关联,如

 <$ (m:Car)
MATCH(m:CAR)WITH m CREATE(n:SportCar) - [:IS_A] - >(m)

并创建其他类型的汽车,并将它们与Car节点相关联。 您可以然后通过

  MATCH(m:Car)< -  [:IS_A]  - (n)RETURN n $ b找到所有车辆$ b  

您也可以在每个节点上放置Car和SportCar(以及LuxoCar等)标签。这只是众多方法中的两种。



恩典与和平,

Jim

Is there some way to model type hierarchies in Neo4j? If for example I want to build a class hierarchy of cars, I might have a base type of "Car" and then have sub classes that extend that, like "SportCar", etc.

I'd like to be able to create instances of "SportCar", but run a query to get all "Car"s. Is this possible? If so what is the technique?

I think what I'm trying to do is create a "label hierarchy" - but I just don't think that's supported in neo4j.

解决方案

Paul,

First, I'd recommend that you read "Graph Databases", a free O'Reilly e-book available at this link.

As a quick stab at an answer, there are numerous ways to do this sort of thing, and the best choice depends on the problem you are trying to solve. One way would be to construct a "Car" node, then relate your "SportCar" nodes to the "Car" node with a typed relationship like

CREATE (m:Car)
MATCH (m:CAR) WITH m CREATE (n:SportCar)-[:IS_A]->(m)

and create other types of cars, also relating them to the Car node.

You can then find all cars via

MATCH (m:Car)<-[:IS_A]-(n) RETURN n

You can also just put Car and SportCar (and LuxoCar, etc) labels on each node. And that's just two of many approaches.

Grace and peace,

Jim

这篇关于如何在Neo4j中使用类型层次结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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