Getting Started
Before the detailed introduction to Cypher, let’s take a look briefly first. In this chapter, how to create a graph, make a query, and modify the created graph will be addressed.
Creating Graph
AgensGraph can store several graphs in a single database. So the cypher query is usable only after creating and selecting a graph.
Create Graph
CREATE GRAPH graphName; SET graph_path = graphName;
CREATE GRAPH
is a query to create a graph and the graph name must be defined.graph_path
is a variable for which graph you use. WithSET
command, you can set the graph you want to use.Create Labels
CREATE VLABEL vlabelName; CREATE ELABEL elabelName; CREATE VLABEL childVlabelName inherits (parentVlabelName); CREATE ELABEL childElabelName inherits (parentElabelName1, parentElabelName2);
CREATE VLABEL
andCREATE ELABEL
are the command to create vlabel and elabel. Each command must be used with its name.Inherits()
is a command to inherit other labels. When you create a label, if you put the parent label names after the child label you want to create, the child label will inherit the parent labels mentioned. The parent label can be more than one.Create vertices and edges
CREATE (:person {name: 'Jack'}); CREATE (:person {name: 'Emily'})-[:knows]->(:person {name: 'Tom'});
The above queries show how to create vertex and edge. This command should be used correctly with a pattern for creating vertex or edge. (You will see various clauses and patterns for vertex and edge in detail in other chapters)
Querying Graph
Querying to a graph means defining a pattern of graph you want to look for and extracting information from the responded graph.
MATCH (:person {name: 'Jack'})-[:likes]->(v:person)
RETURN v.name;
Result :
name
-------
Emily
(1 row)
The MATCH
clause is used to retrieve the matched graph data with the mentioned in MATCH clause, and only the mentioned attributes in RETURN
clause will be returned.
Manipulating Graph
In order to modify the existing graph data, the patterns in the MATCH clause should be used.
MATCH (v:person {name: 'Jack'})
SET v.age = '24';
With the SET
clause, you can modify the property value of vertex or edge.