Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
Loading...
How-to guides to query TerminusDB and TerminusCMS data products using WOQL
How to write filters in WOQL
To use this How-To, first clone the Star Wars demo into your team on TerminusCMS. You will then have full access to the data needed for this tutorial
Since WOQL is a datalog, filters are just part of the query. You can express negative information, or constraints on the variables in order to get a restriction down to the things you want.
For instance, we can write the following query in the query panel for the Star Wars demo:
This results in:
We can ask for a specific example of a vehicle name by filtering on equality.
For instance:
Which results in:
We can also write:
In which we get the complement of the above.
Or, we can use the regex operator to get a wider variety, for instance:
In this case, we get:
A how-to guide for querying arrays and Sets with WOQL
In TerminusDB there are a number of collection types, including List
, Set
, and Array
.
While these all generate JSON lists through the document interface, they have different semantics due to their different realisation in the graph.
Sets are the simplest objects in TerminusDB. They are simply edges with the same name that lead to more than one object.
For instance, an example the document:
To search for results of friends in WOQL, we can simply use triple
.
If you want to get back the values in a specific order, you can use an order_by
clause.
To search a list of objects, you need to traverse the intermediate cons cells. The list is actually a graph structure shaped like:
This can be traversed using a path query as follows:
To search an array, you can use select, and group by.
This will give you back the array value (a person) as well as the index in the array, in order.
A how-to guide to help you order query results using WOQL
To use this How-To, first clone the Star Wars demo into your team on TerminusCMS. You will then have full access to the data needed for this tutorial
order_by
The order_by
keyword will allow you to sort results.
This returns the first two results of people, who have a given eye color, sorted by eye color, in reverse order.
To get the alternative order, you can write:
Or simply:
How to do shortest path queries using the Python client
The shortest path between an object a
and an object b
via a predicate p
can achieved with the use of a path query.
This example uses the DBPedia database which can be cloned from TerminusHub. It gives the shortest path between band members of Whitesnake, bands that these members have been in, the members of these bands, the bands that these members have been in, etc. until finally, we arrive at a band that plays Country music (a 6 hop path).
In Python:
The answer we obtain for the band is Angelfire_(band)
with the path:
How to do schema queries using WOQL with TerminusDB and TerminusCMS data products
To use this How-To, first clone the Star Wars demo into your team on TerminusCMS. You will then have full access to the data needed for this tutorial
In order to query the schema, you can use graph arguments to WOQL. TerminusCMS stores each branch as a pair of graphs, an instance graph and a schema graph.
We can specify the graph by passing it as an argument to the quad
word.
To find all classes in the schema for instance, we can write:
This results in:
The @schema
denotes the default schema prefix, and makes it clear that this identifier lives in the schema namespace rather than the data namespace.
We can also use the typical triple
word if we use from
to set our default graph to schema
instead of instance
.
cls |
---|
@schema:Film
@schema:People
@schema:Planet
@schema:Species
@schema:Starship
@schema:Vehicle
How to add a document using WOQL
To use this How-To, first clone the Star Wars demo into your team on TerminusCMS. You will then have full access to the data needed for this tutorial
You can add a document in WOQL using the insert_document
keyword.
We can also add documents by using a variable. For instance, we can create a new planet for each individual in the star wars universe as follows:
How to read documents using WOQL
To use this How-To, first clone the Star Wars demo into your team on TerminusCMS. You will then have full access to the data needed for this tutorial
You can read a document after finding the document id as follows:
This finds a People
document and makes sure it has the label "Boosk"
. It then reads the document into the variable doc
.
A how-to guide describing the basics of WOQL to help you get started querying
To use this How-To, first clone the Star Wars demo into your team on TerminusCMS. You will then have full access to the data needed for this tutorial
First, go to the WOQL Query Panel in the UI. Now you can type a query using the syntax of the JS client in the panel.
Queries are composed of variables, names, words and values.
Let's start with a simple query which just looks at one field.
We need to describe which variables we want to use, and we do that with Vars
.
Next, we add the limit
word, to limit to 10 entries.
Then we complete the query with a triple
word, using the source
variable, the label
field, and the destination
variable.
The results will come back in a table below in the UI. In the client it will return as a list of JSON objects, having each of the variables described in Vars
bound.
The destination
variable is filled with elements of type string, because label
always terminates in a string. However we can also add other fields to our object, to search for more information by chaining triple
together.
This query results in the following:
and
The .
syntax is actually introducing an implicit and
between triple
words. We can rewrite our query above as:
select
Since we probably do not really need the person
variable, as it is an id, and we are just using it to make sure we are talking about the same person in both triples, we can use select
to remove it.
Now we get back the table with the person
column removed.
How to edit documents using WOQL
We can get a document by Id, by using read_document
. For instance, we can write:
We can also add documents by using a variable. For instance, we can create a new planet for each individual in the star wars universe as follows:
How to delete a document using WOQL
It is possible to delete a document in WOQL using the delete_document
keyword.
First lets insert a document.
Supposing we get back the following:
Now we can delete it with:
Name | Eyes | Person |
---|---|---|
To use this How-To, first into your team on TerminusCMS. You will then have full access to the data needed for this tutorial
To use this How-To, first into your team on TerminusCMS. You will then have full access to the data needed for this tutorial
Luke Skywalker
blue
People/1
Obi-Wan Kenobi
blue-gray
People/10
Anakin Skywalker
blue
People/11
Wilhuff Tarkin
blue
People/12
Chewbacca
blue
People/13
A how-to guide showing mathematical operations in WOQL
WOQL has a number of mathematical operations that can be performed. These include, plus
, minus
, divide
, times
, div
(for integer division), exp
and floor
.
To use these operations you need to evaluate
an arithmetic expression, and then you will be able to bind the result to a variable.
For instance:
This will store the value of 2 times 3 in the variable result
. The bindings which result from this query are:
You can also chain these together, to build up more complicated computations, or use the results obtained by queries to derive new values.
Which results in:
How to perform path queries using WOQL
To use this How-To, first clone the Star Wars demo into your team on TerminusCMS. You will then have full access to the data needed for this tutorial
path
TerminusCMS gives us path queries that allow us to express chains of relationships succinctly.
The path
keyword enables you to find a path through the graph traversing intermediate edges. An example would be finding a group of individuals who have at some point shared a vehicle as pilots, or piloted another vehicle that was shared with someone. This is a transitive relationship and will explore the entire graph.
For instance
This path
means we follow the pilot
field backward (because of the <
arrow), to the vehicle of which the person is a pilot and then follow it forwards pilot>
any number of times but at least once which is what the +
means.
The path itself can also be returned by adding another field, as so:
This can be inspected to understand the manner in which we got from person1
to person2
.
A how to guide to show you how to group query results using WOQL in TerminusDB and TerminusCMS
To use this How-To, first clone the Star Wars demo into your team on TerminusCMS. You will then have full access to the data needed for this tutorial
group_by
If we need to group variables according to some criteria, we can create an aggregate of solutions using group_by
.
A group by is composed of a focus, a template, and a group together with a query.
We will demonstrate this with the following query:
The first argument, here "eyes"
refers to the eyes variable, and is the variable around which to form the group, the focus.
The second ["label"]
is the template, which refers to the variable "label"
. The template will be those things grouped under the first variable.
The third variable v.group
, is the group variable, which will include groups of templates for each set of solutions which shares a focus.
This raw query output will be:
Find an object state at a given time using the Python client.
Connect to an existing database with multiple commit activities.
Get the timestamp
of the last commit before the date specified in date_string
, using the timetuple()
method.
Get the next commit after the timestamp
retrieved in and store commit data in commit_query
.
Get the commit identifier into variable commit_id
after running the commit_query
defined in .
Get the commit object associated with the commit_id
obtained in . Note the full path
of the commit_id
.
Use regular expressions with WOQL and JavaScript.
WOQL provides Pearl Compatible Regular Expressions (PCRE) using the predicate regexp
with the structure:
Examples of basic regular expression pattern matching with WOQL.
Match a string to pattern "t(..)t"
.
Results
Match a string to pattern "m..h"
.
Results
Match start ^
and end $
of a string to pattern "^m...h$"
.
Results
Match start and end of the string to pattern "^(....)-(..)-(..)$"
.
Results
v:All | v:Match |
---|---|
v:All |
---|
v:All |
---|
v:All | v:Year | v:Month | v:Day |
---|---|---|---|
"test"
"es"
"match"
(no match)
"match"
"2020-10-12"
"2020"
"10"
"12"