Simple queries filters

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.

2: Advanced queries.

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:

  1. today|yesterday|tomorrow|now
  2. +|- number y|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)}}

2 Likes

Thanks,

What are your examples for :

Full-text query
Examples:

?

Regards

1 Like

I’ve been working my way through this page to help make sure I understand and can implement simple queries in cases that make sense for my workflow.

I’m finding that it would be very helpful to have some more “preamble” on what, exactly queries are doing, what filters are, and what specific filters are supposed to do before going right into the examples. Right now everything happens very abruptly. I found the examples in Boolean logic 101 to be immensely helpful, so maybe similar hand holding here would be useful?

I’d be happy to help make changes to this effect at some point, but I don’t feel I have a good enough grasp on writing queries just yet to jump in. So, I’m writing it down here now for accountability!