Creating edge label schema
Creating edge label database schema.
Edge labels, as discussed in the data model, define the name of the edge label,
incoming and outgoing vertex labels connected by an edge, and associated property
keys for the edge. Property keys and vertex labels must be created prior to
using them in edge label creation. Edge label schema can be created with
create()
, and property keys or connections can be added to
existing schema with add()
. The cardinality of an edge label is
multiple cardinality by default, but single
cardinality can be assigned. Edge labels can be created with a specific Time-To-Live
(TTL) value, or prior existence of a edge label can be checked using
ifNotExists()
.
A key component of an edge label is the connection()
which
identifies the two types of vertices that are connected with an edge using the edge
label. The connection has directionality specified by the order, going from the
outgoing vertex label listed first to the incoming vertex label listed second.
Prerequisites
Procedure
-
Define a multiple cardinality edge label:
The name of the edgeLabel is ate, it is defined with multiple cardinality explicitly stated with
multiple()
:schema.edgeLabel('ate').multiple().create()
Since multiple cardinality is the default,multiple()
can be omitted:schema.edgeLabel('ate').create()
To check if an edge label already exists before creating it, useifNotExists()
:schema.edgeLabel('knows').multiple().create().ifNotExists().create()
-
Define a single cardinality edge label:
The name of the edgeLabel is has, it is defined with single cardinality:
Since single cardinality is not the default,schema.edgeLabel('has').single().create()
single()
must be included.
-
Properties can be defined in either the
create()
oradd()
statement:
orschema.edgeLabel('ate').multiple().create() schema.edgeLabel('ate').properties('mealDate').add()
schema.edgeLabel('ate').multiple().properties('mealDate').create()
Important: If cardinality (single
ormultiple
) orproperties()
are specified, those options must precede theconnection()
option in the schema statement.
-
Connections can be defined in either the
create()
oradd()
statement:
orschema.edgeLabel('ate').multiple().create() schema.edgeLabel('ate').connection('person', 'meal').add()
schema.edgeLabel('ate').multiple().connection('person', 'meal').create()
-
Multiple connections between vertex labels can be created for the same edge
label:
These schema statements will allow edges to be inserted that use the edge label includedIn between recipes and meals, meals and books, and recipe and books.schema.edgeLabel('includedIn').connection('recipe','meal').add() schema.edgeLabel('includedIn').connection('meal','book').add() schema.edgeLabel('includedIn').connection('recipe','book').add()
-
If cardinality (
single
ormultiple
) orproperties()
are specified, those options must precede theconnection()
option in the schema statement.Multiple steps:schema.edgeLabel('includedIn').multiple().create() schema.edgeLabel('includedIn').properties('amount').add() schema.edgeLabel('includedIn').connection('recipe','meal').add() schema.edgeLabel('includedIn').connection('meal','book').add() schema.edgeLabel('includedIn').connection('recipe','book').add() schema.edgeLabel('includedIn').connection('ingredient','recipe').add()
Single step:schema.edgeLabel('includedIn').multiple().properties('amount').connection('recipe','meal').connection('recipe','book').connection('ingredient','recipe').create()
-
Edge labels can be defined with a Time-To-Live (TTL) value; once the specified
time is reached, the value is deleted from the graph. Define an edge label with
TTL:
The name of the edge label is createDate is defined with a TTL of 60 seconds with
ttl()
:schema.edgeLabel('createDate').ttl(60).create()
Example
// ********
// EDGE LABELS
// ********
// SYNTAX:
//schema.edgeLabel('edgeLabel').
// [ single() | multiple() ].
// [ connection( outVertex, inVertex) ].
// [ ttl ].
// [ properties(property[, property]) ].
// [ ifNotExists() ].
// [ create() | add() | describe() | exists() ]
// DEFAULT IS MULTIPLE CARDINALITY
// ********
schema.edgeLabel('ate').multiple().create()
schema.edgeLabel('ate').properties('mealDate').add()
schema.edgeLabel('ate').connection('person', 'meal').add()
schema.edgeLabel('knows').multiple().create()
schema.edgeLabel('knows').properties('since').add()
schema.edgeLabel('knows').connection('person','person').add()
schema.edgeLabel('includes').multiple().create()
schema.edgeLabel('includes').properties('numServ').add()
schema.edgeLabel('includes').connection('meal','meal_item').add()
schema.edgeLabel('includedIn').multiple().create()
schema.edgeLabel('includedIn').properties('amount').add()
schema.edgeLabel('includedIn').connection('recipe','meal').add()
schema.edgeLabel('includedIn').connection('meal','book').add()
schema.edgeLabel('includedIn').connection('recipe','book').add()
schema.edgeLabel('includedIn').connection('ingredient','recipe').add()
schema.edgeLabel('created').multiple().create()
schema.edgeLabel('created').properties('createDate').add()
schema.edgeLabel('created').connection('person', 'recipe').add()
schema.edgeLabel('reviewed').multiple().create()
schema.edgeLabel('reviewed').properties('time','year','stars','comment').add()
schema.edgeLabel('reviewed').connection('person','recipe').add()
schema.edgeLabel('authored').multiple().create()
schema.edgeLabel('authored').connection('person', 'book').add()
schema.edgeLabel('contains').multiple().ttl(60800).create()
schema.edgeLabel('contains').properties('expireDate').add()
schema.edgeLabel('contains').connection('fridgeSensor','ingredient').add()
schema.edgeLabel('isStockedWith').multiple().ttl(60800).create()
schema.edgeLabel('isStockedWith').properties('expireDate').add()
schema.edgeLabel('isStockedWith').connection('store','ingredient').add()
schema.edgeLabel('isLocatedAt').multiple().create()
schema.edgeLabel('isLocatedAt').connection('home','location').add()
schema.edgeLabel('isLocatedAt').connection('store','location').add()
schema.edgeLabel('isLocatedAt').connection('fridgeSensor','home').add()