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

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.

主站蜘蛛池模板: 静宁县| 宣城市| 龙口市| 乐陵市| 库伦旗| 开江县| 唐山市| 潜山县| 乐清市| 乐东| 广饶县| 新竹县| 襄城县| 乌恰县| 忻城县| 太谷县| 巫山县| 杭锦后旗| 理塘县| 县级市| 盐山县| 余姚市| 乌鲁木齐县| 叙永县| 尖扎县| 承德市| 资溪县| 普安县| 绥德县| 辽阳县| 兰西县| 安吉县| 泽州县| 台江县| 长沙市| 栾川县| 汝阳县| 永仁县| 临桂县| 河南省| 象州县|