Logseq Queries and Advanced Features
AI-Generated Content
Logseq Queries and Advanced Features
Your notes are only as valuable as your ability to find and connect them. Logseq’s query system transforms your graph from a static repository into a dynamic, responsive knowledge workspace. By mastering both its intuitive simple queries and its powerful advanced Datalog engine, you can create live dashboards, surface hidden connections, and build a PKM system that actively works for you.
From Simple Filters to Foundational Power
The gateway to dynamic knowledge in Logseq begins with simple queries. These use a natural language-like syntax within double parentheses to filter your blocks based on common attributes. Think of them as quick searches with superpowers. The most powerful simple queries filter by page or block properties, tags, or dates.
For example, {{query [[project]]}} will instantly find all blocks linked to the [[project]] page. To find tasks tagged with #meeting that are due today, you could use {{query (and [[TODO]] #meeting {{today}} )}}. The parentheses allow for basic boolean logic like and, or, and not. This is immensely useful for creating a daily agenda: {{query (and [[TODO]] {{today}} (not #someday))}} shows today's actionable items while filtering out future maybes.
The true foundation for sophisticated queries, however, is the consistent use of properties. Properties are key-value pairs you add to a block (block properties) or a page (page properties). A block property for a book note might look like:
author:: George Orwell
year-published:: 1949
status:: [[To Read]]Page properties, defined at the top of a page, describe the entire page, like a project’s deadline:: 2024-12-01 or a person’s role:: Designer. Queries don't just search text; they can specifically target these property values, enabling precise, metadata-driven retrieval that simple text matching can never achieve.
Harnessing Advanced Datalog Queries
When your filtering needs surpass the capabilities of simple syntax, you graduate to advanced queries using Datalog. Datalog is a declarative logic programming language that Logseq adopts to query its underlying database. Instead of describing how to find data, you declare what data you want. This happens in a dedicated code block tagged with #+BEGIN_QUERY and #+END_QUERY.
A basic Datalog query has a structure comprising variables, clauses, and a results table. Here is the skeleton:
#+BEGIN_QUERY
{:title "My Query"
:query [:find (pull ?b [*])
:where
[?b :block/content ?content]]
}
#+END_QUERYThe :find clause specifies what to return—here, (pull ?b [*]) gets the entire block entity. The :where clause defines the conditions. This example finds all blocks, which isn't useful alone. The power comes from adding specific :where conditions.
This is where complex boolean logic and property-based filtering shine. To find all blocks tagged #idea that also reference a page on [[PKM]] but are not marked [[DONE]], your :where clause becomes a precise filter:
:where
[?b :block/content ?content]
[?b :block/refs [:block/name "idea"]]
[?b :block/refs [:block/name "PKM"]]
(not [?b :block/marker ?marker]
[(= ?marker "DONE")])You can query properties directly. To find all books by "George Orwell" with a status of "To Read":
:where
[?b :block/properties ?props]
[(get ?props :author) ?author]
[(= ?author "George Orwell")]
[(get ?props :status) ?status]
[(= ?status "To Read")]This ability to chain conditions, use logical negation, and traverse property maps makes Datalog the tool for building complex, customized views of your knowledge.
Building Dynamic Dashboards and Views
The ultimate application of queries is to create dynamic dashboards and views. A dashboard is just a page that aggregates query results from across your entire graph. Instead of manually updating a to-do list or a literature review, you embed queries that live-update as you tag blocks, set properties, and mark items complete.
For instance, a "Weekly Review" dashboard might contain several queries:
- Overdue Actions: A Datalog query finding
[[TODO]]blocks with adue-date::property that is in the past. - New Contacts: A simple query showing all blocks tagged
#personcreated in the last 7 days:{{query [[person]] {{between [[last week]] [[today]]}} }}. - Active Projects: A query pulling all pages with a page property of
project-status:: Active, displaying their next actions.
Because these queries run against the current state of your database, your dashboard is always current. This is what makes your knowledge system alive and responsive. You’re not just storing information; you’re creating a control panel that gives you immediate insight into the state of your projects, reading, and ideas. The view you create for project planning can be entirely different from the view for creative brainstorming, yet both draw from the same, ever-growing base of notes.
Common Pitfalls
- Ignoring Properties for Tags: A common mistake is using only tags (e.g.,
#author-George-Orwell) when a property (author:: George Orwell) is better. Tags are great for broad categorization, but properties enable richer querying—you can't easily query a tag for a specific value like "Orwell". Use properties for defined metadata like dates, people, ratings, or statuses. - Overly Complex Simple Queries: If your
{{query ... }}syntax is becoming a tangled nest of parentheses, it's a sign to switch to a Datalog query. Datalog’s structure makes complex logic more readable and maintainable. Simple queries are for quick filters; use Datalog for advanced logic. - Forgetting the Pull Pattern: In Datalog,
:find ?bonly returns a block ID. You almost always want:find (pull ?b [*])to get the block's content, properties, and references. For specific data, use(pull ?b [:block/content :block/properties]). - Static Mindset with Dashboards: The pitfall is creating a beautiful dashboard once and never updating it. Periodically review the queries in your dashboards. Are they still serving your needs? As your workflow evolves, so should your queries. A dynamic dashboard is a tool that should evolve with you.
Summary
- Simple queries (
{{query ...}}) provide an accessible, natural language-like way to filter blocks by tags, dates, or linked pages using basic boolean logic for everyday retrieval. - Block and page properties (key-value pairs like
status::) are the essential metadata that enable precise, powerful querying beyond simple text or tag matching. - Advanced Datalog queries (within
#+BEGIN_QUERY) unlock complex boolean logic and property-based filtering, allowing you to declare exactly what data you want from your graph using a logical programming syntax. - Combined, these tools allow you to build dynamic dashboards—pages of live-updating queries—that transform your graph from an archive into an active, responsive knowledge workspace tailored to your current goals and contexts.