| name | logseq-queries |
| description | Write Logseq simple queries and advanced Datalog queries. Use when the user wants to search their graph, filter blocks or pages by tags, properties, dates, task status, or any combination of criteria. |
Logseq Queries Skill
Logseq has two query systems. Simple queries handle common searches with a concise syntax. Advanced queries use Datalog for full control over the graph database.
Simple Queries
Simple queries use {{query ...}} syntax. Place them inside a block.
Find all blocks or pages referencing a tag or page:
- {{query [[project/alpha]]}}
- {{query #meeting}}
Find tasks by status:
- {{query (task TODO)}}
- {{query (task DONE)}}
- {{query (task TODO DOING)}}
Find blocks with a property value:
- {{query (property status "active")}}
- {{query (property rating 5)}}
Combine conditions with and:
- {{query (and [[project/alpha]] (task TODO))}}
- {{query (and #meeting (property status "done"))}}
Use or for alternatives:
- {{query (or [[project/alpha]] [[project/beta]])}}
- {{query (or (task TODO) (task DOING))}}
Use not to exclude:
- {{query (and (task TODO) (not [[someday]]))}}
Filter by page vs block:
- {{query (page-tags #project)}}
Filter by date range:
- {{query (between -7d today)}}
- {{query (and (task TODO) (between [[2024-01-01]] [[2024-03-31]]))}}
Date shortcuts: today, yesterday, -7d, -30d, +7d.
Advanced Queries
Advanced queries use Datalog wrapped in #+BEGIN_QUERY ... #+END_QUERY. They give you full control.
The basic structure:
#+BEGIN_QUERY
{:title "My Query Title"
:query [:find (pull ?b [*])
:where
...conditions...]}
#+END_QUERY
Key Attributes
The most commonly queried attributes include :block/content, :block/marker, :block/priority, :block/page, :block/properties, :block/refs, :block/scheduled, :block/deadline, :page/name, :page/tags, and :page/journal?.
For the complete list of queryable attributes, types, and descriptions, see references/ATTRIBUTES.md.
Common Patterns
Find all TODO blocks:
#+BEGIN_QUERY
{:title "All TODOs"
:query [:find (pull ?b [*])
:where
[?b :block/marker "TODO"]]}
#+END_QUERY
Find TODO blocks tagged with a page:
#+BEGIN_QUERY
{:title "Project Alpha TODOs"
:query [:find (pull ?b [*])
:where
[?b :block/marker "TODO"]
[?b :block/refs ?p]
[?p :page/name "project/alpha"]]}
#+END_QUERY
Find blocks with a specific property value:
#+BEGIN_QUERY
{:title "High priority items"
:query [:find (pull ?b [*])
:where
[?b :block/properties ?props]
[(get ?props :priority) ?v]
[(= ?v "high")]]}
#+END_QUERY
Find all journal pages in the last 7 days:
#+BEGIN_QUERY
{:title "Recent journal pages"
:query [:find (pull ?p [*])
:in $ ?start
:where
[?p :page/journal? true]
[?p :page/journal-day ?d]
[(>= ?d ?start)]]
:inputs [:7d-before]}
#+END_QUERY
Find blocks containing specific text:
#+BEGIN_QUERY
{:title "Mentions of deadline"
:query [:find (pull ?b [*])
:where
[?b :block/content ?c]
[(clojure.string/includes? ?c "deadline")]]}
#+END_QUERY
Find pages with a specific tag:
#+BEGIN_QUERY
{:title "All book pages"
:query [:find (pull ?p [*])
:where
[?p :page/tags ?t]
[?t :page/name "book"]]}
#+END_QUERY
Sort results:
#+BEGIN_QUERY
{:title "TODOs sorted by priority"
:query [:find (pull ?b [*])
:where
[?b :block/marker "TODO"]
[?b :block/priority ?pri]]
:result-transform (fn [results]
(sort-by #(get % :block/priority "Z") results))}
#+END_QUERY
Custom view with Hiccup:
#+BEGIN_QUERY
{:title "Pages list"
:query [:find ?name
:where
[?p :page/tags ?t]
[?t :page/name "project"]
[?p :page/original-name ?name]]
:view (fn [results]
[:ul
(for [[name] results]
[:li [:a {:href (str "/page/" name)} name]])])}
#+END_QUERY
:inputs, :result-transform, and :collapsed?
For the complete reference on dynamic inputs, result transforms, and display options, see references/ATTRIBUTES.md.
Tips
- Simple queries are easier to maintain. Use them unless you need something specific.
- Advanced queries can be slow on large graphs. Add specific conditions to narrow results early.
- The
:page/name attribute is always lowercase. Use it for comparisons. Use :page/original-name to display.
- Wrap query blocks in a collapsible parent block to keep pages clean.
- Test queries on a small graph before using on your main vault.