Looking at the documentation about queries, I don’t really see what’s possible with Logseq’s built-in query language. So, let’s work on improving the documentation together!
This post is a wiki post, so you can edit it any way you see fit. I’ll start by pasting the entire (simple) query page from the documentation. Over the coming days, I’ll also be rewriting it based on my understanding. I hope that also @tienson and others from the @staff and @moderators chime in.
This post is based on the official documentation entry.
What are “Queries”?
Queries are for asking questions from your Logseq graph (collection of notes).
How to write queries?
There’re two kinds of queries:
1: Simple queries by using {{query }}
, the format is something like this:
{{query Something you're looking for}}
You can type /query
to create a simple query.
In this article, we only cover simple queries.
Simple Queries filters
and
Examples:
(and [[page 1]] [[page 2]])
(and (task now later done) [[page]])
or
Examples:
(or [[page 1]] [[page 2]])
not
Examples:
(not [[page 1]] [[page 2]])
between
The format (between start end)
will only support blocks on the journal pages.
There’re several built-in symbols:
today|yesterday|tomorrow|now
-
+|-
numbery|m|w|d|h|min
Examples:
(between -7d +7d)
(between -2w today)
page
Examples: [[questions]]
{{query (page “term/alias”) }}
property (block properties)
Examples:
{{query((property type book))}}
Full-text query
Examples:
task (used to be todo)
Examples:
(task now)
(task now later)
priority
Examples:
(priority a)
(priority a b c)
page-property
Example:
{{query (page-property related "Block embed")}}
page-tags
Example:
{{query((page-tags embed))}}
all-page-tags
Example:
{{query((all-page-tags))}}
sort-by
Format: (sort-by key order)
or (sort-by key)
key
: created-at
| updated-at
order
: desc
or asc
, omit means desc
.
(and (task now later) (sort-by created-at desc))
More query examples
Find the blocks containing page “tag1”
{{query [[tag1]]}}
Find the blocks containing both tag1
and tag2
{{query (and [[tag1]] [[tag2]])}}
Find the blocks containing either tag1
or tag2
{{query (or [[tag1]] [[tag2]])}}
Find the blocks containing tag2
but not tag1
{{query (and [[tag2]] (not [[tag1]])) }}
Find journal blocks created between [[Dec 5th, 2020]] to [[Dec 7th, 2020]]
{{query (between [[Dec 5th,2020]] [[Dec 7th,2020]] ))}}
Find all the blocks created today (you need to enable [[block timestamps]] before using this query)
{{query (between created-at today tomorrow)}}