官术网_书友最值得收藏!

Introduction to modeling nodes and relationships

We looked at the data models in Figure 2.2. Without delving into technical details, we can say that "A person can fly from New York to London on carrier X". With this statement, we can start exploring possibilities in which this data can be modelled. A preliminary approach would be to mark cities as nodes and flights as relationships, as shown in the following figure:

Figure 2.3: A preliminary model with flights and cities—the property graph

This might seem to be a fair model, however, there are a few problems with the approach. Relationships, in graphs, are used to model how the entities' nodes are related to each other. As discussed earlier, flights are one of the two core entities in our model. Flights don't relate cities to each other, instead, they are a means to get from one city to another. Modeling flights as relationships, can work out in presenting a flight plan, but if we have to allow flight bookings in the future, then we need to change flights to nodes. In general, it's always a good practice to model any entity in the domain as a node. Figure 2.4 shows us an alternative approach to model flights and cities.

Note

Modeling entities as relationships must be avoided. Relationships in Neo4j can't have other relationships linked to them. Relationships should depict semantically relevant connections between two entities, not entities themselves.

Figure 2.4: A model for flights and cities

The preceding data model has :Flight and :City as labels for the nodes for flights and cities, respectively. :HAS_FLIGHT and :FLYING_TO are relationships that link a flight to its origin city and destination city, respectively.

When we represent a part of a graph that contains all possible nodes and relationships, we lay down a specification for how data is connected and structured. Thus, we can describe graphs using specification by example.

This graph seems to be a good starting point for us to begin feeding the data into Neo4j.

Note

Before we move forward, it would be a good idea to install Neo4j so that we can work on the Cypher queries. Neo4j can be downloaded from http://neo4j.com/download/.

Once installed, we will use the neo4j-shell, a console tool for Neo4j to create nodes and relationships. Further in this book, we will make use of the Neo4j Browser tool, when we start dealing with traversal and exploring our graph. Before trying out any of the queries, ensure that the Neo4j server is started.

主站蜘蛛池模板: 常德市| 巧家县| 田东县| 靖宇县| 阳山县| 南华县| 中西区| 邵武市| 上饶县| 龙口市| 康平县| 鸡西市| 寻甸| 灵台县| 定西市| 西畴县| 铜鼓县| 双辽市| 库尔勒市| 黑山县| 河北省| 瓮安县| 龙门县| 东台市| 深水埗区| 景德镇市| 辽宁省| 高唐县| 乌苏市| 嘉禾县| 额济纳旗| 永新县| 宜君县| 皋兰县| 韶山市| 皋兰县| 延庆县| 鸡西市| 徐闻县| 师宗县| 平武县|