You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: getting_started/python-client/lesson_7.md
+10-10
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,10 @@
1
1
# Lesson 7 - Logical query using triple and WOQL
2
2
3
-
In this lesson we are exploring to a more advance territories. We will make some logical query with WOQL query. In may cases we only need to make document queries with the document interface that we covered in lesson 1 to 5. But in rare occasions, logical queries maybe the more straight forward and easy way to find information form the data we stored in our database
3
+
In this lesson we are exploring more advanced territories. We will do logical queries with WOQL query. In many cases we only need to make document queries with the document interface that we covered in lessons 1 to 5. But on rare occasions, logical queries maybe the more straight forward and easy way to find information from the data we stored in our database
4
4
5
5
## Triples - Subject, Predicate and Object
6
6
7
-
In TerminusDB as store things as triples, each of them is consists of 'Subject', 'Predicate' and 'Object'. We can inspect all the the triples in a graph (in the following example, the instance graph) by using `star()` in `WOQLQuery`:
7
+
In TerminusDB things are stored as triples, each consists of 'Subject', 'Predicate' and 'Object'. We can inspect all of the triples in a graph (in the following example, the instance graph) by using `star()` in `WOQLQuery`:
By inspecting the output of the above code, we see all the triples within our instance graph.
22
22
23
-
With a bit of understand of how triples are related to each other, we can link up a few triples, leaving some "variables" which we wants to find the answer with, we can make WOQL queries which harness the power of logical programming in Prolog. We will explain all of it with examples.
23
+
With a bit of understanding about how triples are related to each other, we can link triples, leaving some "variables" that we want to find the answer to. We can make WOQL queries that harness the power of logical programming in Prolog. We will explain it all with examples.
24
24
25
25
## WOQLQuery - Making logical queries with triples
26
26
27
-
Let's imagine you are working with Awesome Startup in the example. You would like to find our the contact number of Darci, our creative writer to discuss about the next article publication. Here is how it is done:
27
+
Let's imagine you are working with our example company, Awesome Startup. You would like to find the contact number of Darci, the creative writer, to discuss the next article publication. Here is how it is done:
28
28
29
29
```
30
30
from terminusdb_client import WOQLClient
@@ -47,19 +47,19 @@ else:
47
47
48
48
```
49
49
50
-
A few things to note here, first we have to create a WOQLQuery object with the string `"Darci Prosser"` as it needed to be started explicitly as a string in the database, not a string that we used to construct the query like `"v:person"` or `"@schema:name"`.
50
+
A few things to note here, first we have to create a WOQLQuery object with the string `"Darci Prosser"` as it needed to be stated explicitly as a string in the database, not a string that we used to construct the query like `"v:person"` or `"@schema:name"`.
51
51
52
-
Second, the prefix before the `:` is telling TerminusDB how to treat the strings in the query. For example, a `v:`denote a variable so `v:person` is a variable that we don't know and would like to find what it is in the query. On the other hand, `@schema:`denote that it is a property that is defined in the schema, so `@schema:name` says that `name` is the property that we stated for the `Employee` documents in the schema.
52
+
Secondly, the prefix before the `:` is telling TerminusDB how to treat the strings in the query. For example, a `v:`denotes a variable so `v:person` is a variable that we don't know and would like to find what it is in the query. On the other hand, `@schema:`denotes that it is a property that is defined in the schema, so `@schema:name` says that `name` is the property that we stated for the `Employee` documents in the schema.
53
53
54
-
Third, we can linked up the triples that we created with [`wq().triple`](https://terminusdb.github.io/terminusdb-client-python/woqlQuery.html#terminusdb_client.WOQLQuery.triple) with either [`wq().woql_and`](https://terminusdb.github.io/terminusdb-client-python/woqlQuery.html#terminusdb_client.WOQLQuery.woql_and) or a simple `+` like we did above.
54
+
Finally, we can link the triples we created with [`wq().triple`](https://terminusdb.github.io/terminusdb-client-python/woqlQuery.html#terminusdb_client.WOQLQuery.triple) with either [`wq().woql_and`](https://terminusdb.github.io/terminusdb-client-python/woqlQuery.html#terminusdb_client.WOQLQuery.woql_and) or a simple `+` like we did above.
55
55
56
56
So the query above can be interpreted as:
57
57
58
58
`There is a person who's name is "Darci Prosser" and I would like to know that person's contact number.`
59
59
60
-
As you see, making WOQL queries is quite logical, just need to think what questions you are asking and how to link up all the questions and informations with triple. With some practice you can get use to it. Let's try another one in the next example.
60
+
As you see, making WOQL queries is quite logical, you just need to think about what question you are asking and how to link all the questions and parts of the triple. With some practice you will get used to it. Let's try another example.
61
61
62
-
Let's say you have called Darci and unfortunately she is on holiday and you cannot wait for her to be back to talk about it. You decided to contact her manager instead. However, you do not know who her manager is and don't know the manager's contact number either. But fear not! With WOQL query it is not more complicated that making our previous query.
62
+
Let's say you have called Darci and unfortunately she is on holiday and you cannot wait for her to get back. You decided to contact her manager instead. However, you do not know who her manager is or their contact number either. But fear not! With WOQL query it is a logical query similar to the example above.
This time, instead of asking for Darci's contact number, you ask `who is the manager` and set the manager as a variable `v:manager`. Then with `v:manager` you can find out the `name` and `contact_number` of the manager.
79
79
80
-
As you can see, when the question get more complicated, more triple is added to link up extra information. But the structure of the query is the same and it is much efficient and easier than joining tables many times in SQL queries to get the same answer.
80
+
As you can see, when the questions get more complicated, more triples are added to link the extra information. The structure of the query reamins the same and is much easier and more efficient than joining tables many times in SQL queries to get the same answer.
81
81
82
82
Feel free to practice and play with the WOQL query. The code we showed in this lesson can be found in the file [woql_query.py](woql_query.py)
0 commit comments