Skip to content
Permalink
Branch: master
Find file Copy path
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
105 lines (85 sloc) 4.37 KB
pageTitle keywords longTailKeywords Summary
Insert Query
graql, insert query, insertion
grakn insert data, graql insert query, graql create instances
Insert queries in Grakn.

Insert Instances of an Entity Type

To insert an instance of an entity type into the knowledge graph, we use the insert keyword followed by a series of statements that are similar to match patterns. To try the following examples with one of the Grakn clients, follows these Clients Guide.

[tab:Graql]

insert $p isa person, has full-name "John Parkson", has gender "male", has email "john.parkson@gmail.com", has phone-number "+44-1234=567890";

[tab:end]

[tab:Java]

GraqlInsert query = Graql.insert(
  var("p").isa("person").has("full-name", "John Parkson").has("email", "john.parkson@gmail.com").has("phone-number", "+44-1234-567890")
);

[tab:end]

This match insert query:

  1. Assigns the surname attribute of a person with id of V41016 to variable $s.
  2. Inserts a person entity with the surname attribute holding the value of the variable $s.

Insert Instances of an Attribute Type

Similar to inserting an instance of an entity, to insert an instance of an attribute, we use the insert keyword followed by the variable pattern to describe the attribute of interest and its value.

[tab:Graql]

insert $x isa emotion; $x "like";

[tab:end]

[tab:Java]

GraqlInsert query = Graql.insert(
  var("x").isa("emotion").val("like")
);

[tab:end]

Insert Instances of a Relation Type

Given the dependent nature of relations, inserting an instance of a relation is quite different from that of an entity. The roles of a relation to be inserted are expected to be played by instances that already exist in the knowledge graph. Therefore inserting a relation is always preceded by matching the roleplayers - what is commonly called the match insert. What follows the insert keyword, is a series of statements that are similar to the match patterns.

[tab:Graql]

match
  $org isa organisation, has name "Facelook";
  $person isa person, has email "tanya.arnold@gmail.com";
insert $new-employment (employer: $org, employee: $person) isa employment;
  $new-employment has reference-id "WGFTSH";

[tab:end]

[tab:Java]

GraqlInsert query = Graql.match(
  var("org").isa("organisation").has("name", "Facelook"),
  var("p").isa("person").has("email", "tanya.arnold@gmail.com")
).insert(
  var("emp").isa("employment").rel("employer", "org").rel("employee", "p").has("reference-id", "WGFTSH")
);

[tab:end]

This match insert query:

  1. Matches the organisation that plays employer, assigned to variable $c.
  2. Matches the person that plays employee, assigned to variable $p.
  3. Inserts an employment relation with $c and $p as its roleplayers, assigned to variable $emp.
  4. Inserts the ownership of reference-id with value WGFTSH by to the $emp relation instance.

Clients Guide

[Note] **For those developing with Client [Java](../03-client-api/01-java.md)**: Executing a `insert` query, is as simple as calling the [`execute()`](../03-client-api/01-java.md#eagerly-execute-a-graql-query) method on a transaction and passing the query object to it.
[Note] **For those developing with Client [Node.js](../03-client-api/03-nodejs.md)**: Executing a `insert` query, is as simple as passing the Graql(string) query to the [`query()`](../03-client-api/03-nodejs.md#lazily-execute-a-graql-query) function available on the [`transaction`](../03-client-api/03-nodejs.md#transaction) object.
[Note] **For those developing with Client [Python](../03-client-api/02-python.md)**: Executing a `insert` query, is as simple as passing the Graql(string) query to the [`query()`](../03-client-api/02-python.md#lazily-execute-a-graql-query) method available on the [`transaction`](../03-client-api/02-python.md#transaction) object.

Summary

An insert query optionally preceded by a match clause is used to insert a data instance into the knowledge graph.

Next, we learn how to delete data from a knowledge graph.

You can’t perform that action at this time.