Hint: Nested Tasks in advanced query

So last night and this morning, I got a little into the querying in Logseq.
One of the problems I went into, was the following:

Task

Let’s say I have a page about some project TestProject.
Inside TestProject, I want to see all tasks that link to it, and sort them, and also be able to unfold them (for task-specific information and sub-tasks).

Tasks might be scattered, but for this small example, let’s say those are all the tasks:
image
and
image

Problem

In simple queries the main functionality works really well like this:

{{query (and (todo now later done) [[TestProject]]) }}

Now, within TestProject, it’s not that nice to link to itself. Also, as I said, I might want to filter or sort the output.

With advanced queries, I can search for stuff that links to TestProject (within the same page), and can also filter and sort, but the following query doesn’t put the whole block to the output. OR rather, It puts the block, but not the nested blocks.

#+BEGIN_QUERY
{
  :query [
    :find (pull ?b [*])
    :in $ ?current-page
    :where
      [?p :block/name ?current-page]
      [?b :block/path-refs ?p]
      [?b :block/marker ?marker]
      [(contains? #{"TODO" "DOING" "DONE" "NOW" "LATER" "WAITING"} ?marker)]]
  :inputs [:current-page]}
#+END_QUERY

Solution

By doing as suggested by MeasuredTiredness in Query for pages with property value that contains specific word - #2 by MeasuredTiredness, I looked up what happens for a simple query, by checking the console log.
The secret is, in the simple query, it’s not (pull ?b [*]), but instead a lot of stuff in the place of *. Now, it turns out, the interesting part of this lots of stuff is {:block/_parent ...}.
To be fair, I don’t know exactly, what this is, but it seems that it makes the block behave as an expandable block, as I wanted.
Thus, the wanted query (without sorting yet) is the following:

#+BEGIN_QUERY
{
  :query [
    :find (pull ?b [* {:block/_parent ...}])
    :in $ ?current-page
    :where
      [?p :block/name ?current-page]
      [?b :block/path-refs ?p]
      [?b :block/marker ?marker]
      [(contains? #{"TODO" "DOING" "DONE" "NOW" "LATER" "WAITING"} ?marker)]]
  :inputs [:current-page]}
#+END_QUERY

Hello. I’ve copied and pasted your query to a testing page (similar to your scenario), but it seems doesn’t work (the query still shows “parent block” only). Do you have any idea what’s going wrong here? Many thanks.

Hi :slight_smile:
Unfortunately, it always opens with those bullets collapsed. But at least you can open them.
I mean, if you hover with the mouse to the left of the bullet, there should be this triangle to show/hide nested elements.

Brilliant… it works, this will come in handy. Thanks bud.

1 Like

You saved my day! Thanks @quor. I had even opened an issue for this: Can't uncollapse when using advanced queries · Issue #3049 · logseq/logseq · GitHub

Does anybody know if there’s a way to show the blocks unfolded by default?

1 Like

I’m glad I helped! :slight_smile:

Didn’t find a way to show the blocks unfolded by default.
I think I saw somewhere on discord that all of this is considered a bug, so I have my fingers crossed that this will be solved in some future release.
(Also see Cannot expand/collapse nested blocks inside queries or linked references · Issue #3744 · logseq/logseq · GitHub )

1 Like