Graql Insert Queries
Edit me

The page documents use of the Graql insert query, which will insert a specified variable pattern into the graph. To follow along, or experiment further, with the examples given below, please load the basic-genealogy.gql file, which can be found in the examples directory of the Grakn installation zip, or on Github.

match-insert

If a match query is provided, the query will insert the given variable patterns for every result of the query. The pattern describes properties to set on a particular concept and can optionally be bound to a variable or an ID.

In the example below, we insert additional (fictional) information for a person entity who we have matched through identifier Mary Guthrie.

match $p has identifier "Mary Guthrie"; insert $p has middlename "Mathilda"; $p has birth-date "1902-01-01"; $p has death-date "1952-01-01"; $p has age 50;
qb.match(var("p").has("identifier", "Mary Guthrie"))
    .insert(var("p").has("middlename", "Mathilda"), 
        var("p").has("birth-date", LocalDateTime.of(1902, 1, 1, 0, 0, 0).toString()),
        var("p").has("death-date", LocalDateTime.of(1952, 1, 1, 0, 0, 0).toString()),
        var("p").has("age", 50)
    ).execute();

Properties

isa

Set the type of the inserted concept.

insert has identifier "Titus Groan" isa person;
qb.insert(var().has("identifier", "Titus Groan").isa("person")).execute();

id

It is not possible to insert a concept with the given id, as this is the job of the system. However, if you attempt to insert by id, you will retrieve a concept if one with that id already exists. The created or retrieved concept can then be modified with further properties.


insert id "1376496" isa person;

qb.insert(var().id(ConceptId.of("1376496")).isa("person")).execute();

val

Set the value of the concept.

insert val "Ash" isa surname;
qb.insert(var().val("Ash").isa("surname")).execute();

has

Add a resource of the given type to the concept.

insert isa person, has identifier "Fuchsia Groan" has gender "female";
qb.insert(var().isa("person").has("identifier", "Fuchsia Groan").has("gender", "female")).execute();

relation

Make the concept a relation that relates the given role players, playing the given roles.
(With apologies to ‘Gormenghast’ fans, who will be aware that Titus and Fuchsia are siblings and thus cannot marry).

match $p1 has identifier "Titus Groan"; $p2 has identifier "Fuchsia Groan"; insert (spouse1: $p1, spouse2: $p2) isa marriage;
qb.match(
  var("p1").has("name", "Titus Groan"),
  var("p2").has("name", "Fuchsia Groan")
).insert(
  var()
    .rel("spouse1", "p1")
    .rel("spouse2", "p2")
    .isa("marriage")
).execute();

Type Properties

The following properties only apply to types.

sub

Set up a hierarchy.

insert man sub person;
insert woman sub person;
qb.insert(label("man").sub("person")).execute();
qb.insert(label("woman").sub("person")).execute();

relates

Add a role to a relation.

insert siblings sub relation, relates sibling1, relates sibling2;
qb.insert(
  label("siblings").sub("relation")
    .relates("sibling1").relates("sibling2")
).execute();

plays

Allow the concept type to play the given role.

insert person plays sibling1;
insert person plays sibling2;
qb.insert(label("person").plays("sibling1")).execute();
qb.insert(label("person").plays("sibling2")).execute();

has

Allow the concept type to have the given resource.

This is done by creating a specific relation relating the concept and resource.

insert person has nickname;
qb.insert(label("person").has("nickname")).execute();

Comments

Want to leave a comment? Visit the issues on Github for this page (you’ll need a GitHub account). You are also welcome to contribute to our documentation directly via the “Edit me” button at the top of the page.

has

Tags: graql